use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PathPattern(pub String);
impl PathPattern {
pub fn matches(&self, path: &str) -> bool {
glob_match(&self.0, path)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandPattern(pub String);
impl CommandPattern {
pub fn matches(&self, program: &str) -> bool {
glob_match(&self.0, program)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkPattern(pub String);
impl NetworkPattern {
pub fn matches(&self, target: &str) -> bool {
glob_match(&self.0, target)
}
}
fn glob_match(pattern: &str, value: &str) -> bool {
if pattern == "*" {
return true;
}
if let Some(prefix) = pattern.strip_suffix('*') {
return value.starts_with(prefix);
}
if let Some(suffix) = pattern.strip_prefix('*') {
return value.ends_with(suffix);
}
pattern == value
}