use regex::Regex;
pub struct Pattern(pub Box<dyn Fn(&str) -> bool + Sync + Send>);
impl Pattern {
pub fn any() -> Self {
Pattern(Box::new(move |_value| true))
}
pub fn regex(re: Regex) -> Self {
Pattern(Box::new(move |value| re.is_match(value)))
}
pub fn matches(&self, value: &str) -> bool {
(self.0)(value)
}
}
impl ::std::ops::Not for Pattern {
type Output = Pattern;
fn not(self) -> Self::Output {
let cb = self.0;
Pattern(Box::new(move |value| !cb(value)))
}
}
impl ::std::ops::BitAnd for Pattern {
type Output = Pattern;
fn bitand(self, rhs: Pattern) -> Self::Output {
let cb1 = self.0;
let cb2 = rhs.0;
Pattern(Box::new(move |value| cb1(value) && cb2(value)))
}
}
impl ::std::ops::BitOr for Pattern {
type Output = Pattern;
fn bitor(self, rhs: Pattern) -> Self::Output {
let cb1 = self.0;
let cb2 = rhs.0;
Pattern(Box::new(move |value| cb1(value) || cb2(value)))
}
}