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
/// to be made to the input data.
#[serde(default)]
pub rules: HashMap<Regex, String>,
pub rules: Vec<(Regex, String)>,
}
/// Representation inside the crate of LangEvolves 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<Self, Self::Err> {
@ -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());
}