Skip to main content

perspt_sdk/
scope.rs

1//! Glob-like scope patterns shared by capabilities and grant policies
2//! (split from `capability.rs` under the file-length rules).
3
4use serde::{Deserialize, Serialize};
5
6/// A glob-like path pattern. `matches` uses a simple prefix/suffix/`*` rule.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct PathPattern(pub String);
9
10impl PathPattern {
11    pub fn matches(&self, path: &str) -> bool {
12        glob_match(&self.0, path)
13    }
14}
15
16/// A command pattern matched against the canonical program name.
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18pub struct CommandPattern(pub String);
19
20impl CommandPattern {
21    pub fn matches(&self, program: &str) -> bool {
22        glob_match(&self.0, program)
23    }
24}
25
26/// A network host/URL pattern.
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28pub struct NetworkPattern(pub String);
29
30impl NetworkPattern {
31    pub fn matches(&self, target: &str) -> bool {
32        glob_match(&self.0, target)
33    }
34}
35
36/// Minimal glob: supports a single trailing `*`, leading `*`, or exact match.
37fn glob_match(pattern: &str, value: &str) -> bool {
38    if pattern == "*" {
39        return true;
40    }
41    if let Some(prefix) = pattern.strip_suffix('*') {
42        return value.starts_with(prefix);
43    }
44    if let Some(suffix) = pattern.strip_prefix('*') {
45        return value.ends_with(suffix);
46    }
47    pattern == value
48}