1use std::{
2 fmt::Display,
3 ops::{Deref, DerefMut},
4};
56use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
78use crate::Type;
910/// Type that uses a special value to be used as none.
11///
12/// See [`Optional`] documentation for the rationale for this trait's existence.
13///
14/// # Caveats
15///
16/// Since use of default values as none is typical, this trait is implemented for all types that
17/// implement [`Default`] for convenience. Unfortunately, this means you can not implement this
18/// trait manually for types that implement [`Default`].
19///
20/// Moreoever, since `bool` implements [`Default`], `NoneValue` gets implemented for `bool` as well.
21/// However, this is unsound since its not possible to distinguish between `false` and `None` in
22/// this case. This is why you'll get a panic on trying to serialize or deserialize an
23/// `Optionanl<bool>`.
24pub trait NoneValue {
25type NoneType;
2627/// The none-equivalent value.
28fn null_value() -> Self::NoneType;
29}
3031impl<T> NoneValue for T
32where
33T: Default,
34{
35type NoneType = Self;
3637fn null_value() -> Self {
38 Default::default()
39 }
40}
4142/// An optional value.
43///
44/// Since D-Bus doesn't have the concept of nullability, it uses a special value (typically the
45/// default value) as the null value. For example [this signal][ts] uses empty strings for null
46/// values. Serde has built-in support for `Option` but unfortunately that doesn't work for us.
47/// Hence the need for this type.
48///
49/// The serialization and deserialization of `Optional` relies on [`NoneValue`] implementation of
50/// the underlying type.
51///
52/// # Examples
53///
54/// ```
55/// use zvariant::{serialized::Context, Optional, to_bytes, LE};
56///
57/// // `Null` case.
58/// let ctxt = Context::new_dbus(LE, 0);
59/// let s = Optional::<&str>::default();
60/// let encoded = to_bytes(ctxt, &s).unwrap();
61/// assert_eq!(encoded.bytes(), &[0, 0, 0, 0, 0]);
62/// let s: Optional<&str> = encoded.deserialize().unwrap().0;
63/// assert_eq!(*s, None);
64///
65/// // `Some` case.
66/// let s = Optional::from(Some("hello"));
67/// let encoded = to_bytes(ctxt, &s).unwrap();
68/// assert_eq!(encoded.len(), 10);
69/// // The first byte is the length of the string in Little-Endian format.
70/// assert_eq!(encoded[0], 5);
71/// let s: Optional<&str> = encoded.deserialize().unwrap().0;
72/// assert_eq!(*s, Some("hello"));
73/// ```
74///
75/// [ts]: https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-name-owner-changed
76#[derive(Clone, Debug, PartialEq, Eq, Hash)]
77pub struct Optional<T>(Option<T>);
7879impl<T> Type for Optional<T>
80where
81T: Type,
82{
83fn signature() -> crate::Signature<'static> {
84 T::signature()
85 }
86}
8788impl<T> Serialize for Optional<T>
89where
90T: Type + NoneValue + Serialize,
91 <T as NoneValue>::NoneType: Serialize,
92{
93fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94where
95S: Serializer,
96 {
97if T::signature() == bool::signature() {
98panic!("`Optional<bool>` type is not supported");
99 }
100101match &self.0 {
102Some(value) => value.serialize(serializer),
103None => T::null_value().serialize(serializer),
104 }
105 }
106}
107108impl<'de, T, E> Deserialize<'de> for Optional<T>
109where
110T: Type + NoneValue + Deserialize<'de>,
111 <T as NoneValue>::NoneType: Deserialize<'de> + TryInto<T, Error = E> + PartialEq,
112 E: Display,
113{
114fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
115where
116D: Deserializer<'de>,
117 {
118if T::signature() == bool::signature() {
119panic!("`Optional<bool>` type is not supported");
120 }
121122let value = <<T as NoneValue>::NoneType>::deserialize(deserializer)?;
123if value == T::null_value() {
124Ok(Optional(None))
125 } else {
126Ok(Optional(Some(value.try_into().map_err(de::Error::custom)?)))
127 }
128 }
129}
130131impl<T> From<Option<T>> for Optional<T> {
132fn from(value: Option<T>) -> Self {
133 Optional(value)
134 }
135}
136137impl<T> From<Optional<T>> for Option<T> {
138fn from(value: Optional<T>) -> Self {
139 value.0
140}
141}
142143impl<T> Deref for Optional<T> {
144type Target = Option<T>;
145146fn deref(&self) -> &Self::Target {
147&self.0
148}
149}
150151impl<T> DerefMut for Optional<T> {
152fn deref_mut(&mut self) -> &mut Self::Target {
153&mut self.0
154}
155}
156157impl<T> Default for Optional<T> {
158fn default() -> Self {
159Self(None)
160 }
161}
162163#[cfg(test)]
164mod tests {
165use std::panic::catch_unwind;
166167#[test]
168fn bool_in_optional() {
169// Ensure trying to encode/decode `bool` in `Optional` fails.
170use crate::{to_bytes, Optional, LE};
171172let ctxt = crate::serialized::Context::new_dbus(LE, 0);
173let res = catch_unwind(|| to_bytes(ctxt, &Optional::<bool>::default()));
174assert!(res.is_err());
175176let data = crate::serialized::Data::new([0, 0, 0, 0].as_slice(), ctxt);
177let res = catch_unwind(|| data.deserialize::<Optional<bool>>());
178assert!(res.is_err());
179 }
180}