use std::sync::Mutex; use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::str::FromStr; use serde::{Deserialize, Deserializer, Serialize}; use once_cell::sync::Lazy; static RANKING_RULE_REGEX: Lazy> = Lazy::new(|| { let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap(); Mutex::new(regex) }); #[derive(Default, Clone, Serialize, Deserialize)] pub struct Settings { pub ranking_rules: Option>, pub ranking_distinct: Option, pub attribute_identifier: Option, pub attributes_searchable: Option>, pub attributes_displayed: Option>, pub stop_words: Option>, pub synonyms: Option>>, } impl Into for Settings { fn into(self) -> SettingsUpdate { let settings = self.clone(); let ranking_rules = match settings.ranking_rules { Some(rules) => Some(RankingRule::from_vec(rules)), None => None }; SettingsUpdate { ranking_rules: ranking_rules.into(), ranking_distinct: settings.ranking_distinct.into(), attribute_identifier: settings.attribute_identifier.into(), attributes_searchable: settings.attributes_searchable.into(), attributes_displayed: settings.attributes_displayed.into(), stop_words: settings.stop_words.into(), synonyms: settings.synonyms.into(), } } } #[derive(Default, Clone, Serialize, Deserialize)] pub struct SettingsComplete { #[serde(default, deserialize_with = "deserialize_some")] pub ranking_rules: Option>>, #[serde(default, deserialize_with = "deserialize_some")] pub ranking_distinct: Option>, #[serde(default, deserialize_with = "deserialize_some")] pub attribute_identifier: Option>, #[serde(default, deserialize_with = "deserialize_some")] pub attributes_searchable: Option>>, #[serde(default, deserialize_with = "deserialize_some")] pub attributes_displayed: Option>>, #[serde(default, deserialize_with = "deserialize_some")] pub stop_words: Option>>, #[serde(default, deserialize_with = "deserialize_some")] pub synonyms: Option>>>, } impl Into for SettingsComplete { fn into(self) -> SettingsUpdate { let settings = self.clone(); let ranking_rules = match settings.ranking_rules { Some(Some(rules)) => Some(Some(RankingRule::from_vec(rules))), Some(None) => Some(None), None => None, }; SettingsUpdate { ranking_rules: ranking_rules.into(), ranking_distinct: settings.ranking_distinct.into(), attribute_identifier: settings.attribute_identifier.into(), attributes_searchable: settings.attributes_searchable.into(), attributes_displayed: settings.attributes_displayed.into(), stop_words: settings.stop_words.into(), synonyms: settings.synonyms.into(), } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum UpdateState { Update(T), Add(T), Delete(T), Clear, Nothing, } impl From> for UpdateState { fn from(opt: Option) -> UpdateState { match opt { Some(t) => UpdateState::Update(t), None => UpdateState::Nothing, } } } impl From>> for UpdateState { fn from(opt: Option>) -> UpdateState { match opt { Some(Some(t)) => UpdateState::Update(t), Some(None) => UpdateState::Clear, None => UpdateState::Nothing, } } } impl UpdateState { pub fn is_changed(&self) -> bool { match self { UpdateState::Nothing => false, _ => true, } } } #[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") } } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum RankingRule { Typo, Words, Proximity, Attribute, WordsPosition, Exact, Asc(String), Dsc(String), } impl ToString for RankingRule { fn to_string(&self) -> String { match self { RankingRule::Typo => "_typo".to_string(), RankingRule::Words => "_words".to_string(), RankingRule::Proximity => "_proximity".to_string(), RankingRule::Attribute => "_attribute".to_string(), RankingRule::WordsPosition => "_words_position".to_string(), RankingRule::Exact => "_exact".to_string(), RankingRule::Asc(field) => format!("asc({})", field), RankingRule::Dsc(field) => format!("dsc({})", field), } } } impl FromStr for RankingRule { type Err = RankingRuleConversionError; fn from_str(s: &str) -> Result { let rule = match s { "_typo" => RankingRule::Typo, "_words" => RankingRule::Words, "_proximity" => RankingRule::Proximity, "_attribute" => RankingRule::Attribute, "_words_position" => RankingRule::WordsPosition, "_exact" => RankingRule::Exact, _ => { let captures = RANKING_RULE_REGEX.lock().unwrap().captures(s).unwrap(); match captures[0].as_ref() { "asc" => RankingRule::Asc(captures[1].to_string()), "dsc" => RankingRule::Dsc(captures[1].to_string()), _ => return Err(RankingRuleConversionError) } } }; Ok(rule) } } impl RankingRule { pub fn get_field(&self) -> Option { match self { RankingRule::Asc(field) | RankingRule::Dsc(field) => Some((*field).clone()), _ => None, } } pub fn from_vec(rules: Vec) -> Vec { rules.iter() .map(|s| RankingRule::from_str(s.as_str())) .filter_map(Result::ok) .collect() } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SettingsUpdate { pub ranking_rules: UpdateState>, pub ranking_distinct: UpdateState, pub attribute_identifier: UpdateState, pub attributes_searchable: UpdateState>, pub attributes_displayed: UpdateState>, pub stop_words: UpdateState>, pub synonyms: UpdateState>>, } impl Default for SettingsUpdate { fn default() -> Self { Self { ranking_rules: UpdateState::Nothing, ranking_distinct: UpdateState::Nothing, attribute_identifier: UpdateState::Nothing, attributes_searchable: UpdateState::Nothing, attributes_displayed: UpdateState::Nothing, stop_words: UpdateState::Nothing, synonyms: UpdateState::Nothing, } } } // Any value that is present is considered Some value, including null. fn deserialize_some<'de, T, D>(deserializer: D) -> Result, D::Error> where T: Deserialize<'de>, D: Deserializer<'de> { Deserialize::deserialize(deserializer).map(Some) }