1use serde::{Deserialize, Deserializer, Serialize};
7use std::fmt;
8
9#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
12#[serde(rename_all = "kebab-case")]
13pub enum CapabilityPolicy {
14 Deny,
16 #[default]
18 Ask,
19 Allow,
21}
22
23impl CapabilityPolicy {
24 #[must_use]
26 pub const fn as_str(self) -> &'static str {
27 match self {
28 Self::Deny => "deny",
29 Self::Ask => "ask",
30 Self::Allow => "allow",
31 }
32 }
33
34 #[must_use]
36 pub const fn is_denied(self) -> bool {
37 matches!(self, Self::Deny)
38 }
39
40 #[must_use]
42 pub const fn is_preauthorized(self) -> bool {
43 matches!(self, Self::Allow)
44 }
45
46 #[must_use]
48 pub const fn allows_requests(self) -> bool {
49 !self.is_denied()
50 }
51}
52
53impl fmt::Display for CapabilityPolicy {
54 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55 formatter.write_str(self.as_str())
56 }
57}
58
59impl<'de> Deserialize<'de> for CapabilityPolicy {
60 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61 where
62 D: Deserializer<'de>,
63 {
64 let value = String::deserialize(deserializer)?;
65 match value.as_str() {
66 "deny" => Ok(Self::Deny),
67 "ask" => Ok(Self::Ask),
68 "allow" => Ok(Self::Allow),
69 other => Err(serde::de::Error::custom(format!(
70 "invalid capability policy `{other}`; expected \"deny\", \"ask\", or \"allow\""
71 ))),
72 }
73 }
74}
75
76#[derive(
78 Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
79)]
80#[serde(rename_all = "kebab-case")]
81pub enum ApprovalScope {
82 #[default]
84 Once,
85 Session,
88}
89
90impl ApprovalScope {
91 #[must_use]
93 pub const fn as_str(self) -> &'static str {
94 match self {
95 Self::Once => "once",
96 Self::Session => "session",
97 }
98 }
99}
100
101impl fmt::Display for ApprovalScope {
102 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
103 formatter.write_str(self.as_str())
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::{ApprovalScope, CapabilityPolicy};
110
111 #[test]
112 fn capability_policy_round_trips_canonical_strings() {
113 for policy in [
114 CapabilityPolicy::Deny,
115 CapabilityPolicy::Ask,
116 CapabilityPolicy::Allow,
117 ] {
118 let encoded = serde_json::to_string(&policy).expect("serialize");
119 let decoded: CapabilityPolicy = serde_json::from_str(&encoded).expect("deserialize");
120 assert_eq!(decoded, policy);
121 assert_eq!(encoded, format!("\"{}\"", policy.as_str()));
122 }
123 assert!(serde_json::from_str::<CapabilityPolicy>("\"invalid\"").is_err());
124 }
125
126 #[test]
127 fn approval_scope_defaults_to_once_and_round_trips() {
128 assert_eq!(ApprovalScope::default(), ApprovalScope::Once);
129 for scope in [ApprovalScope::Once, ApprovalScope::Session] {
130 let encoded = serde_json::to_string(&scope).expect("serialize");
131 let decoded: ApprovalScope = serde_json::from_str(&encoded).expect("deserialize");
132 assert_eq!(decoded, scope);
133 assert_eq!(encoded, format!("\"{}\"", scope.as_str()));
134 }
135 }
136}