Added documentation and tests
This commit is contained in:
parent
d33190a46d
commit
cb933d4896
@ -227,8 +227,12 @@ impl Settings {
|
|||||||
///
|
///
|
||||||
/// # Example
|
/// # 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: S) -> std::io::Result<Self>
|
pub fn from<S>(s: S) -> std::io::Result<Self>
|
||||||
where
|
where
|
||||||
@ -245,6 +249,20 @@ impl Settings {
|
|||||||
///
|
///
|
||||||
/// * `from` - Regex that should match the text to be replaced
|
/// * `from` - Regex that should match the text to be replaced
|
||||||
/// * `to` - Regex that should replace some text
|
/// * `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) {
|
pub fn add_rule(&mut self, from: &str, to: &str) {
|
||||||
self.rules.push(Rule::new(from, to))
|
self.rules.push(Rule::new(from, to))
|
||||||
}
|
}
|
||||||
@ -255,6 +273,20 @@ impl Settings {
|
|||||||
///
|
///
|
||||||
/// * `name` - Name of the category
|
/// * `name` - Name of the category
|
||||||
/// * `content` - Content of the category, phonemes
|
/// * `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) {
|
pub fn add_category(&mut self, name: &str, content: &str) {
|
||||||
self.categories.insert(String::from(name), String::from(content));
|
self.categories.insert(String::from(name), String::from(content));
|
||||||
}
|
}
|
||||||
@ -309,6 +341,7 @@ impl Settings {
|
|||||||
RULESET_CURRENT_VERSION.to_string()
|
RULESET_CURRENT_VERSION.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Transform input rules into Regexes that can be understood by Rust.
|
||||||
fn update_rules(&self) -> std::result::Result<Vec<Rule>, String> {
|
fn update_rules(&self) -> std::result::Result<Vec<Rule>, String> {
|
||||||
let rules = self.rules.clone();
|
let rules = self.rules.clone();
|
||||||
let rules: Vec<Rule> = rules
|
let rules: Vec<Rule> = rules
|
||||||
|
@ -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) {
|
pub fn detect_number_categories(&self) -> (u8, u8) {
|
||||||
let captures_from = self.from.to_string().matches('%').count() as u8;
|
let captures_from = self.from.to_string().matches('%').count() as u8;
|
||||||
let captures_to = self.to.matches('%').count() as u8;
|
let captures_to = self.to.matches('%').count() as u8;
|
||||||
@ -94,6 +110,22 @@ impl Rule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Rule {
|
impl From<String> 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 {
|
fn from(source: String) -> Self {
|
||||||
let components: Vec<&str> = source.split_terminator('>').collect();
|
let components: Vec<&str> = source.split_terminator('>').collect();
|
||||||
Rule::new(components[0], components[1])
|
Rule::new(components[0], components[1])
|
||||||
@ -101,6 +133,21 @@ impl From<String> for Rule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq 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 {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.from == other.from && self.to == other.to
|
self.from == other.from && self.to == other.to
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user