2020-01-16 19:19:44 +01:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
|
|
|
use std::str::FromStr;
|
2020-02-11 15:16:02 +01:00
|
|
|
use std::iter::IntoIterator;
|
2020-01-08 14:17:38 +01:00
|
|
|
|
2020-01-28 17:45:29 +01:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize};
|
2020-01-14 17:26:27 +01:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2020-02-26 18:24:19 +01:00
|
|
|
use self::RankingRule::*;
|
|
|
|
|
|
|
|
pub const DEFAULT_RANKING_RULES: [RankingRule; 6] = [Typo, Words, Proximity, Attribute, WordsPosition, Exactness];
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
static RANKING_RULE_REGEX: Lazy<regex::Regex> = Lazy::new(|| {
|
2020-06-26 22:09:34 +02:00
|
|
|
regex::Regex::new(r"(asc|desc)\(([a-zA-Z0-9-_]*)\)").unwrap()
|
2020-01-14 17:26:27 +01:00
|
|
|
});
|
|
|
|
|
2020-01-08 14:17:38 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
2020-01-21 12:03:48 +01:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2020-01-08 14:17:38 +01:00
|
|
|
pub struct Settings {
|
2020-01-28 17:45:29 +01:00
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub ranking_rules: Option<Option<Vec<String>>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
2020-02-25 16:10:34 +01:00
|
|
|
pub distinct_attribute: Option<Option<String>>,
|
2020-01-28 17:45:29 +01:00
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
2020-01-29 18:30:21 +01:00
|
|
|
pub searchable_attributes: Option<Option<Vec<String>>>,
|
2020-01-28 17:45:29 +01:00
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
2020-01-29 18:30:21 +01:00
|
|
|
pub displayed_attributes: Option<Option<HashSet<String>>>,
|
2020-01-28 17:45:29 +01:00
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub stop_words: Option<Option<BTreeSet<String>>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
2020-02-25 15:51:37 +01:00
|
|
|
pub accept_new_fields: Option<Option<bool>>,
|
2020-05-05 22:27:06 +02:00
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub attributes_for_faceting: Option<Option<Vec<String>>>,
|
2020-01-08 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 17:45:29 +01:00
|
|
|
// Any value that is present is considered Some value, including null.
|
|
|
|
fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
|
|
|
|
where T: Deserialize<'de>,
|
|
|
|
D: Deserializer<'de>
|
|
|
|
{
|
|
|
|
Deserialize::deserialize(deserializer).map(Some)
|
2020-01-08 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
impl Settings {
|
2020-06-26 22:09:34 +02:00
|
|
|
pub fn to_update(&self) -> Result<SettingsUpdate, RankingRuleConversionError> {
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = self.clone();
|
|
|
|
|
|
|
|
let ranking_rules = match settings.ranking_rules {
|
2020-06-26 22:09:34 +02:00
|
|
|
Some(Some(rules)) => UpdateState::Update(RankingRule::try_from_iter(rules.iter())?),
|
2020-01-28 17:45:29 +01:00
|
|
|
Some(None) => UpdateState::Clear,
|
|
|
|
None => UpdateState::Nothing,
|
2020-01-20 09:52:24 +01:00
|
|
|
};
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(SettingsUpdate {
|
2020-02-02 22:59:19 +01:00
|
|
|
ranking_rules,
|
2020-02-25 16:10:34 +01:00
|
|
|
distinct_attribute: settings.distinct_attribute.into(),
|
2020-03-09 18:40:49 +01:00
|
|
|
primary_key: UpdateState::Nothing,
|
2020-01-29 18:30:21 +01:00
|
|
|
searchable_attributes: settings.searchable_attributes.into(),
|
|
|
|
displayed_attributes: settings.displayed_attributes.into(),
|
2020-01-20 09:52:24 +01:00
|
|
|
stop_words: settings.stop_words.into(),
|
|
|
|
synonyms: settings.synonyms.into(),
|
2020-02-25 15:51:37 +01:00
|
|
|
accept_new_fields: settings.accept_new_fields.into(),
|
2020-05-05 22:27:06 +02:00
|
|
|
attributes_for_faceting: settings.attributes_for_faceting.into(),
|
2020-01-29 18:30:21 +01:00
|
|
|
})
|
2020-01-20 09:52:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 14:17:38 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum UpdateState<T> {
|
|
|
|
Update(T),
|
|
|
|
Clear,
|
|
|
|
Nothing,
|
|
|
|
}
|
|
|
|
|
2020-01-28 17:45:29 +01:00
|
|
|
impl <T> From<Option<Option<T>>> for UpdateState<T> {
|
|
|
|
fn from(opt: Option<Option<T>>) -> UpdateState<T> {
|
2020-01-08 14:17:38 +01:00
|
|
|
match opt {
|
2020-01-28 17:45:29 +01:00
|
|
|
Some(Some(t)) => UpdateState::Update(t),
|
|
|
|
Some(None) => UpdateState::Clear,
|
2020-01-08 14:17:38 +01:00
|
|
|
None => UpdateState::Nothing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RankingRuleConversionError;
|
|
|
|
|
|
|
|
impl std::fmt::Display for RankingRuleConversionError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(f, "impossible to convert into RankingRule")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:26:27 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum RankingRule {
|
|
|
|
Typo,
|
|
|
|
Words,
|
|
|
|
Proximity,
|
|
|
|
Attribute,
|
|
|
|
WordsPosition,
|
2020-01-31 11:45:57 +01:00
|
|
|
Exactness,
|
2020-01-14 17:26:27 +01:00
|
|
|
Asc(String),
|
2020-03-02 17:13:23 +01:00
|
|
|
Desc(String),
|
2020-01-14 17:26:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 18:24:19 +01:00
|
|
|
impl std::fmt::Display for RankingRule {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2020-01-16 19:19:44 +01:00
|
|
|
match self {
|
2020-02-26 18:49:17 +01:00
|
|
|
RankingRule::Typo => f.write_str("typo"),
|
|
|
|
RankingRule::Words => f.write_str("words"),
|
|
|
|
RankingRule::Proximity => f.write_str("proximity"),
|
|
|
|
RankingRule::Attribute => f.write_str("attribute"),
|
|
|
|
RankingRule::WordsPosition => f.write_str("wordsPosition"),
|
|
|
|
RankingRule::Exactness => f.write_str("exactness"),
|
2020-02-26 18:24:19 +01:00
|
|
|
RankingRule::Asc(field) => write!(f, "asc({})", field),
|
2020-03-02 17:13:23 +01:00
|
|
|
RankingRule::Desc(field) => write!(f, "desc({})", field),
|
2020-01-16 19:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for RankingRule {
|
|
|
|
type Err = RankingRuleConversionError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let rule = match s {
|
2020-02-26 18:32:15 +01:00
|
|
|
"typo" => RankingRule::Typo,
|
|
|
|
"words" => RankingRule::Words,
|
|
|
|
"proximity" => RankingRule::Proximity,
|
|
|
|
"attribute" => RankingRule::Attribute,
|
2020-02-27 14:31:08 +01:00
|
|
|
"wordsPosition" => RankingRule::WordsPosition,
|
2020-02-26 18:32:15 +01:00
|
|
|
"exactness" => RankingRule::Exactness,
|
2020-01-16 19:19:44 +01:00
|
|
|
_ => {
|
2020-01-29 18:30:21 +01:00
|
|
|
let captures = RANKING_RULE_REGEX.captures(s).ok_or(RankingRuleConversionError)?;
|
|
|
|
match (captures.get(1).map(|m| m.as_str()), captures.get(2)) {
|
|
|
|
(Some("asc"), Some(field)) => RankingRule::Asc(field.as_str().to_string()),
|
2020-03-02 17:13:23 +01:00
|
|
|
(Some("desc"), Some(field)) => RankingRule::Desc(field.as_str().to_string()),
|
2020-01-16 19:19:44 +01:00
|
|
|
_ => return Err(RankingRuleConversionError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
impl RankingRule {
|
2020-02-11 15:16:02 +01:00
|
|
|
pub fn field(&self) -> Option<&str> {
|
2020-01-20 09:52:24 +01:00
|
|
|
match self {
|
2020-03-02 17:13:23 +01:00
|
|
|
RankingRule::Asc(field) | RankingRule::Desc(field) => Some(field),
|
2020-01-20 09:52:24 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:09:34 +02:00
|
|
|
pub fn try_from_iter(rules: impl IntoIterator<Item = impl AsRef<str>>) -> Result<Vec<RankingRule>, RankingRuleConversionError> {
|
2020-02-11 15:16:02 +01:00
|
|
|
rules.into_iter()
|
|
|
|
.map(|s| RankingRule::from_str(s.as_ref()))
|
2020-01-20 09:52:24 +01:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 14:17:38 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SettingsUpdate {
|
2020-01-14 17:26:27 +01:00
|
|
|
pub ranking_rules: UpdateState<Vec<RankingRule>>,
|
2020-02-25 16:10:34 +01:00
|
|
|
pub distinct_attribute: UpdateState<String>,
|
2020-03-09 18:40:49 +01:00
|
|
|
pub primary_key: UpdateState<String>,
|
2020-01-29 18:30:21 +01:00
|
|
|
pub searchable_attributes: UpdateState<Vec<String>>,
|
|
|
|
pub displayed_attributes: UpdateState<HashSet<String>>,
|
2020-01-08 14:17:38 +01:00
|
|
|
pub stop_words: UpdateState<BTreeSet<String>>,
|
|
|
|
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
|
2020-02-25 15:51:37 +01:00
|
|
|
pub accept_new_fields: UpdateState<bool>,
|
2020-05-05 22:27:06 +02:00
|
|
|
pub attributes_for_faceting: UpdateState<Vec<String>>,
|
2020-01-08 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SettingsUpdate {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
ranking_rules: UpdateState::Nothing,
|
2020-02-25 16:10:34 +01:00
|
|
|
distinct_attribute: UpdateState::Nothing,
|
2020-03-09 18:40:49 +01:00
|
|
|
primary_key: UpdateState::Nothing,
|
2020-01-29 18:30:21 +01:00
|
|
|
searchable_attributes: UpdateState::Nothing,
|
|
|
|
displayed_attributes: UpdateState::Nothing,
|
2020-01-08 14:17:38 +01:00
|
|
|
stop_words: UpdateState::Nothing,
|
|
|
|
synonyms: UpdateState::Nothing,
|
2020-02-25 15:51:37 +01:00
|
|
|
accept_new_fields: UpdateState::Nothing,
|
2020-05-05 22:27:06 +02:00
|
|
|
attributes_for_faceting: UpdateState::Nothing,
|
2020-01-08 14:17:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|