BlueGit

ict rusty-teams

Files

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

policy.rs

policy.rs 5.5 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}
41
42impl Default for AgentPolicy {
43 fn default() -> Self {
44 AgentPolicy {
45 enabled: false,
46 port: DEFAULT_PORT,
47 chats: Access::None,
48 channels: Access::None,
49 write: false,
50 review: true,
51 calendar: false,
52 transcripts: false,
53 }
54 }
55}
56
57impl AgentPolicy {
58 pub fn access(&self, kind: ConversationKind) -> &Access {
59 match kind {
60 ConversationKind::Chat | ConversationKind::Meeting => &self.chats,
61 ConversationKind::Channel => &self.channels,
62 }
63 }
64
65 pub fn can_read(&self, kind: ConversationKind, conversation_id: &str) -> bool {
66 self.enabled && self.access(kind).allows(conversation_id)
67 }
68
69 pub fn can_write(&self, kind: ConversationKind, conversation_id: &str) -> bool {
70 self.can_read(kind, conversation_id) && self.write
71 }
72
73 pub fn can_read_any_kind(&self, conversation_id: &str) -> bool {
74 self.enabled
75 && (self.chats.allows(conversation_id) || self.channels.allows(conversation_id))
76 }
77
78 pub fn readable_ids<'a>(
79 &self,
80 kind: ConversationKind,
81 conversation_ids: impl IntoIterator<Item = &'a str>,
82 ) -> Vec<String> {
83 conversation_ids
84 .into_iter()
85 .filter(|conversation_id| self.can_read(kind, conversation_id))
86 .map(str::to_owned)
87 .collect()
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 fn selected(ids: &[&str]) -> Access {
96 Access::Selected(ids.iter().map(|id| (*id).to_owned()).collect())
97 }
98
99 fn policy(chats: Access, channels: Access, write: bool) -> AgentPolicy {
100 AgentPolicy {
101 enabled: true,
102 chats,
103 channels,
104 write,
105 ..AgentPolicy::default()
106 }
107 }
108
109 #[test]
110 fn read_and_write_follow_scope_and_flags() {
111 use ConversationKind::{Channel, Chat, Meeting};
112 let cases = [
113 (
114 policy(Access::None, Access::None, true),
115 Chat,
116 "a",
117 false,
118 false,
119 ),
120 (
121 policy(Access::All, Access::None, false),
122 Chat,
123 "a",
124 true,
125 false,
126 ),
127 (
128 policy(Access::All, Access::None, true),
129 Chat,
130 "a",
131 true,
132 true,
133 ),
134 (
135 policy(selected(&["a"]), Access::None, true),
136 Chat,
137 "a",
138 true,
139 true,
140 ),
141 (
142 policy(selected(&["a"]), Access::None, true),
143 Chat,
144 "b",
145 false,
146 false,
147 ),
148 (
149 policy(selected(&["a"]), Access::None, true),
150 Meeting,
151 "a",
152 true,
153 true,
154 ),
155 (
156 policy(selected(&["a"]), Access::None, true),
157 Channel,
158 "a",
159 false,
160 false,
161 ),
162 (
163 policy(Access::None, Access::All, true),
164 Channel,
165 "a",
166 true,
167 true,
168 ),
169 (
170 policy(Access::All, selected(&["c"]), false),
171 Channel,
172 "d",
173 false,
174 false,
175 ),
176 ];
177 for (policy, kind, id, read, write) in cases {
178 assert_eq!(policy.can_read(kind, id), read, "{kind:?} {id}");
179 assert_eq!(policy.can_write(kind, id), write, "{kind:?} {id}");
180 }
181 }
182
183 #[test]
184 fn disabled_policy_allows_nothing() {
185 let policy = AgentPolicy {
186 enabled: false,
187 chats: Access::All,
188 channels: Access::All,
189 write: true,
190 ..AgentPolicy::default()
191 };
192 assert!(!policy.can_read(ConversationKind::Chat, "a"));
193 assert!(!policy.can_write(ConversationKind::Channel, "a"));
194 }
195
196 #[test]
197 fn default_is_closed() {
198 let policy = AgentPolicy::default();
199 assert_eq!(policy.port, DEFAULT_PORT);
200 assert!(!policy.enabled && !policy.write && policy.review && !policy.calendar && !policy.transcripts);
201 assert_eq!(policy.chats, Access::None);
202 assert_eq!(policy.channels, Access::None);
203 }
204
205 #[test]
206 fn readable_ids_keeps_only_allowed() {
207 let policy = policy(selected(&["a", "c"]), Access::None, false);
208 assert_eq!(
209 policy.readable_ids(ConversationKind::Chat, ["a", "b", "c"]),
210 ["a", "c"]
211 );
212 }
213}