diff --git a/src/settings/mod.rs b/src/settings/mod.rs index cb9599f..441d571 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -227,8 +227,12 @@ impl Settings { /// /// # Example /// - /// ```no_run - /// let s = lang_evolve_core::settings::Settings::from("settings.yml"); + /// ``` + /// # use lang_evolve_core::settings::Settings; + /// # use std::path::Path; + /// # let s = Settings::default(); + /// # s.export(Path::new("settings.yml")); + /// let s = Settings::from("settings.yml"); /// ``` pub fn from(s: S) -> std::io::Result where @@ -245,6 +249,20 @@ impl Settings { /// /// * `from` - Regex that should match the text to be replaced /// * `to` - Regex that should replace some text + /// + /// # Example + /// + /// ``` + /// # use lang_evolve_core::settings::Settings; + /// let mut settings = Settings::default(); + /// settings.add_rule("ha", "wa"); + /// + /// use std::str::FromStr; + /// let reference = Settings::from_str( + /// r#"{"version":"1","categories":{},"rules":[{"from":"ha","to":"wa"}]}"#) + /// .unwrap(); + /// assert_eq!(reference, settings); + /// ``` pub fn add_rule(&mut self, from: &str, to: &str) { self.rules.push(Rule::new(from, to)) } @@ -255,6 +273,20 @@ impl Settings { /// /// * `name` - Name of the category /// * `content` - Content of the category, phonemes + /// + /// # Example + /// + /// ``` + /// # use lang_evolve_core::settings::Settings; + /// let mut settings = Settings::default(); + /// settings.add_category("C", "abcde"); + /// + /// use std::str::FromStr; + /// let reference = Settings::from_str( + /// r#"{"version":"1","categories":{"C": "abcde"},"rules":[]}"#) + /// .unwrap(); + /// assert_eq!(reference, settings); + /// ``` pub fn add_category(&mut self, name: &str, content: &str) { self.categories.insert(String::from(name), String::from(content)); } @@ -309,6 +341,7 @@ impl Settings { RULESET_CURRENT_VERSION.to_string() } + /// Transform input rules into Regexes that can be understood by Rust. fn update_rules(&self) -> std::result::Result, String> { let rules = self.rules.clone(); let rules: Vec = rules diff --git a/src/settings/rule/mod.rs b/src/settings/rule/mod.rs index f14fe36..bbc9883 100644 --- a/src/settings/rule/mod.rs +++ b/src/settings/rule/mod.rs @@ -44,6 +44,22 @@ impl Rule { } } + /// Detect the number of categories in a rule + /// + /// For a rule, this function detects the number of categories set in the + /// `from` member of a `Rule` and in its `to` member. The result is returned + /// as a tuple of `u8`s. + /// + /// # Example + /// + /// ``` + /// # #[path = "mod.rs"] + /// # mod rule; + /// # use rule::Rule; + /// let rule = Rule::new("%Bea*i+", "a%A%C"); + /// let nb_rules = rule.detect_number_categories(); + /// assert_eq!((1 as u8, 2 as u8), nb_rules); + /// ``` 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; @@ -94,6 +110,22 @@ impl Rule { } impl From for Rule { + /// Allow to create a rule from a single `String` + /// + /// It is possible to create a rule from a string, delimited by a `>`. This + /// means a rule like `%C>%D` will be interpreted as going from `%C` to + /// `%D`. + /// + /// # Example + /// + /// ``` + /// # #[path = "mod.rs"] + /// # mod rule; + /// # use rule::Rule; + /// let rule1 = Rule::new("%C", "%D"); + /// let rule2 = Rule::from(String::from("%C>%D")); + /// assert_eq!(rule1, rule2); + /// ``` fn from(source: String) -> Self { let components: Vec<&str> = source.split_terminator('>').collect(); Rule::new(components[0], components[1]) @@ -101,6 +133,21 @@ impl From for Rule { } impl PartialEq for Rule { + /// Equality between `Rule` structs + /// + /// This allows for equality comparison between two `Rule` structs. + /// + /// # Example + /// + /// ``` + /// # #[path = "mod.rs"] + /// # mod rule; + /// use rule::Rule; + /// let rule1 = Rule::new("%C", "%D"); + /// let rule2 = Rule::from("%C>%D"); + /// assert!(rule1 == rule2); + /// assert!(rule2 == rule1); + /// ``` fn eq(&self, other: &Self) -> bool { self.from == other.from && self.to == other.to }