2021-03-05 19:54:46 +01:00
|
|
|
use std::fmt;
|
2021-06-01 15:48:38 +02:00
|
|
|
use std::str::FromStr;
|
2020-11-27 12:14:56 +01:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-11-27 12:14:56 +01:00
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
use crate::error::{Error, UserError};
|
|
|
|
|
2021-01-20 17:27:43 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
2020-08-12 10:43:02 +02:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
2021-04-01 19:13:18 +02:00
|
|
|
/// Query words at the front of an attribute is considered better than if it was at the back.
|
2020-08-12 10:43:02 +02:00
|
|
|
Words,
|
2021-04-01 19:13:18 +02:00
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
2021-08-23 11:37:18 +02:00
|
|
|
/// 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,
|
2020-08-12 10:43:02 +02:00
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
2021-08-23 11:37:18 +02:00
|
|
|
/// attributes are considered better.
|
2020-08-12 10:43:02 +02:00
|
|
|
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-20 17:27:43 +01:00
|
|
|
Asc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
/// Sorted by the decreasing value of the field specified.
|
2021-01-20 17:27:43 +01:00
|
|
|
Desc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-08 17:03:27 +02: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 15:48:38 +02:00
|
|
|
impl FromStr for Criterion {
|
2021-06-14 16:46:19 +02:00
|
|
|
type Err = Error;
|
2021-06-01 15:48:38 +02:00
|
|
|
|
2021-08-23 11:37:18 +02:00
|
|
|
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
|
|
|
match text {
|
2020-12-04 12:02:22 +01:00
|
|
|
"words" => Ok(Criterion::Words),
|
2021-04-01 19:13:18 +02:00
|
|
|
"typo" => Ok(Criterion::Typo),
|
2021-08-23 11:37:18 +02:00
|
|
|
"sort" => Ok(Criterion::Sort),
|
2020-12-04 12:02:22 +01:00
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
2021-08-23 11:37:18 +02:00
|
|
|
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 AscDesc {
|
|
|
|
pub fn field(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
AscDesc::Asc(field) => field,
|
|
|
|
AscDesc::Desc(field) => field,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() }),
|
2021-08-17 15:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:43:02 +02:00
|
|
|
pub fn default_criteria() -> Vec<Criterion> {
|
|
|
|
vec![
|
|
|
|
Criterion::Words,
|
2021-04-01 19:13:18 +02:00
|
|
|
Criterion::Typo,
|
2021-08-23 11:37:18 +02:00
|
|
|
Criterion::Sort,
|
2020-08-12 10:43:02 +02:00
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Attribute,
|
|
|
|
Criterion::Exactness,
|
|
|
|
]
|
|
|
|
}
|
2021-03-05 19:54:46 +01:00
|
|
|
|
|
|
|
impl fmt::Display for Criterion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Criterion::*;
|
|
|
|
|
|
|
|
match self {
|
2021-06-16 18:33:33 +02:00
|
|
|
Words => f.write_str("words"),
|
|
|
|
Typo => f.write_str("typo"),
|
2021-08-23 11:37:18 +02:00
|
|
|
Sort => f.write_str("sort"),
|
2021-06-16 18:33:33 +02:00
|
|
|
Proximity => f.write_str("proximity"),
|
|
|
|
Attribute => f.write_str("attribute"),
|
|
|
|
Exactness => f.write_str("exactness"),
|
2021-08-23 11:37:18 +02:00
|
|
|
Asc(attr) => write!(f, "{}:asc", attr),
|
|
|
|
Desc(attr) => write!(f, "{}:desc", attr),
|
2021-03-05 19:54:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|