#[cfg(feature = "regex")]
use regex::Regex;
pub enum Pattern<'a> {
Literal(&'a str),
#[cfg(feature = "regex")]
Re(&'a Regex),
}
impl Pattern<'_> {
pub fn is_match(&self, hay: &str) -> bool {
match *self {
Pattern::Literal(s) => hay.contains(s),
#[cfg(feature = "regex")]
Pattern::Re(re) => re.is_match(hay),
}
}
pub fn replace_all(&self, buf: &str, repl: &str) -> String {
match *self {
Pattern::Literal(s) => buf.replace(s, repl),
#[cfg(feature = "regex")]
Pattern::Re(re) => re.replace_all(buf, repl).into(),
}
}
}
impl<'a> From<&'a str> for Pattern<'a> {
fn from(s: &'a str) -> Self {
Pattern::Literal(s)
}
}
#[cfg(feature = "regex")]
impl<'a> From<&'a Regex> for Pattern<'a> {
fn from(re: &'a Regex) -> Self {
Pattern::Re(re)
}
}