BlueGit

ict rusty-teams

Files

rusty-teams / crates / mcp / src / policy.rs

policy.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
policy.rs 5.8 KiB Download Raw
1use std::collections::BTreeSet;
2
3pub const DEFAULT_PORT: u16 = 40615;
4
5#[derive(Debug, Clone, PartialEq, Eq, Default)]
6pub enum Access {
7 #[default]
8 None,
9 Selected(BTreeSet<String>),
10 All,
11}
12
13impl Access {
14 pub fn allows(&self, conversation_id: &str) -> bool {
15 match self {
16 Access::None => false,
17 Access::Selected(ids) => ids.contains(conversation_id),
18 Access::All => true,
19 }
20 }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum ConversationKind {
25 Chat,
26 Meeting,
27 Channel,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct AgentPolicy {
32 pub enabled: bool,
33 pub port: u16,
34 pub chats: Access,
35 pub channels: Access,
36 pub write: bool,
37 pub review: bool,
38 pub calendar: bool,
39 pub transcripts: bool,
40 pub meetings: bool,
41}
42
43impl Default for AgentPolicy {
44 fn default() -> Self {
45 AgentPolicy {
46 enabled: false,
47 port: DEFAULT_PORT,
48 chats: Access::None,
49 channels: Access::None,
50 write: false,
51 review: true,
52 calendar: false,
53 transcripts: false,
54 meetings: false,
55 }
56 }
57}
58
59impl AgentPolicy {
60 pub fn access(&self, kind: ConversationKind) -> &Access {
61 match kind {
62 ConversationKind::Chat | ConversationKind::Meeting => &self.chats,
63 ConversationKind::Channel => &self.channels,
64 }
65 }
66
67 pub fn can_read(&self, kind: ConversationKind, conversation_id: &str) -> bool {
68 self.enabled && self.access(kind).allows(conversation_id)
69 }
70
71 pub fn can_write(&self, kind: ConversationKind, conversation_id: &str) -> bool {
72 self.can_read(kind, conversation_id) && self.write
73 }
74
75 pub fn can_organize_meetings(&self) -> bool {
76 self.enabled && self.meetings
77 }
78
79 pub fn can_read_any_kind(&self, conversation_id: &str) -> bool {
80 self.enabled
81 && (self.chats.allows(conversation_id) || self.channels.allows(conversation_id))
82 }
83
84 pub fn readable_ids<'a>(
85 &self,
86 kind: ConversationKind,
87 conversation_ids: impl IntoIterator<Item = &'a str>,
88 ) -> Vec<String> {
89 conversation_ids
90 .into_iter()
91 .filter(|conversation_id| self.can_read(kind, conversation_id))
92 .map(str::to_owned)
93 .collect()
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 fn selected(ids: &[&str]) -> Access {
102 Access::Selected(ids.iter().map(|id| (*id).to_owned()).collect())
103 }
104
105 fn policy(chats: Access, channels: Access, write: bool) -> AgentPolicy {
106 AgentPolicy {
107 enabled: true,
108 chats,
109 channels,
110 write,
111 ..AgentPolicy::default()
112 }
113 }
114
115 #[test]
116 fn read_and_write_follow_scope_and_flags() {
117 use ConversationKind::{Channel, Chat, Meeting};
118 let cases = [
119 (
120 policy(Access::None, Access::None, true),
121 Chat,
122 "a",
123 false,
124 false,
125 ),
126 (
127 policy(Access::All, Access::None, false),
128 Chat,
129 "a",
130 true,
131 false,
132 ),
133 (
134 policy(Access::All, Access::None, true),
135 Chat,
136 "a",
137 true,
138 true,
139 ),
140 (
141 policy(selected(&["a"]), Access::None, true),
142 Chat,
143 "a",
144 true,
145 true,
146 ),
147 (
148 policy(selected(&["a"]), Access::None, true),
149 Chat,
150 "b",
151 false,
152 false,
153 ),
154 (
155 policy(selected(&["a"]), Access::None, true),
156 Meeting,
157 "a",
158 true,
159 true,
160 ),
161 (
162 policy(selected(&["a"]), Access::None, true),
163 Channel,
164 "a",
165 false,
166 false,
167 ),
168 (
169 policy(Access::None, Access::All, true),
170 Channel,
171 "a",
172 true,
173 true,
174 ),
175 (
176 policy(Access::All, selected(&["c"]), false),
177 Channel,
178 "d",
179 false,
180 false,
181 ),
182 ];
183 for (policy, kind, id, read, write) in cases {
184 assert_eq!(policy.can_read(kind, id), read, "{kind:?} {id}");
185 assert_eq!(policy.can_write(kind, id), write, "{kind:?} {id}");
186 }
187 }
188
189 #[test]
190 fn disabled_policy_allows_nothing() {
191 let policy = AgentPolicy {
192 enabled: false,
193 chats: Access::All,
194 channels: Access::All,
195 write: true,
196 ..AgentPolicy::default()
197 };
198 assert!(!policy.can_read(ConversationKind::Chat, "a"));
199 assert!(!policy.can_write(ConversationKind::Channel, "a"));
200 }
201
202 #[test]
203 fn default_is_closed() {
204 let policy = AgentPolicy::default();
205 assert_eq!(policy.port, DEFAULT_PORT);
206 assert!(
207 !policy.enabled
208 && !policy.write
209 && policy.review
210 && !policy.calendar
211 && !policy.transcripts
212 );
213 assert_eq!(policy.chats, Access::None);
214 assert_eq!(policy.channels, Access::None);
215 }
216
217 #[test]
218 fn readable_ids_keeps_only_allowed() {
219 let policy = policy(selected(&["a", "c"]), Access::None, false);
220 assert_eq!(
221 policy.readable_ids(ConversationKind::Chat, ["a", "b", "c"]),
222 ["a", "c"]
223 );
224 }
225}