use {
alloc::{
borrow::Cow,
collections::BTreeSet,
},
crate::Glob,
};
#[derive(Debug, Eq, PartialEq)]
pub struct GlobSet<'a> {
globs: BTreeSet<Glob<'a>>,
}
impl<'a> GlobSet<'a> {
pub fn from_iter<I0, I1>(iter: I0) -> Option<Self> where I0: IntoIterator<Item=I1>, I1: Into<Cow<'a, str>> {
let globs: BTreeSet<_> = iter.into_iter().filter_map(|s| {
let s = s.into();
(s.is_empty() == false).then(|| Glob::from(s))
}).collect();
(globs.is_empty() == false).then_some(Self { globs })
}
pub fn merge<I>(iter: I) -> Option<Self> where I: IntoIterator<Item=Self> {
let result = iter.into_iter().fold(Self { globs: BTreeSet::new() }, |mut result, mut item| {
result.globs.append(&mut item.globs);
result
});
(result.globs.is_empty() == false).then_some(result)
}
pub fn any<S>(&self, s: S) -> bool where S: AsRef<str> {
self.globs.iter().any(|g| g.matches(&s))
}
}