//! A type that represents the union of a set of regular expressions. use regex::Regex; use std::borrow::Borrow; // Yeah, I'm aware this is sorta crappy, should be cheaper to compile a regex // ORing all the patterns, I guess... /// A dynamic set of regular expressions. #[derive(Debug)] pub struct RegexSet { items: Vec, } impl RegexSet { /// Is this set empty? pub fn is_empty(&self) -> bool { self.items.is_empty() } /// Extend this set with every regex in the iterator. pub fn extend(&mut self, iter: I) where I: IntoIterator, { for s in iter.into_iter() { self.insert(&s) } } /// Insert a new regex into this set. pub fn insert(&mut self, string: &S) where S: Borrow, { let s = string.borrow(); match Regex::new(&format!("^{}$", s)) { Ok(r) => { self.items.push(r); } Err(err) => { error!("Invalid pattern provided: {}, {:?}", s, err); } } } /// Does the given `string` match any of the regexes in this set? pub fn matches(&self, string: &S) -> bool where S: Borrow, { let s = string.borrow(); for r in &self.items { if r.is_match(s) { return true; } } false } } impl Default for RegexSet { fn default() -> Self { RegexSet { items: vec![], } } }