Skip to main content

brainwires_permissions/types/
path_pattern.rs

1use serde::{Deserialize, Serialize};
2
3/// Path pattern for glob matching
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(transparent)]
6pub struct PathPattern {
7    pattern: String,
8}
9
10impl PathPattern {
11    /// Create a new path pattern
12    pub fn new(pattern: &str) -> Self {
13        Self {
14            pattern: pattern.to_string(),
15        }
16    }
17
18    /// Create a glob pattern
19    pub fn glob(pattern: &str) -> Self {
20        Self::new(pattern)
21    }
22
23    /// Check if a path matches this pattern
24    #[cfg(feature = "native")]
25    pub fn matches(&self, path: &str) -> bool {
26        // Use glob matching
27        if let Ok(pattern) = glob::Pattern::new(&self.pattern) {
28            pattern.matches(path) || pattern.matches_path(std::path::Path::new(path))
29        } else {
30            // Fall back to simple string matching if pattern is invalid
31            path.contains(&self.pattern)
32        }
33    }
34
35    /// Check if a path matches this pattern (simple string matching for WASM)
36    #[cfg(not(feature = "native"))]
37    pub fn matches(&self, path: &str) -> bool {
38        path.contains(&self.pattern)
39    }
40
41    /// Get the pattern string
42    pub fn pattern(&self) -> &str {
43        &self.pattern
44    }
45}