From c48316e20fc499e6c0b2056e777e6d4ab9c74b5f Mon Sep 17 00:00:00 2001 From: Lewis Wynne Date: Tue, 11 Mar 2025 15:54:15 +0000 Subject: [PATCH] defs for user defined inflections --- src/inflect_rs.rs | 84 +++++++++++++++++++++++++++++++++++++-- tests/inflect_rs_tests.rs | 2 +- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/inflect_rs.rs b/src/inflect_rs.rs index 13fba3c..e156a6a 100644 --- a/src/inflect_rs.rs +++ b/src/inflect_rs.rs @@ -2518,7 +2518,8 @@ impl Words { } // FIXME: This is terrible. Placeholder. -pub struct Word(pub String); +#[derive(Debug, Clone)] +pub struct Word(String); impl Word { pub fn new(word: String) -> Result { @@ -2563,8 +2564,85 @@ impl Engine { } } - fn checkpat(self, _pattern: Option) { - return; + fn _number_args(&self) -> Option<&HashMap> { + self._number_args.as_ref() + } + + fn set_number_args(&mut self, args: Option>) { + self._number_args = args; + } + + fn defnoun(&mut self, singular: &Option, plural: &Option) { + self.checkpat(singular); + self.checkpatplural(plural); + self.pl_sb_user_defined + .extend(vec![singular.clone(), plural.clone()]); + self.si_sb_user_defined + .extend(vec![plural.clone(), singular.clone()]); + } + + fn defverb( + &mut self, + singular_1st: &Option, + singular_2nd: &Option, + singular_3rd: &Option, + plural_1st: &Option, + plural_2nd: &Option, + plural_3rd: &Option, + ) { + self.checkpat(singular_1st); + self.checkpat(singular_2nd); + self.checkpat(singular_3rd); + self.checkpat(plural_1st); + self.checkpat(plural_2nd); + self.checkpat(plural_3rd); + self.pl_v_user_defined.extend(vec![ + singular_1st.clone(), + singular_2nd.clone(), + singular_3rd.clone(), + plural_1st.clone(), + plural_2nd.clone(), + plural_3rd.clone(), + ]); + } + + fn defadj(&mut self, singular: &Option, plural: &Option) { + self.checkpat(singular); + self.checkpatplural(plural); + self.pl_adj_user_defined + .extend(vec![singular.clone(), plural.clone()]); + } + + fn defa(&mut self, pattern: &Option) { + self.checkpat(pattern); + self.a_a_user_defined.extend(vec![ + pattern.clone(), + Some(Word::new(String::from("a")).expect("Failed to make Word")), + ]); + } + + fn defan(&mut self, pattern: &Option) { + self.checkpat(pattern); + self.a_a_user_defined.extend(vec![ + pattern.clone(), + Some(Word::new(String::from("an")).expect("Failed to make Word")), + ]); + } + + fn checkpat(&self, pattern: &Option) { + if pattern.is_none() { + return; + } + + if pattern.is_some() { + let word = pattern.clone().expect("Failed to unwrap Word"); + let _re = Regex::new(word.get()).expect("Failed to compile regex"); + } + } + + // TODO: Check for replace pattern. + fn checkpatplural(&self, pattern: &Option) { + self.checkpat(pattern); } pub fn gender(&mut self, gender: &str) { diff --git a/tests/inflect_rs_tests.rs b/tests/inflect_rs_tests.rs index 80a8bb1..ade0a05 100644 --- a/tests/inflect_rs_tests.rs +++ b/tests/inflect_rs_tests.rs @@ -85,7 +85,7 @@ fn test_words() { #[test] fn test_word() { let word = Word::new(String::from("fox")); - assert_eq!(word.unwrap().get(), "fox"); + assert_eq!(word.expect("Failed to unwrap Word").get(), "fox"); } #[test]