use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::ParseCapabilityError;
macro_rules! capability_variants {
($($(#[$meta:meta])* $variant:ident => $snake:literal),+ $(,)?) => {
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
pub enum Capability {
$($(#[$meta])* $variant,)+
}
impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
$(Self::$variant => $snake,)+
};
f.write_str(s)
}
}
impl FromStr for Capability {
type Err = ParseCapabilityError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($snake => Ok(Self::$variant),)+
_ => Err(ParseCapabilityError::new(s)),
}
}
}
};
}
capability_variants! {
Network => "network",
FileRead => "file_read",
FileWrite => "file_write",
ProcessExec => "process_exec",
EnvAccess => "env_access",
UnsafeCode => "unsafe_code",
Ffi => "ffi",
Crypto => "crypto",
SystemTime => "system_time",
ProcMacro => "proc_macro",
}