Compare commits
2 Commits
6fb1c287e0
...
dbbb1616dd
| Author | SHA1 | Date | |
|---|---|---|---|
|
dbbb1616dd
|
|||
|
23e3acb182
|
@@ -284,30 +284,36 @@ impl Settings {
|
|||||||
RULESET_CURRENT_VERSION.to_string()
|
RULESET_CURRENT_VERSION.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_rules(&self) -> Vec<(Regex, String)> {
|
fn update_rules(&self) -> std::result::Result<Vec<(Regex, String)>, String> {
|
||||||
let mut rules = self.rules.clone();
|
let mut rules = self.rules.clone();
|
||||||
|
|
||||||
|
// TODO break categories in different rules
|
||||||
|
for (from, to) in rules.iter_mut() {
|
||||||
|
let re = Regex::new("%\\D");
|
||||||
|
let from_match = re.is_match(from.as_str());
|
||||||
|
let to_match = re.is_match(to);
|
||||||
|
if from_match || to_match {
|
||||||
for (category, content) in &self.categories {
|
for (category, content) in &self.categories {
|
||||||
let mut temp_rules: Vec<(Regex, String)> = Vec::new();
|
if from_match {
|
||||||
for (from, to) in &rules {
|
*from = Regex::new(
|
||||||
let from = Regex::new(
|
|
||||||
from.to_string()
|
from.to_string()
|
||||||
.replace(
|
.replace(
|
||||||
category.as_str(),
|
format!("%{}", category).as_str(),
|
||||||
format!("[{}]", content).as_str(),
|
format!("[{}]", content).as_str(),
|
||||||
)
|
)
|
||||||
.replace("%", "")
|
.as_str()
|
||||||
.as_str(),
|
|
||||||
);
|
);
|
||||||
let to = to.to_string().replace(
|
}
|
||||||
category,
|
if to_match {
|
||||||
format!("[{}]", content).replace("%", "").as_str(),
|
*to = to.to_string().replace(
|
||||||
|
format!("%{}", category).as_str(),
|
||||||
|
format!("[{}]", content).as_str()
|
||||||
);
|
);
|
||||||
temp_rules.push((from, to));
|
|
||||||
}
|
}
|
||||||
rules = temp_rules.clone();
|
|
||||||
}
|
}
|
||||||
rules
|
}
|
||||||
|
}
|
||||||
|
Ok(rules)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply list of rules to input
|
/// Apply list of rules to input
|
||||||
@@ -334,9 +340,9 @@ impl Settings {
|
|||||||
/// // set some input
|
/// // set some input
|
||||||
/// let _output = settings.apply(input);
|
/// let _output = settings.apply(input);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn apply(&self, s: String) -> String {
|
pub fn apply(&self, s: String) -> std::result::Result<String, String> {
|
||||||
// Replace all `%C`s by their equivalent
|
// TODO Add Error handling
|
||||||
let rules = self.update_rules();
|
let rules = self.update_rules().unwrap();
|
||||||
let mut s = s.clone();
|
let mut s = s.clone();
|
||||||
debug!("===============================================");
|
debug!("===============================================");
|
||||||
for (from, to) in rules {
|
for (from, to) in rules {
|
||||||
@@ -345,7 +351,7 @@ impl Settings {
|
|||||||
s = from.replace_all(&s, to.as_str()).to_string();
|
s = from.replace_all(&s, to.as_str()).to_string();
|
||||||
debug!("new: {}", s);
|
debug!("new: {}", s);
|
||||||
}
|
}
|
||||||
s
|
Ok(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user