brainwires_permissions/types/
path_pattern.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(transparent)]
6pub struct PathPattern {
7 pattern: String,
8}
9
10impl PathPattern {
11 pub fn new(pattern: &str) -> Self {
13 Self {
14 pattern: pattern.to_string(),
15 }
16 }
17
18 pub fn glob(pattern: &str) -> Self {
20 Self::new(pattern)
21 }
22
23 #[cfg(feature = "native")]
25 pub fn matches(&self, path: &str) -> bool {
26 if let Ok(pattern) = glob::Pattern::new(&self.pattern) {
28 pattern.matches(path) || pattern.matches_path(std::path::Path::new(path))
29 } else {
30 path.contains(&self.pattern)
32 }
33 }
34
35 #[cfg(not(feature = "native"))]
37 pub fn matches(&self, path: &str) -> bool {
38 path.contains(&self.pattern)
39 }
40
41 pub fn pattern(&self) -> &str {
43 &self.pattern
44 }
45}