use anyhow::Context;
use anyhow::Result;
use globset::Glob;
use globset::GlobMatcher;
use std::path::Path;
pub struct Exclusion {
pattern: String,
matcher: GlobMatcher,
}
impl Exclusion {
pub fn new(pattern: &str) -> Result<Self> {
let matcher = Glob::new(pattern)
.with_context(|| format!("`{pattern}` is not a usable exclude pattern"))?
.compile_matcher();
Ok(Self {
pattern: pattern.to_string(),
matcher,
})
}
pub fn matches(&self, relative: &Path) -> bool {
self.matcher.is_match(Self::normalised(relative))
}
pub fn pattern(&self) -> &str {
&self.pattern
}
fn normalised(relative: &Path) -> String {
relative.to_string_lossy().replace('\\', "/")
}
}