diff --git a/src/settings/mod.rs b/src/settings/mod.rs index e045a44..cb9599f 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -206,15 +206,17 @@ impl Settings { } }, }; + if settings.version > Self::get_ruleset_version() { error!("Ruleset version too high!"); - return Err(std::io::Error::new( + Err(std::io::Error::new( std::io::ErrorKind::InvalidData, "Ruleset version too high!", - )); + )) + } else { + info!("Successfuly imported {}", path.display()); + Ok(settings) } - info!("Successfuly imported {}", path.display()); - Ok(settings) } /// Import settings from file path described by the argument `path` @@ -228,13 +230,13 @@ impl Settings { /// ```no_run /// let s = lang_evolve_core::settings::Settings::from("settings.yml"); /// ``` - pub fn from(s: S) -> Self + pub fn from(s: S) -> std::io::Result where S: ToString, { let s = s.to_string(); let path = std::path::Path::new(&s); - Self::import(&path).unwrap() + Self::import(&path) } /// Add a new rule to the current settings diff --git a/src/settings/rule/mod.rs b/src/settings/rule/mod.rs index 96a6926..f14fe36 100644 --- a/src/settings/rule/mod.rs +++ b/src/settings/rule/mod.rs @@ -1,14 +1,13 @@ use std::collections::HashMap; -use serde::{Deserialize, Serialize}; - use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; mod regex_wrapper; use regex_wrapper::Regex; lazy_static! { - static ref RE : Regex = Regex::new("%([A-Z])"); + static ref RE: Regex = Regex::new("%([A-Z])"); } /// Representation of a rule in LangEvolveRs @@ -45,15 +44,9 @@ impl Rule { } } - fn detect_categories(&self) -> (u8, u8) { - let captures_from = match RE.captures(self.from.as_str()) { - None => 0 as u8, - Some(c) => c.len() as u8, - }; - let captures_to = match RE.captures(&self.to) { - None => 0 as u8, - Some(c) => c.len() as u8, - }; + pub fn detect_number_categories(&self) -> (u8, u8) { + let captures_from = self.from.to_string().matches('%').count() as u8; + let captures_to = self.to.matches('%').count() as u8; (captures_from, captures_to) } @@ -67,7 +60,7 @@ impl Rule { format!("%{}", category).as_str(), format!("[{}]", content).as_str(), ) - .as_str() + .as_str(), ); } rule @@ -79,16 +72,14 @@ impl Rule { categories: &HashMap, ) -> std::result::Result { let mut rule = self.clone(); - let (from_match, to_match) = self.detect_categories(); + let (from_match, to_match) = self.detect_number_categories(); // If there are only simple rewrites to make in the from String if from_match > 0 && to_match == 0 { rule = self.simple_rewrite(&categories); } // If there are equivalences between from and to - if from_match > 0 && to_match <= from_match && to_match > 0 { - - } + if from_match > 0 && to_match <= from_match && to_match > 0 {} Ok(rule) } @@ -105,10 +96,7 @@ impl Rule { impl From for Rule { fn from(source: String) -> Self { let components: Vec<&str> = source.split_terminator('>').collect(); - Self { - from: Regex::new(components[0]), - to: String::from(components[1]), - } + Rule::new(components[0], components[1]) } }