systemd_zbus/
lib.rs

1//! Upstream docs - <https://www.freedesktop.org/software/systemd/man/org.freedesktop.systemd1.html>
2
3pub use zbus;
4
5mod generated;
6pub use generated::*;
7
8mod types;
9pub use types::*;
10
11#[cfg(test)]
12mod tests;
13
14#[macro_export]
15macro_rules! enum_impl_serde_str {
16    ($type_name:ident) => {
17        impl serde::Serialize for $type_name {
18            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19            where
20                S: serde::Serializer,
21            {
22                serializer.serialize_str(self.into())
23            }
24        }
25
26        impl<'de> serde::Deserialize<'de> for $type_name {
27            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
28            where
29                D: serde::Deserializer<'de>,
30            {
31                use std::str::FromStr;
32                let s = String::deserialize(deserializer)?;
33                $type_name::from_str(s.as_str()).map_err(serde::de::Error::custom)
34            }
35        }
36    };
37}
38
39#[macro_export]
40macro_rules! impl_value_conversions_as_str {
41    ($type_name:ident) => {
42        impl TryFrom<zbus::zvariant::OwnedValue> for $type_name {
43            type Error = zbus::Error;
44
45            fn try_from(value: zbus::zvariant::OwnedValue) -> Result<Self, zbus::Error> {
46                use std::str::FromStr;
47                let value = <String>::try_from(value)?;
48                return Ok($type_name::from_str(value.as_str())?);
49            }
50        }
51        impl<'a> TryFrom<zbus::zvariant::Value<'a>> for $type_name {
52            type Error = zbus::Error;
53
54            fn try_from(value: zbus::zvariant::Value<'a>) -> Result<Self, zbus::Error> {
55                use std::str::FromStr;
56                let value = <zbus::zvariant::Str>::try_from(value)?;
57                return Ok($type_name::from_str(value.as_str())?);
58            }
59        }
60        impl<'a> From<&'a $type_name> for zbus::zvariant::OwnedValue {
61            fn from(value: &'a $type_name) -> Self {
62                let string: &str = value.into();
63                zbus::zvariant::Str::from(string).into()
64            }
65        }
66        impl From<$type_name> for zbus::zvariant::OwnedValue {
67            fn from(value: $type_name) -> Self {
68                let string: &str = value.into();
69                zbus::zvariant::Str::from(string).into()
70            }
71        }
72        impl<'a> From<&'a $type_name> for zbus::zvariant::Value<'a> {
73            fn from(value: &'a $type_name) -> Self {
74                let string: &str = value.into();
75                zbus::zvariant::Str::from(string).into()
76            }
77        }
78        impl<'a> From<$type_name> for zbus::zvariant::Value<'a> {
79            fn from(value: $type_name) -> Self {
80                let string: &str = value.into();
81                zbus::zvariant::Str::from(string).into()
82            }
83        }
84    };
85}
86
87#[macro_export]
88macro_rules! enum_impl_str_conv {
89    ($type_name:ident, { $($label:tt : $variant:tt,)* }) => {
90        impl std::str::FromStr for $type_name {
91            type Err = zbus::fdo::Error;
92
93            fn from_str(m: &str) -> Result<Self, Self::Err> {
94                let res = match m {
95                    $($label => $type_name::$variant,)+
96                    _ => return Err(zbus::fdo::Error::IOError(format!("{} is an invalid variant", m))),
97                };
98                Ok(res)
99            }
100        }
101
102        impl From<$type_name> for &str {
103            fn from(m: $type_name) -> Self {
104                match m {
105                    $($type_name::$variant => $label,)+
106                }
107            }
108        }
109
110        impl From<&$type_name> for &str {
111            fn from(s: &$type_name) -> Self {
112                <&str>::from(*s)
113            }
114        }
115}}