Skip to main content

tokio_dbus/message/
message_kind.rs

1#[cfg(feature = "alloc")]
2use crate::message::OwnedMessageKind;
3use crate::{ObjectPath, Serial};
4
5/// The kind of a D-Bus message.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum MessageKind<'a> {
9    /// Method call. This message type may prompt a reply.
10    MethodCall {
11        /// The path being called.
12        path: &'a ObjectPath,
13        /// The member being called.
14        member: &'a str,
15    },
16    /// Method reply with returned data.
17    MethodReturn {
18        /// The serial this is a reply to.
19        reply_serial: Serial,
20    },
21    /// Error reply. If the first argument exists and is a string, it is an
22    /// error message.
23    Error {
24        /// The name of the error.
25        error_name: &'a str,
26        /// The serial this is a reply to.
27        reply_serial: Serial,
28    },
29    /// Signal emission.
30    Signal {
31        /// The path the signal is emitted from.
32        path: &'a ObjectPath,
33        /// The member being signalled.
34        member: &'a str,
35    },
36}
37
38#[cfg(feature = "alloc")]
39impl MessageKind<'_> {
40    #[inline]
41    pub(crate) fn to_owned(self) -> OwnedMessageKind {
42        match self {
43            MessageKind::MethodCall { path, member } => OwnedMessageKind::MethodCall {
44                path: path.into(),
45                member: member.into(),
46            },
47            MessageKind::MethodReturn { reply_serial } => {
48                OwnedMessageKind::MethodReturn { reply_serial }
49            }
50            MessageKind::Error {
51                error_name,
52                reply_serial,
53            } => OwnedMessageKind::Error {
54                error_name: error_name.into(),
55                reply_serial,
56            },
57            MessageKind::Signal { path, member } => OwnedMessageKind::Signal {
58                path: path.into(),
59                member: member.into(),
60            },
61        }
62    }
63}
64
65#[cfg(feature = "alloc")]
66impl PartialEq<OwnedMessageKind> for MessageKind<'_> {
67    fn eq(&self, other: &OwnedMessageKind) -> bool {
68        match (*self, other) {
69            (
70                MessageKind::MethodCall {
71                    path: path_left,
72                    member: member_left,
73                },
74                OwnedMessageKind::MethodCall {
75                    path: path_right,
76                    member: member_right,
77                },
78            ) => *path_left == **path_right && *member_left == **member_right,
79            (
80                MessageKind::MethodReturn {
81                    reply_serial: reply_serial_left,
82                },
83                OwnedMessageKind::MethodReturn {
84                    reply_serial: reply_serial_right,
85                },
86            ) => reply_serial_left == *reply_serial_right,
87            (
88                MessageKind::Error {
89                    error_name: error_name_left,
90                    reply_serial: reply_serial_left,
91                },
92                OwnedMessageKind::Error {
93                    error_name: error_name_right,
94                    reply_serial: reply_serial_right,
95                },
96            ) => *error_name_left == **error_name_right && reply_serial_left == *reply_serial_right,
97            (
98                MessageKind::Signal {
99                    path: path_left,
100                    member: member_left,
101                },
102                OwnedMessageKind::Signal {
103                    path: path_right,
104                    member: member_right,
105                },
106            ) => *path_left == **path_right && *member_left == **member_right,
107            _ => false,
108        }
109    }
110}