use std::borrow::Cow;
use glob_match::glob_match;
type FilterListType<'src> = Vec<Cow<'src, str>>;
pub struct Filter<'src> {
included: FilterListType<'src>,
excluded: FilterListType<'src>,
}
impl<'src> Filter<'src> {
pub fn new<Iter, Str>(included: Iter, excluded: Iter) -> Filter<'src>
where
Iter: IntoIterator<Item = Str>,
Str: Into<Cow<'src, str>>,
{
Filter {
included: included.into_iter().map(|s| s.into()).collect(),
excluded: excluded.into_iter().map(|s| s.into()).collect(),
}
}
pub fn all() -> Filter<'src> {
Filter {
included: Vec::new(),
excluded: Vec::new(),
}
}
pub fn check(&self, path: &str) -> bool {
let is_included = self.match_path(&self.included, path).unwrap_or(true);
let is_excluded = self.match_path(&self.excluded, path).unwrap_or(false);
return is_included && !is_excluded;
}
fn match_path(&self, globs: &FilterListType, path: &str) -> Option<bool> {
if globs.is_empty() {
return None;
}
for glob in globs {
if glob_match(glob, path) {
return Some(true);
}
}
return Some(false);
}
}