2021-03-06 02:54:46 +08:00
|
|
|
use std::fmt;
|
2021-06-01 21:48:38 +08:00
|
|
|
use std::str::FromStr;
|
2020-11-27 19:14:56 +08:00
|
|
|
|
2020-12-04 19:02:22 +08:00
|
|
|
use regex::Regex;
|
2020-11-27 19:14:56 +08:00
|
|
|
use serde::{Serialize, Deserialize};
|
2021-03-30 17:10:06 +08:00
|
|
|
use once_cell::sync::Lazy;
|
2020-11-27 19:14:56 +08:00
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
use crate::error::{Error, UserError};
|
|
|
|
|
2021-03-30 17:10:06 +08:00
|
|
|
static ASC_DESC_REGEX: Lazy<Regex> = Lazy::new(|| {
|
|
|
|
Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap()
|
|
|
|
});
|
|
|
|
|
2021-01-21 00:27:43 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
2020-08-12 16:43:02 +08:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
2021-04-02 01:13:18 +08:00
|
|
|
/// Query words at the front of an attribute is considered better than if it was at the back.
|
2020-08-12 16:43:02 +08:00
|
|
|
Words,
|
2021-04-02 01:13:18 +08:00
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
2020-08-12 16:43:02 +08:00
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
|
|
|
/// attributes are considred better.
|
|
|
|
Attribute,
|
|
|
|
/// Sorted by the similarity of the matched words with the query words.
|
|
|
|
Exactness,
|
|
|
|
/// Sorted by the increasing value of the field specified.
|
2021-01-21 00:27:43 +08:00
|
|
|
Asc(String),
|
2020-08-12 16:43:02 +08:00
|
|
|
/// Sorted by the decreasing value of the field specified.
|
2021-01-21 00:27:43 +08:00
|
|
|
Desc(String),
|
2020-08-12 16:43:02 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 23:03:27 +08:00
|
|
|
impl Criterion {
|
|
|
|
/// Returns the field name parameter of this criterion.
|
|
|
|
pub fn field_name(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
Criterion::Asc(name) | Criterion::Desc(name) => Some(name),
|
|
|
|
_otherwise => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 21:48:38 +08:00
|
|
|
impl FromStr for Criterion {
|
2021-06-14 22:46:19 +08:00
|
|
|
type Err = Error;
|
2021-06-01 21:48:38 +08:00
|
|
|
|
|
|
|
fn from_str(txt: &str) -> Result<Criterion, Self::Err> {
|
2020-12-04 19:02:22 +08:00
|
|
|
match txt {
|
|
|
|
"words" => Ok(Criterion::Words),
|
2021-04-02 01:13:18 +08:00
|
|
|
"typo" => Ok(Criterion::Typo),
|
2020-12-04 19:02:22 +08:00
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
|
|
|
text => {
|
2021-06-14 22:46:19 +08:00
|
|
|
let caps = ASC_DESC_REGEX.captures(text).ok_or_else(|| {
|
|
|
|
UserError::InvalidCriterionName { name: text.to_string() }
|
|
|
|
})?;
|
2020-12-04 19:02:22 +08:00
|
|
|
let order = caps.get(1).unwrap().as_str();
|
|
|
|
let field_name = caps.get(2).unwrap().as_str();
|
|
|
|
match order {
|
2021-01-21 00:27:43 +08:00
|
|
|
"asc" => Ok(Criterion::Asc(field_name.to_string())),
|
|
|
|
"desc" => Ok(Criterion::Desc(field_name.to_string())),
|
2021-06-14 22:46:19 +08:00
|
|
|
text => return Err(UserError::InvalidCriterionName { name: text.to_string() }.into()),
|
2020-12-04 19:02:22 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:43:02 +08:00
|
|
|
pub fn default_criteria() -> Vec<Criterion> {
|
|
|
|
vec![
|
|
|
|
Criterion::Words,
|
2021-04-02 01:13:18 +08:00
|
|
|
Criterion::Typo,
|
2020-08-12 16:43:02 +08:00
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Attribute,
|
|
|
|
Criterion::Exactness,
|
|
|
|
]
|
|
|
|
}
|
2021-03-06 02:54:46 +08:00
|
|
|
|
|
|
|
impl fmt::Display for Criterion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Criterion::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Words => f.write_str("words"),
|
2021-04-02 01:13:18 +08:00
|
|
|
Typo => f.write_str("typo"),
|
2021-03-06 02:54:46 +08:00
|
|
|
Proximity => f.write_str("proximity"),
|
|
|
|
Attribute => f.write_str("attribute"),
|
|
|
|
Exactness => f.write_str("exactness"),
|
2021-03-08 19:09:09 +08:00
|
|
|
Asc(attr) => write!(f, "asc({})", attr),
|
|
|
|
Desc(attr) => write!(f, "desc({})", attr),
|
2021-03-06 02:54:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|