Introduce the new Sort criterion and AscDesc enum

This commit is contained in:
Kerollmops
2021-08-17 15:05:00 +02:00
parent 5b88df508e
commit 687cd2e205
4 changed files with 64 additions and 9 deletions

View File

@ -12,6 +12,9 @@ pub enum Criterion {
Words,
/// Sorted by increasing number of typos.
Typo,
/// Dynamically sort at query time the documents. None, one or multiple Asc/Desc sortable
/// attributes can be used in place of this criterion at query time.
Sort,
/// Sorted by increasing distance between matched query terms.
Proximity,
/// Documents with quey words contained in more important
@ -38,26 +41,46 @@ impl Criterion {
impl FromStr for Criterion {
type Err = Error;
fn from_str(txt: &str) -> Result<Criterion, Self::Err> {
match txt {
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
match text {
"words" => Ok(Criterion::Words),
"typo" => Ok(Criterion::Typo),
"sort" => Ok(Criterion::Sort),
"proximity" => Ok(Criterion::Proximity),
"attribute" => Ok(Criterion::Attribute),
"exactness" => Ok(Criterion::Exactness),
text => match text.rsplit_once(':') {
Some((field_name, "asc")) => Ok(Criterion::Asc(field_name.to_string())),
Some((field_name, "desc")) => Ok(Criterion::Desc(field_name.to_string())),
_ => Err(UserError::InvalidCriterionName { name: text.to_string() }.into()),
text => match AscDesc::from_str(text) {
Ok(AscDesc::Asc(field)) => Ok(Criterion::Asc(field)),
Ok(AscDesc::Desc(field)) => Ok(Criterion::Desc(field)),
Err(error) => Err(error.into()),
},
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum AscDesc {
Asc(String),
Desc(String),
}
impl FromStr for AscDesc {
type Err = UserError;
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
match text.rsplit_once(':') {
Some((field_name, "asc")) => Ok(AscDesc::Asc(field_name.to_string())),
Some((field_name, "desc")) => Ok(AscDesc::Desc(field_name.to_string())),
_ => Err(UserError::InvalidCriterionName { name: text.to_string() }),
}
}
}
pub fn default_criteria() -> Vec<Criterion> {
vec![
Criterion::Words,
Criterion::Typo,
Criterion::Sort,
Criterion::Proximity,
Criterion::Attribute,
Criterion::Exactness,
@ -71,6 +94,7 @@ impl fmt::Display for Criterion {
match self {
Words => f.write_str("words"),
Typo => f.write_str("typo"),
Sort => f.write_str("sort"),
Proximity => f.write_str("proximity"),
Attribute => f.write_str("attribute"),
Exactness => f.write_str("exactness"),