1use crate::capability::Capability;
13use crate::spec::CapabilitySpec;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum ScopeKind {
20 Paths,
22 Hosts,
24 Programs,
26 Targets,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36#[cfg_attr(feature = "serde", serde(tag = "type", rename_all = "snake_case"))]
37pub enum ResolvedCapability {
38 #[cfg_attr(feature = "serde", serde(rename = "fs:read"))]
40 FsRead {
41 paths: Vec<String>,
43 },
44 #[cfg_attr(feature = "serde", serde(rename = "net:http"))]
46 NetHttp {
47 hosts: Vec<String>,
49 },
50 #[cfg_attr(feature = "serde", serde(rename = "process:spawn"))]
52 ProcessSpawn {
53 programs: Vec<String>,
55 },
56 #[cfg_attr(feature = "serde", serde(rename = "device:input"))]
58 DeviceInput {
59 targets: Vec<String>,
61 },
62 #[cfg_attr(feature = "serde", serde(rename = "device:capture"))]
64 DeviceCapture {
65 targets: Vec<String>,
67 },
68}
69
70impl ResolvedCapability {
71 pub const fn capability(&self) -> Capability {
73 match self {
74 Self::FsRead { .. } => Capability::ReadFs,
75 Self::NetHttp { .. } => Capability::Net,
76 Self::ProcessSpawn { .. } => Capability::Process,
77 Self::DeviceInput { .. } | Self::DeviceCapture { .. } => Capability::Device,
78 }
79 }
80}
81
82pub const fn scope_kind(spec: &CapabilitySpec) -> ScopeKind {
84 match spec {
85 CapabilitySpec::FsRead(_) => ScopeKind::Paths,
86 CapabilitySpec::NetHttp(_) => ScopeKind::Hosts,
87 CapabilitySpec::ProcessSpawn(_) => ScopeKind::Programs,
88 CapabilitySpec::DeviceInput(_) | CapabilitySpec::DeviceCapture(_) => ScopeKind::Targets,
89 }
90}
91
92pub const fn class_of(spec: &CapabilitySpec) -> Capability {
97 match spec {
98 CapabilitySpec::FsRead(_) => Capability::ReadFs,
99 CapabilitySpec::NetHttp(_) => Capability::Net,
100 CapabilitySpec::ProcessSpawn(_) => Capability::Process,
101 CapabilitySpec::DeviceInput(_) | CapabilitySpec::DeviceCapture(_) => Capability::Device,
102 }
103}
104
105pub fn resolve(spec: &CapabilitySpec) -> ResolvedCapability {
109 match spec.clone() {
110 CapabilitySpec::FsRead(s) => ResolvedCapability::FsRead { paths: s.paths },
111 CapabilitySpec::NetHttp(s) => ResolvedCapability::NetHttp { hosts: s.hosts },
112 CapabilitySpec::ProcessSpawn(s) => ResolvedCapability::ProcessSpawn {
113 programs: s.programs,
114 },
115 CapabilitySpec::DeviceInput(s) => ResolvedCapability::DeviceInput { targets: s.targets },
116 CapabilitySpec::DeviceCapture(s) => {
117 ResolvedCapability::DeviceCapture { targets: s.targets }
118 }
119 }
120}
121
122pub fn class_of_all(specs: &[CapabilitySpec]) -> Capability {
126 specs.iter().map(class_of).max().unwrap_or(Capability::Pure)
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn class_of_is_total_on_every_variant() {
135 let specs = [
136 crate::spec::fs_read(&["data"]),
137 crate::spec::net_http(&["api.example.com"]),
138 crate::spec::process_spawn(),
139 crate::spec::device_input(&["window:editor"]),
140 crate::spec::device_capture(&["window:editor"]),
141 ];
142 let classes: Vec<Capability> = specs.iter().map(class_of).collect();
143 assert_eq!(
144 classes,
145 [
146 Capability::ReadFs,
147 Capability::Net,
148 Capability::Process,
149 Capability::Device,
150 Capability::Device
151 ]
152 );
153 }
154}