use regex::{self, Error as RegexError};
use std::str::FromStr;
use crate::{error, matcher::Matcher, path::Path, streamer::ParsedKind};
#[derive(Debug, Clone)]
pub struct Regex {
regex: regex::Regex,
}
impl Regex {
pub fn new(rgx: regex::Regex) -> Self {
Self { regex: rgx }
}
}
impl Matcher for Regex {
fn match_path(&self, path: &Path, _kind: ParsedKind) -> bool {
let str_path: String = path.to_string();
self.regex.is_match(&str_path)
}
}
impl FromStr for Regex {
type Err = error::Matcher;
fn from_str(path: &str) -> Result<Self, Self::Err> {
let regex = regex::Regex::from_str(path)
.map_err(|e: RegexError| Self::Err::Parse(e.to_string()))?;
Ok(Self::new(regex))
}
}