mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-24 13:40:31 +01:00
fix(criterion): compile asc/desc regex only once
use once_cell instead of lazy_static reorder imports
This commit is contained in:
parent
5a1d3609a9
commit
2cb32edaa9
@ -4,9 +4,14 @@ use std::fmt;
|
|||||||
use anyhow::{Context, bail};
|
use anyhow::{Context, bail};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::facet::FacetType;
|
use crate::facet::FacetType;
|
||||||
|
|
||||||
|
static ASC_DESC_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||||
|
Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap()
|
||||||
|
});
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
pub enum Criterion {
|
pub enum Criterion {
|
||||||
/// Sorted by increasing number of typos.
|
/// Sorted by increasing number of typos.
|
||||||
@ -39,8 +44,7 @@ impl Criterion {
|
|||||||
"wordsposition" => Ok(Criterion::WordsPosition),
|
"wordsposition" => Ok(Criterion::WordsPosition),
|
||||||
"exactness" => Ok(Criterion::Exactness),
|
"exactness" => Ok(Criterion::Exactness),
|
||||||
text => {
|
text => {
|
||||||
let re = Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#)?;
|
let caps = ASC_DESC_REGEX.captures(text).with_context(|| format!("unknown criterion name: {}", text))?;
|
||||||
let caps = re.captures(text).with_context(|| format!("unknown criterion name: {}", text))?;
|
|
||||||
let order = caps.get(1).unwrap().as_str();
|
let order = caps.get(1).unwrap().as_str();
|
||||||
let field_name = caps.get(2).unwrap().as_str();
|
let field_name = caps.get(2).unwrap().as_str();
|
||||||
faceted_attributes.get(field_name).with_context(|| format!("Can't use {:?} as a criterion as it isn't a faceted field.", field_name))?;
|
faceted_attributes.get(field_name).with_context(|| format!("Can't use {:?} as a criterion as it isn't a faceted field.", field_name))?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user