From 23e3acb1829aca8e5a0addb12483f5c8c6ae8c1f Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 4 Apr 2020 18:28:27 +0200 Subject: [PATCH] Better category find and replace I didn't have to do it in two `replace` calls Now I have to check if the rule has a or several corresponding categories in the initial and final regex, find whether they have the same amount of elements, and if so create rules that allow elements from each category to be mapped 1:1 --- src/settings/mod.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 5814dd6..c29f780 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -284,24 +284,26 @@ impl Settings { RULESET_CURRENT_VERSION.to_string() } + // TODO make the return type a Result fn update_rules(&self) -> Vec<(Regex, String)> { let mut rules = self.rules.clone(); + // TODO Optimize that shit + // TODO break categories in different rules for (category, content) in &self.categories { let mut temp_rules: Vec<(Regex, String)> = Vec::new(); for (from, to) in &rules { let from = Regex::new( from.to_string() .replace( - category.as_str(), + format!("%{}", category).as_str(), format!("[{}]", content).as_str(), ) - .replace("%", "") .as_str(), ); let to = to.to_string().replace( - category, - format!("[{}]", content).replace("%", "").as_str(), + format!("%{}", category).as_str(), + format!("[{}]", content).as_str(), ); temp_rules.push((from, to)); } @@ -334,8 +336,7 @@ impl Settings { /// // set some input /// let _output = settings.apply(input); /// ``` - pub fn apply(&self, s: String) -> String { - // Replace all `%C`s by their equivalent + pub fn apply(&self, s: String) -> std::result::Result { let rules = self.update_rules(); let mut s = s.clone(); debug!("==============================================="); @@ -345,7 +346,7 @@ impl Settings { s = from.replace_all(&s, to.as_str()).to_string(); debug!("new: {}", s); } - s + Ok(s) } }