use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PathPattern {
pattern: String,
}
impl PathPattern {
pub fn new(pattern: &str) -> Self {
Self {
pattern: pattern.to_string(),
}
}
pub fn glob(pattern: &str) -> Self {
Self::new(pattern)
}
#[cfg(feature = "native")]
pub fn matches(&self, path: &str) -> bool {
if let Ok(pattern) = glob::Pattern::new(&self.pattern) {
pattern.matches(path) || pattern.matches_path(std::path::Path::new(path))
} else {
path.contains(&self.pattern)
}
}
#[cfg(not(feature = "native"))]
pub fn matches(&self, path: &str) -> bool {
path.contains(&self.pattern)
}
pub fn pattern(&self) -> &str {
&self.pattern
}
}