From 1269db042b5c3169eb512504cd5af1405a04ddfd Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Mon, 9 Dec 2019 09:30:45 -0800 Subject: [PATCH] Allow for more flexible regex matching. --- src/mappers.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/mappers.rs b/src/mappers.rs index cf95b15..e197c89 100644 --- a/src/mappers.rs +++ b/src/mappers.rs @@ -65,9 +65,33 @@ where } } -pub fn matches(value: &str) -> Matches { - let regex = regex::bytes::Regex::new(value).expect("failed to create regex"); - Matches(regex) +pub trait IntoRegex { + fn into_regex(self) -> regex::bytes::Regex; +} +impl IntoRegex for &str { + fn into_regex(self) -> regex::bytes::Regex { + regex::bytes::Regex::new(self).expect("failed to create regex") + } +} +impl IntoRegex for String { + fn into_regex(self) -> regex::bytes::Regex { + regex::bytes::Regex::new(&self).expect("failed to create regex") + } +} +impl IntoRegex for &mut regex::bytes::RegexBuilder { + fn into_regex(self) -> regex::bytes::Regex { + self.build().expect("failed to create regex") + } +} +impl IntoRegex for regex::bytes::Regex { + fn into_regex(self) -> regex::bytes::Regex { + self + } +} + +pub fn matches(value: impl IntoRegex) -> Matches { + //let regex = regex::bytes::Regex::new(value).expect("failed to create regex"); + Matches(value.into_regex()) } #[derive(Debug)] pub struct Matches(regex::bytes::Regex); @@ -250,11 +274,36 @@ mod tests { #[test] fn test_matches() { + // regex from str let mut c = matches(r#"^foo\d*bar$"#); assert_eq!(true, c.map("foobar")); assert_eq!(true, c.map("foo99bar")); assert_eq!(false, c.map("foo99barz")); assert_eq!(false, c.map("bat")); + + // regex from String + let mut c = matches(r#"^foo\d*bar$"#.to_owned()); + assert_eq!(true, c.map("foobar")); + assert_eq!(true, c.map("foo99bar")); + assert_eq!(false, c.map("foo99barz")); + assert_eq!(false, c.map("bat")); + + // regex from RegexBuilder + let mut c = matches(regex::bytes::RegexBuilder::new("foobar").case_insensitive(true)); + assert_eq!(true, c.map("foobar")); + assert_eq!(true, c.map("FOOBAR")); + assert_eq!(false, c.map("FOO99BAR")); + + // regex from Regex + let mut c = matches( + regex::bytes::RegexBuilder::new("foobar") + .case_insensitive(true) + .build() + .unwrap(), + ); + assert_eq!(true, c.map("foobar")); + assert_eq!(true, c.map("FOOBAR")); + assert_eq!(false, c.map("FOO99BAR")); } #[test]