Skip to main content

phi_cap/
capability.rs

1//! Decision layer: the coarse capability classes a `Policy` matches on.
2//!
3//! Coarse classes describe the **action surface** โ€” "what can this touch" โ€”
4//! never data semantics: a remote DB query is [`Capability::Net`], the same
5//! query against a local file is [`Capability::ReadFs`] (sdk-assembly ยง4.3).
6//!
7//! This enum is the "right side" of the single translation point
8//! [`crate::class_of`]. There is exactly one decision vocabulary; new
9//! dimensions extend `CapabilitySpec` variants, never a third enum.
10
11use core::fmt;
12
13/// Coarse capability class โ€” the decision key handed to `Policy::allow`.
14///
15/// Ordering follows the O7 risk total order
16/// (`Device > Process > Net > WriteFs > ReadFs > Pure`); a cross-class
17/// declaration resolves to the highest-ranked class.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
21pub enum Capability {
22    /// Pure computation, no side effects.
23    Pure,
24    /// Reads files.
25    ReadFs,
26    /// Writes files.
27    WriteFs,
28    /// Network access.
29    Net,
30    /// Spawns processes.
31    Process,
32    /// Device / GUI operations.
33    Device,
34}
35
36impl Capability {
37    /// Position on the O7 risk total order. Used to break cross-class ties.
38    pub const fn risk(self) -> u8 {
39        match self {
40            Self::Pure => 0,
41            Self::ReadFs => 1,
42            Self::WriteFs => 2,
43            Self::Net => 3,
44            Self::Process => 4,
45            Self::Device => 5,
46        }
47    }
48}
49
50impl Ord for Capability {
51    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
52        self.risk().cmp(&other.risk())
53    }
54}
55
56impl PartialOrd for Capability {
57    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
58        Some(self.cmp(other))
59    }
60}
61
62impl fmt::Display for Capability {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        let s = match self {
65            Self::Pure => "pure",
66            Self::ReadFs => "read_fs",
67            Self::WriteFs => "write_fs",
68            Self::Net => "net",
69            Self::Process => "process",
70            Self::Device => "device",
71        };
72        f.write_str(s)
73    }
74}