use {
alloc::borrow::Cow,
crate::Glob,
};
#[cfg(not(feature="std"))]
use alloc::collections::BTreeSet as Set;
#[cfg(feature="std")]
use std::collections::HashSet as Set;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct GlobSet<'a> {
globs: Set<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 = iter.into_iter().filter_map(|s| {
let s = s.into();
(s.is_empty() == false).then(|| Glob::from(s))
}).collect::<Set<_>>();
(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: Set::new() }, |mut result, item| {
result.globs.extend(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))
}
#[inline]
pub fn is_disjoint(&self, other: &Self) -> bool {
self.globs.is_disjoint(&other.globs)
}
#[inline]
pub fn is_subset(&self, other: &Self) -> bool {
self.globs.is_subset(&other.globs)
}
#[inline]
pub fn is_superset(&self, other: &Self) -> bool {
self.globs.is_superset(&other.globs)
}
}