dbus_message_parser/match_rule/
decode.rs1use super::{
2 arg::Arg, arg_path::ArgPath, split::Split, unescape::unescape, MatchRule, MatchRuleError,
3};
4use crate::{
5 message::MessageType,
6 value::{Bus, Interface, Member, ObjectPath, UniqueConnectionName},
7};
8use std::convert::TryFrom;
9
10impl TryFrom<&str> for MatchRule {
11 type Error = MatchRuleError;
12
13 fn try_from(match_rule: &str) -> Result<Self, Self::Error> {
14 let (key, value) = match_rule
15 .split_once('=')
16 .ok_or(MatchRuleError::MissingEqual)?;
17
18 if key == "type" {
19 let r#type = unescape(value)?;
20 let r#type = MessageType::try_from(r#type.as_str())?;
21 Ok(MatchRule::Type(r#type))
22 } else if key == "sender" {
23 let sender = unescape(value)?;
24 let sender = Bus::try_from(sender.as_str())?;
25 Ok(MatchRule::Sender(sender))
26 } else if key == "interface" {
27 let interface = unescape(value)?;
28 match Interface::try_from(interface.as_str()) {
29 Ok(interface) => Ok(MatchRule::Interface(interface)),
30 Err(e) => Err(MatchRuleError::InterfaceError(e)),
31 }
32 } else if key == "member" {
33 let member = unescape(value)?;
34 let member = Member::try_from(member.as_str())?;
35 Ok(MatchRule::Member(member))
36 } else if key == "path" {
37 let path = unescape(value)?;
38 match ObjectPath::try_from(path.as_str()) {
39 Ok(path) => Ok(MatchRule::Path(path)),
40 Err(e) => Err(MatchRuleError::PathError(e)),
41 }
42 } else if key == "path_namespace" {
43 let path_namespace = unescape(value)?;
44 match ObjectPath::try_from(path_namespace.as_str()) {
45 Ok(path_namespace) => Ok(MatchRule::PathNamespace(path_namespace)),
46 Err(e) => Err(MatchRuleError::PathErrorNamespace(e)),
47 }
48 } else if key == "destination" {
49 let destination = unescape(value)?;
50 let destination = UniqueConnectionName::try_from(destination.as_str())?;
51 Ok(MatchRule::Destination(destination))
52 } else if let Some(key_arg) = key.strip_prefix("arg") {
53 if let Some(index) = key_arg.strip_suffix("path") {
54 let index = index.parse::<usize>()?;
55 let arg_path = unescape(value)?;
56 match ObjectPath::try_from(arg_path.as_str()) {
57 Ok(arg_path) => Ok(MatchRule::ArgPath(ArgPath::from((index, arg_path)))),
58 Err(e) => Err(MatchRuleError::ArgPathError(e)),
59 }
60 } else if key_arg == "0namespace" {
61 let arg0_namespace = unescape(value)?;
62 match Interface::try_from(arg0_namespace.as_str()) {
63 Ok(arg0_namespace) => Ok(MatchRule::Arg0Namespace(arg0_namespace)),
64 Err(e) => Err(MatchRuleError::Arg0NamespaceError(e)),
65 }
66 } else {
67 let index = key_arg.parse::<usize>()?;
68 let arg = unescape(value)?;
69 let arg = Arg::try_from((index, arg))?;
70 Ok(MatchRule::Arg(arg))
71 }
72 } else if key == "eavesdrop" {
73 let eavesdrop = unescape(value)?;
74 match eavesdrop.as_str() {
75 "true" => Ok(MatchRule::Eavesdrop(true)),
76 "false" => Ok(MatchRule::Eavesdrop(false)),
77 _ => Err(MatchRuleError::EavesdropUnknown),
78 }
79 } else {
80 Err(MatchRuleError::KeyUnknown)
81 }
82 }
83}
84
85impl MatchRule {
86 pub fn decode(match_rules: &str) -> Result<Vec<MatchRule>, MatchRuleError> {
87 let mut result = Vec::new();
88 for match_rule in Split::new(match_rules) {
89 let match_rule = MatchRule::try_from(match_rule?)?;
90 result.push(match_rule);
91 }
92 Ok(result)
93 }
94}