fix(criterion): compile asc/desc regex only once

use once_cell instead of lazy_static

reorder imports
This commit is contained in:
Alexey Shekhirin 2021-03-30 12:10:06 +03:00
parent 5a1d3609a9
commit 2cb32edaa9

View File

@ -4,9 +4,14 @@ use std::fmt;
use anyhow::{Context, bail};
use regex::Regex;
use serde::{Serialize, Deserialize};
use once_cell::sync::Lazy;
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)]
pub enum Criterion {
/// Sorted by increasing number of typos.
@ -39,8 +44,7 @@ impl Criterion {
"wordsposition" => Ok(Criterion::WordsPosition),
"exactness" => Ok(Criterion::Exactness),
text => {
let re = Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#)?;
let caps = re.captures(text).with_context(|| format!("unknown criterion name: {}", text))?;
let caps = ASC_DESC_REGEX.captures(text).with_context(|| format!("unknown criterion name: {}", text))?;
let order = caps.get(1).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))?;