Rules are now kept sorted

Changed the rules from a HashMap to a Vec
This commit is contained in:
Lucien Cartier-Tilet 2020-04-04 17:08:26 +02:00
parent ef8c02fc97
commit 6fb1c287e0
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -125,7 +125,7 @@ pub struct Settings {
/// a regex to be matched while the second represents the change /// a regex to be matched while the second represents the change
/// to be made to the input data. /// to be made to the input data.
#[serde(default)] #[serde(default)]
pub rules: HashMap<Regex, String>, pub rules: Vec<(Regex, String)>,
} }
/// Representation inside the crate of LangEvolves settings. /// Representation inside the crate of LangEvolves settings.
@ -139,8 +139,8 @@ impl Settings {
/// let content_yaml = r#"--- /// let content_yaml = r#"---
/// version: "1" /// version: "1"
/// categories: {} /// categories: {}
/// rules: {}"#; /// rules: []"#;
/// let content_json = r#"{"version":"1","categories":{},"rules":{}}"#; /// let content_json = r#"{"version":"1","categories":{},"rules":[]}"#;
/// assert_eq!(content_yaml, serde_yaml::to_string(&s).unwrap()); /// assert_eq!(content_yaml, serde_yaml::to_string(&s).unwrap());
/// assert_eq!(content_json, serde_json::to_string(&s).unwrap()); /// assert_eq!(content_json, serde_json::to_string(&s).unwrap());
/// ``` /// ```
@ -150,7 +150,7 @@ impl Settings {
Self { Self {
version: Self::get_ruleset_version(), version: Self::get_ruleset_version(),
categories: HashMap::new(), categories: HashMap::new(),
rules: HashMap::new(), rules: Vec::new(),
} }
} }
@ -285,10 +285,7 @@ impl Settings {
} }
fn update_rules(&self) -> Vec<(Regex, String)> { fn update_rules(&self) -> Vec<(Regex, String)> {
let mut rules: Vec<(Regex, String)> = Vec::new(); let mut rules = self.rules.clone();
for (from, to) in &self.rules {
rules.push((from.clone(), to.to_string()));
}
for (category, content) in &self.categories { for (category, content) in &self.categories {
let mut temp_rules: Vec<(Regex, String)> = Vec::new(); let mut temp_rules: Vec<(Regex, String)> = Vec::new();
@ -368,7 +365,7 @@ impl FromStr for Settings {
/// ///
/// ``` /// ```
/// # use std::str::FromStr; /// # use std::str::FromStr;
/// let s = r#"{"version":"1","categories":{},"rules":{}}"#; /// let s = r#"{"version":"1","categories":{},"rules":[]}"#;
/// let settings = lang_evolve_core::settings::Settings::from_str(s).unwrap(); /// let settings = lang_evolve_core::settings::Settings::from_str(s).unwrap();
/// ``` /// ```
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -410,7 +407,7 @@ fn write_settings() {
let settings = r#"--- let settings = r#"---
version: "1" version: "1"
categories: {} categories: {}
rules: {}"#; rules: []"#;
utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap(); utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap();
assert_eq!(settings, utils::read_file(&path).unwrap()); assert_eq!(settings, utils::read_file(&path).unwrap());
} }