diff --git a/crates/jlabel-question/src/fallback/regex.rs b/crates/jlabel-question/src/fallback.rs similarity index 58% rename from crates/jlabel-question/src/fallback/regex.rs rename to crates/jlabel-question/src/fallback.rs index 91aa514..1843f6b 100644 --- a/crates/jlabel-question/src/fallback/regex.rs +++ b/crates/jlabel-question/src/fallback.rs @@ -4,26 +4,6 @@ use regex_syntax::hir::{Dot, Hir, Repetition}; use crate::{ParseError, QuestionMatcher}; -#[derive(Debug, Clone)] -pub enum RegexFallback { - Ok(T), - Regex(RegexQuestion), -} - -impl QuestionMatcher for RegexFallback { - fn parse(patterns: &[&str]) -> Result { - T::parse(patterns) - .map(Self::Ok) - .or_else(|_| RegexQuestion::parse(patterns).map(Self::Regex)) - } - fn test(&self, label: &Label) -> bool { - match &self { - Self::Ok(inner) => inner.test(label), - Self::Regex(regex) => regex.test(label), - } - } -} - #[derive(Debug, Clone)] pub struct RegexQuestion(Regex); @@ -65,42 +45,22 @@ impl QuestionMatcher for RegexQuestion { #[cfg(test)] mod tests { - use std::str::FromStr; - - use jlabel::Label; - - use crate::{fallback::regex::RegexFallback, AllQuestion, QuestionMatcher}; - use super::RegexQuestion; - const TEST_LABEL:&str="sil^k-o+N=n/A:-4+1+5/B:xx-xx_xx/C:09_xx+xx/D:xx+xx_xx/E:xx_xx!xx_xx-xx/F:5_5#0_xx@1_1|1_5/G:xx_xx%xx_xx_xx/H:xx_xx/I:1-5@1+1&1-1|1+5/J:xx_xx/K:1+1-5"; - - #[test] - fn ok() { - let label = Label::from_str(TEST_LABEL).unwrap(); - let parsed = RegexFallback::::parse(&["*-o+*", "*-N+*"]).unwrap(); - assert!(matches!(&parsed, RegexFallback::Ok(_))); - assert!(parsed.test(&label)); - } #[test] fn regex() { - let label = Label::from_str(TEST_LABEL).unwrap(); + const TEST_LABEL:&str="sil^k-o+N=n/A:-4+1+5/B:xx-xx_xx/C:09_xx+xx/D:xx+xx_xx/E:xx_xx!xx_xx-xx/F:5_5#0_xx@1_1|1_5/G:xx_xx%xx_xx_xx/H:xx_xx/I:1-5@1+1&1-1|1+5/J:xx_xx/K:1+1-5"; - assert!(matches!( - RegexFallback::::parse(&["*^k-o+*"]), - Ok(RegexFallback::Regex(_)) - )); + use crate::QuestionMatcher; + use jlabel::Label; + use std::str::FromStr; + + let label = Label::from_str(TEST_LABEL).unwrap(); - assert!(RegexFallback::::parse(&["*^k-o+*"]) - .unwrap() - .test(&label)); - assert!(!RegexFallback::::parse(&["INVALID?*"]) - .unwrap() - .test(&label)); + assert!(RegexQuestion::parse(&["*^k-o+*"]).unwrap().test(&label)); + assert!(!RegexQuestion::parse(&["INVALID?*"]).unwrap().test(&label)); - assert!(!RegexFallback::::parse(&["^k-o+*"]) - .unwrap() - .test(&label)); + assert!(!RegexQuestion::parse(&["^k-o+*"]).unwrap().test(&label)); } #[test] fn wildcard() { diff --git a/crates/jlabel-question/src/fallback/mod.rs b/crates/jlabel-question/src/fallback/mod.rs deleted file mode 100644 index aa2f3be..0000000 --- a/crates/jlabel-question/src/fallback/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod noop; - -#[cfg(feature = "regex")] -pub mod regex; diff --git a/crates/jlabel-question/src/fallback/noop.rs b/crates/jlabel-question/src/fallback/noop.rs deleted file mode 100644 index ea7dd8f..0000000 --- a/crates/jlabel-question/src/fallback/noop.rs +++ /dev/null @@ -1,56 +0,0 @@ -use jlabel::Label; - -use crate::{ParseError, QuestionMatcher}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum NoOpFallback { - Ok(T), - NoOp, -} - -impl QuestionMatcher for NoOpFallback { - fn parse(patterns: &[&str]) -> Result { - T::parse(patterns).map(Self::Ok).or(Ok(Self::NoOp)) - } - fn test(&self, label: &Label) -> bool { - match &self { - Self::Ok(inner) => inner.test(label), - Self::NoOp => false, - } - } -} - -#[cfg(test)] -mod tests { - use std::str::FromStr; - - use jlabel::Label; - - use crate::{ - fallback::noop::NoOpFallback, position::PhonePosition, AllQuestion, Question, - QuestionMatcher, - }; - - const TEST_LABEL:&str="sil^k-o+N=n/A:-4+1+5/B:xx-xx_xx/C:09_xx+xx/D:xx+xx_xx/E:xx_xx!xx_xx-xx/F:5_5#0_xx@1_1|1_5/G:xx_xx%xx_xx_xx/H:xx_xx/I:1-5@1+1&1-1|1+5/J:xx_xx/K:1+1-5"; - - #[test] - fn ok() { - let label = Label::from_str(TEST_LABEL).unwrap(); - let parsed = NoOpFallback::::parse(&["*-o+*", "*-N+*"]).unwrap(); - assert_eq!( - &parsed, - &NoOpFallback::Ok(AllQuestion::Phone(Question { - position: PhonePosition::P3, - range: Some(vec!["o".to_string(), "N".to_string()]) - })) - ); - assert!(parsed.test(&label)); - } - #[test] - fn noop() { - let label = Label::from_str(TEST_LABEL).unwrap(); - let parsed = NoOpFallback::::parse(&["INVALID?*"]).unwrap(); - assert_eq!(parsed, NoOpFallback::NoOp); - assert!(!parsed.test(&label)); - } -} diff --git a/crates/jlabel-question/src/lib.rs b/crates/jlabel-question/src/lib.rs index cc6abc3..44334af 100644 --- a/crates/jlabel-question/src/lib.rs +++ b/crates/jlabel-question/src/lib.rs @@ -1,6 +1,8 @@ -pub mod fallback; pub mod position; +#[cfg(feature = "regex")] +pub mod fallback; + use std::num::ParseIntError; use position::{