1use super::protocol::BusType;
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum DbusOperation {
36 Call,
38 Read,
40 Write,
42 Subscribe,
44}
45
46impl DbusOperation {
47 pub fn as_str(&self) -> &'static str {
49 match self {
50 DbusOperation::Call => "call",
51 DbusOperation::Read => "read",
52 DbusOperation::Write => "write",
53 DbusOperation::Subscribe => "subscribe",
54 }
55 }
56}
57
58impl std::fmt::Display for DbusOperation {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 write!(f, "{}", self.as_str())
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct DbusResource {
67 pub bus: BusType,
69
70 pub destination: String,
72
73 pub interface: String,
75
76 pub member: String,
78}
79
80impl DbusResource {
81 pub fn method_call(
103 bus: BusType,
104 destination: impl Into<String>,
105 interface: impl Into<String>,
106 method: impl Into<String>,
107 ) -> Self {
108 Self {
109 bus,
110 destination: destination.into(),
111 interface: interface.into(),
112 member: method.into(),
113 }
114 }
115
116 pub fn property_read(
120 bus: BusType,
121 destination: impl Into<String>,
122 interface: impl Into<String>,
123 property: impl Into<String>,
124 ) -> Self {
125 Self {
126 bus,
127 destination: destination.into(),
128 interface: interface.into(),
129 member: format!("@{}", property.into()),
130 }
131 }
132
133 pub fn property_write(
137 bus: BusType,
138 destination: impl Into<String>,
139 interface: impl Into<String>,
140 property: impl Into<String>,
141 ) -> Self {
142 Self {
143 bus,
144 destination: destination.into(),
145 interface: interface.into(),
146 member: format!("@{}", property.into()),
147 }
148 }
149
150 pub fn signal(
154 bus: BusType,
155 destination: impl Into<String>,
156 interface: impl Into<String>,
157 signal: impl Into<String>,
158 ) -> Self {
159 Self {
160 bus,
161 destination: destination.into(),
162 interface: interface.into(),
163 member: format!("#{}", signal.into()),
164 }
165 }
166
167 pub fn to_resource_string(&self) -> String {
171 format!(
172 "dbus:{}:{}:{}:{}",
173 self.bus, self.destination, self.interface, self.member
174 )
175 }
176
177 pub fn matches_pattern(&self, pattern: &str) -> bool {
181 let resource = self.to_resource_string();
182 pattern_matches(&resource, pattern)
183 }
184}
185
186impl std::fmt::Display for DbusResource {
187 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188 write!(f, "{}", self.to_resource_string())
189 }
190}
191
192fn pattern_matches(text: &str, pattern: &str) -> bool {
196 if pattern == "*" {
197 return true;
198 }
199
200 let pattern_parts: Vec<&str> = pattern.split('*').collect();
201
202 if pattern_parts.len() == 1 {
203 return text == pattern;
205 }
206
207 let mut pos = 0;
208
209 for (i, part) in pattern_parts.iter().enumerate() {
210 if part.is_empty() {
211 continue;
212 }
213
214 if let Some(found) = text[pos..].find(part) {
215 if i == 0 && found != 0 {
216 return false;
218 }
219 pos += found + part.len();
220 } else {
221 return false;
222 }
223 }
224
225 if !pattern.ends_with('*') && pos != text.len() {
227 return false;
228 }
229
230 true
231}
232
233pub fn container_subject(sandbox_id: &str, container_id: Option<&str>) -> String {
237 match container_id {
238 Some(cid) => format!("container:{sandbox_id}:{cid}"),
239 None => format!("container:{sandbox_id}"),
240 }
241}
242
243#[cfg(test)]
244mod tests {
245 use super::*;
246
247 #[test]
248 fn test_method_call_resource() {
249 let resource = DbusResource::method_call(
250 BusType::Session,
251 "org.freedesktop.Notifications",
252 "org.freedesktop.Notifications",
253 "Notify",
254 );
255
256 assert_eq!(
257 resource.to_resource_string(),
258 "dbus:session:org.freedesktop.Notifications:org.freedesktop.Notifications:Notify"
259 );
260 }
261
262 #[test]
263 fn test_property_read_resource() {
264 let resource = DbusResource::property_read(
265 BusType::Session,
266 "org.mpris.MediaPlayer2.spotify",
267 "org.mpris.MediaPlayer2.Player",
268 "PlaybackStatus",
269 );
270
271 assert_eq!(
272 resource.to_resource_string(),
273 "dbus:session:org.mpris.MediaPlayer2.spotify:org.mpris.MediaPlayer2.Player:@PlaybackStatus"
274 );
275 }
276
277 #[test]
278 fn test_property_write_resource() {
279 let resource = DbusResource::property_write(
280 BusType::Session,
281 "org.mpris.MediaPlayer2.spotify",
282 "org.mpris.MediaPlayer2.Player",
283 "Volume",
284 );
285
286 assert_eq!(
287 resource.to_resource_string(),
288 "dbus:session:org.mpris.MediaPlayer2.spotify:org.mpris.MediaPlayer2.Player:@Volume"
289 );
290 }
291
292 #[test]
293 fn test_signal_resource() {
294 let resource = DbusResource::signal(
295 BusType::System,
296 "org.freedesktop.NetworkManager",
297 "org.freedesktop.NetworkManager",
298 "StateChanged",
299 );
300
301 assert_eq!(
302 resource.to_resource_string(),
303 "dbus:system:org.freedesktop.NetworkManager:org.freedesktop.NetworkManager:#StateChanged"
304 );
305 }
306
307 #[test]
308 fn test_pattern_matching() {
309 assert!(pattern_matches(
311 "dbus:session:org.test:iface:Method",
312 "dbus:session:org.test:iface:Method"
313 ));
314
315 assert!(pattern_matches(
317 "dbus:session:org.test:iface:Method",
318 "dbus:session:org.test:*"
319 ));
320
321 assert!(pattern_matches(
323 "dbus:session:org.test:iface:Method",
324 "dbus:session:*:Method"
325 ));
326
327 assert!(pattern_matches("dbus:session:org.test:iface:Method", "*"));
329
330 assert!(!pattern_matches(
332 "dbus:session:org.test:iface:Method",
333 "dbus:system:*"
334 ));
335 }
336
337 #[test]
338 fn test_resource_matches_pattern() {
339 let resource = DbusResource::method_call(
340 BusType::Session,
341 "org.freedesktop.Notifications",
342 "org.freedesktop.Notifications",
343 "Notify",
344 );
345
346 assert!(resource.matches_pattern("dbus:session:org.freedesktop.Notifications:*"));
347 assert!(resource.matches_pattern("dbus:session:*:Notify"));
348 assert!(resource.matches_pattern("dbus:*"));
349 assert!(!resource.matches_pattern("dbus:system:*"));
350 }
351
352 #[test]
353 fn test_container_subject() {
354 assert_eq!(
355 container_subject("sandbox-123", None),
356 "container:sandbox-123"
357 );
358
359 assert_eq!(
360 container_subject("sandbox-123", Some("container-456")),
361 "container:sandbox-123:container-456"
362 );
363 }
364
365 #[test]
366 fn test_dbus_operation_display() {
367 assert_eq!(DbusOperation::Call.as_str(), "call");
368 assert_eq!(DbusOperation::Read.as_str(), "read");
369 assert_eq!(DbusOperation::Write.as_str(), "write");
370 assert_eq!(DbusOperation::Subscribe.as_str(), "subscribe");
371 }
372}