From 6fb1c287e014852b05b9fda92984a0772643b372 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 4 Apr 2020 17:08:26 +0200 Subject: [PATCH] Rules are now kept sorted Changed the rules from a HashMap to a Vec --- src/settings/mod.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 577e64a..5814dd6 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -125,7 +125,7 @@ pub struct Settings { /// a regex to be matched while the second represents the change /// to be made to the input data. #[serde(default)] - pub rules: HashMap, + pub rules: Vec<(Regex, String)>, } /// Representation inside the crate of LangEvolve’s settings. @@ -139,8 +139,8 @@ impl Settings { /// let content_yaml = r#"--- /// version: "1" /// categories: {} - /// rules: {}"#; - /// let content_json = r#"{"version":"1","categories":{},"rules":{}}"#; + /// rules: []"#; + /// let content_json = r#"{"version":"1","categories":{},"rules":[]}"#; /// assert_eq!(content_yaml, serde_yaml::to_string(&s).unwrap()); /// assert_eq!(content_json, serde_json::to_string(&s).unwrap()); /// ``` @@ -150,7 +150,7 @@ impl Settings { Self { version: Self::get_ruleset_version(), categories: HashMap::new(), - rules: HashMap::new(), + rules: Vec::new(), } } @@ -285,10 +285,7 @@ impl Settings { } fn update_rules(&self) -> Vec<(Regex, String)> { - let mut rules: Vec<(Regex, String)> = Vec::new(); - for (from, to) in &self.rules { - rules.push((from.clone(), to.to_string())); - } + let mut rules = self.rules.clone(); for (category, content) in &self.categories { let mut temp_rules: Vec<(Regex, String)> = Vec::new(); @@ -368,7 +365,7 @@ impl FromStr for Settings { /// /// ``` /// # 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(); /// ``` fn from_str(s: &str) -> Result { @@ -410,7 +407,7 @@ fn write_settings() { let settings = r#"--- version: "1" categories: {} -rules: {}"#; +rules: []"#; utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap(); assert_eq!(settings, utils::read_file(&path).unwrap()); }