Skip to main content

pedant_types/
capability.rs

1use std::str::FromStr;
2
3use serde::{Deserialize, Serialize};
4
5use crate::ParseCapabilityError;
6
7/// A runtime or compile-time capability that a crate may exercise.
8#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9#[serde(rename_all = "snake_case")]
10pub enum Capability {
11    /// TCP, UDP, HTTP, WebSocket, or DNS.
12    Network,
13    /// Reading files or walking directories.
14    FileRead,
15    /// Creating, writing, or deleting files and directories.
16    FileWrite,
17    /// Spawning child processes.
18    ProcessExec,
19    /// Reading environment variables.
20    EnvAccess,
21    /// `unsafe` blocks, `unsafe fn`, or `unsafe impl`.
22    UnsafeCode,
23    /// Foreign function interface calls or `extern` blocks.
24    Ffi,
25    /// Encryption, hashing, signing, or embedded key material.
26    Crypto,
27    /// `SystemTime`, `Instant`, or third-party clock access.
28    SystemTime,
29    /// Proc macro definition (code execution at compile time).
30    ProcMacro,
31}
32
33impl FromStr for Capability {
34    type Err = ParseCapabilityError;
35
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s {
38            "network" => Ok(Self::Network),
39            "file_read" => Ok(Self::FileRead),
40            "file_write" => Ok(Self::FileWrite),
41            "process_exec" => Ok(Self::ProcessExec),
42            "env_access" => Ok(Self::EnvAccess),
43            "unsafe_code" => Ok(Self::UnsafeCode),
44            "ffi" => Ok(Self::Ffi),
45            "crypto" => Ok(Self::Crypto),
46            "system_time" => Ok(Self::SystemTime),
47            "proc_macro" => Ok(Self::ProcMacro),
48            _ => Err(ParseCapabilityError::new(s)),
49        }
50    }
51}