use std::borrow::Borrow; use regex::Regex; // Yeah, I'm aware this is sorta crappy, should be cheaper to compile a regex // ORing all the patterns, I guess... #[derive(Debug)] pub struct RegexSet { items: Vec } impl RegexSet { pub fn is_empty(&self) -> bool { self.items.is_empty() } pub fn extend(&mut self, iter: I) where I: IntoIterator { for s in iter.into_iter() { self.insert(&s) } } 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); } } } 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![], } } }