Skip to main content

keel_policy/
paths.rs

1use serde::{Deserialize, Serialize};
2use std::path::{Path, PathBuf};
3
4/// How a path may be accessed.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum FsAccess {
8    Read,
9    ReadWrite,
10    /// Kernel-enforced deny (read + write/rename where the backend supports it).
11    Deny,
12}
13
14/// A single filesystem rule.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct FsRule {
17    pub path: PathBuf,
18    pub access: FsAccess,
19    /// If true, `path` is treated as a glob (e.g. `**/.env`).
20    #[serde(default)]
21    pub glob: bool,
22}
23
24impl FsRule {
25    pub fn read(path: impl Into<PathBuf>) -> Self {
26        Self {
27            path: path.into(),
28            access: FsAccess::Read,
29            glob: false,
30        }
31    }
32
33    pub fn read_write(path: impl Into<PathBuf>) -> Self {
34        Self {
35            path: path.into(),
36            access: FsAccess::ReadWrite,
37            glob: false,
38        }
39    }
40
41    pub fn deny(path: impl Into<PathBuf>) -> Self {
42        Self {
43            path: path.into(),
44            access: FsAccess::Deny,
45            glob: false,
46        }
47    }
48
49    pub fn deny_glob(pattern: impl Into<PathBuf>) -> Self {
50        Self {
51            path: pattern.into(),
52            access: FsAccess::Deny,
53            glob: true,
54        }
55    }
56}
57
58/// Path pattern helpers for policy authors.
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60pub struct PathPattern(String);
61
62impl PathPattern {
63    pub fn new(s: impl Into<String>) -> Self {
64        Self(s.into())
65    }
66
67    pub fn as_str(&self) -> &str {
68        &self.0
69    }
70
71    pub fn as_path(&self) -> &Path {
72        Path::new(&self.0)
73    }
74}