zvariant/
structure.rs

1#![allow(unknown_lints)]
2use serde::{
3    de::{DeserializeSeed, Deserializer, Error, SeqAccess, Visitor},
4    ser::{Serialize, SerializeTupleStruct, Serializer},
5};
6use std::fmt::{Display, Write};
7
8use crate::{
9    value::SignatureSeed, value_display_fmt, DynamicDeserialize, DynamicType, OwnedValue,
10    Signature, Value,
11};
12
13/// Use this to efficiently build a [`Structure`].
14///
15/// [`Structure`]: struct.Structure.html
16#[derive(Debug, Default, PartialEq)]
17pub struct StructureBuilder<'a>(Vec<Value<'a>>);
18
19impl<'a> StructureBuilder<'a> {
20    /// Create a new `StructureBuilder`.
21    ///
22    /// Same as `StructureBuilder::default()`.
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Append `field` to `self`.
28    ///
29    /// This method returns `Self` so that you can use the builder pattern to create a complex
30    /// structure.
31    #[must_use]
32    pub fn add_field<T>(self, field: T) -> Self
33    where
34        T: DynamicType + Into<Value<'a>>,
35    {
36        self.append_field(Value::new(field))
37    }
38
39    /// Append `field` to `self`.
40    ///
41    /// Identical to `add_field`, except the field must be in the form of a `Value`.
42    #[must_use]
43    pub fn append_field<'e: 'a>(mut self, field: Value<'e>) -> Self {
44        self.0.push(field);
45
46        self
47    }
48
49    /// Append `field` to `self`.
50    ///
51    /// Identical to `add_field`, except it makes changes in-place.
52    pub fn push_field<T>(&mut self, field: T)
53    where
54        T: DynamicType + Into<Value<'a>>,
55    {
56        self.push_value(Value::new(field))
57    }
58
59    /// Append `field` to `self`.
60    ///
61    /// Identical to `append_field`, except it makes changes in-place.
62    pub fn push_value<'e: 'a>(&mut self, field: Value<'e>) {
63        self.0.push(field)
64    }
65
66    /// Build the `Structure`.
67    ///
68    /// [`Structure`]: struct.Structure.html
69    pub fn build(self) -> crate::Result<Structure<'a>> {
70        if self.0.is_empty() {
71            return Err(crate::Error::EmptyStructure);
72        }
73
74        let fields_signatures: Box<[Signature]> =
75            self.0.iter().map(Value::value_signature).cloned().collect();
76        let signature = Signature::structure(fields_signatures);
77
78        Ok(Structure {
79            fields: self.0,
80            signature,
81        })
82    }
83
84    /// Same as `build` except Signature is provided.
85    pub(crate) fn build_with_signature<'s: 'a>(self, signature: &Signature) -> Structure<'a> {
86        Structure {
87            fields: self.0,
88            signature: signature.clone(),
89        }
90    }
91}
92
93/// Use this to deserialize a [`Structure`].
94///
95/// The lifetime `'a` is now redundant and kept only for backward compatibility. All instances now
96/// has a `'static` lifetime. This will be removed in the next major release.
97#[derive(Debug, Clone, PartialEq, Eq)]
98pub struct StructureSeed<'a> {
99    signature: Signature,
100    phantom: std::marker::PhantomData<&'a ()>,
101}
102
103impl StructureSeed<'static> {
104    /// Create a new `StructureSeed`
105    ///
106    /// The given signature must be a valid structure signature.
107    #[must_use]
108    pub fn new_unchecked(signature: &Signature) -> Self {
109        StructureSeed {
110            signature: signature.clone(),
111            phantom: std::marker::PhantomData,
112        }
113    }
114}
115
116impl TryFrom<Signature> for StructureSeed<'static> {
117    type Error = zvariant::Error;
118
119    fn try_from(signature: Signature) -> Result<Self, zvariant::Error> {
120        if !matches!(signature, Signature::Structure(_)) {
121            return Err(zvariant::Error::IncorrectType);
122        }
123
124        Ok(StructureSeed {
125            signature,
126            phantom: std::marker::PhantomData,
127        })
128    }
129}
130
131impl<'de> DeserializeSeed<'de> for StructureSeed<'_> {
132    type Value = Structure<'de>;
133    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
134    where
135        D: Deserializer<'de>,
136    {
137        deserializer.deserialize_seq(StructureVisitor {
138            signature: self.signature,
139        })
140    }
141}
142
143#[derive(Debug, Clone, PartialEq, Eq)]
144struct StructureVisitor {
145    signature: Signature,
146}
147
148impl<'de> Visitor<'de> for StructureVisitor {
149    type Value = Structure<'de>;
150
151    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152        formatter.write_str("a Structure value")
153    }
154
155    fn visit_seq<V>(self, visitor: V) -> Result<Structure<'de>, V::Error>
156    where
157        V: SeqAccess<'de>,
158    {
159        SignatureSeed {
160            signature: &self.signature,
161        }
162        .visit_struct(visitor)
163    }
164}
165
166/// A helper type to wrap structs in [`Value`].
167///
168/// API is provided to convert from, and to tuples.
169///
170/// [`Value`]: enum.Value.html
171#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
172pub struct Structure<'a> {
173    fields: Vec<Value<'a>>,
174    signature: Signature,
175}
176
177impl<'a> Structure<'a> {
178    /// Get a reference to all the fields of `self`.
179    pub fn fields(&self) -> &[Value<'a>] {
180        &self.fields
181    }
182
183    /// Converts `self` to a `Vec` containing all its fields.
184    pub fn into_fields(self) -> Vec<Value<'a>> {
185        self.fields
186    }
187
188    /// Get the signature of this `Structure`.
189    pub fn signature(&self) -> &Signature {
190        &self.signature
191    }
192
193    pub(crate) fn try_to_owned(&self) -> crate::Result<Structure<'static>> {
194        Ok(Structure {
195            fields: self
196                .fields
197                .iter()
198                .map(|v| v.try_to_owned().map(Into::into))
199                .collect::<crate::Result<_>>()?,
200            signature: self.signature.to_owned(),
201        })
202    }
203
204    /// Attempt to clone `self`.
205    pub fn try_clone(&self) -> Result<Self, crate::Error> {
206        let fields = self
207            .fields
208            .iter()
209            .map(|v| v.try_clone())
210            .collect::<crate::Result<Vec<_>>>()?;
211
212        Ok(Self {
213            fields,
214            signature: self.signature.clone(),
215        })
216    }
217}
218
219impl Display for Structure<'_> {
220    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
221        structure_display_fmt(self, f, true)
222    }
223}
224
225pub(crate) fn structure_display_fmt(
226    structure: &Structure<'_>,
227    f: &mut std::fmt::Formatter<'_>,
228    type_annotate: bool,
229) -> std::fmt::Result {
230    f.write_char('(')?;
231
232    let fields = structure.fields();
233
234    match fields.len() {
235        0 => {}
236        1 => {
237            value_display_fmt(&fields[0], f, type_annotate)?;
238            f.write_char(',')?;
239        }
240        _ => {
241            for (i, field) in fields.iter().enumerate() {
242                value_display_fmt(field, f, type_annotate)?;
243
244                if i + 1 < fields.len() {
245                    f.write_str(", ")?;
246                }
247            }
248        }
249    }
250
251    f.write_char(')')
252}
253
254impl DynamicType for Structure<'_> {
255    fn signature(&self) -> Signature {
256        self.signature.clone()
257    }
258}
259
260impl DynamicType for StructureSeed<'_> {
261    fn signature(&self) -> Signature {
262        self.signature.clone()
263    }
264}
265
266impl<'a> DynamicDeserialize<'a> for Structure<'a> {
267    type Deserializer = StructureSeed<'static>;
268
269    fn deserializer_for_signature(signature: &Signature) -> zvariant::Result<Self::Deserializer> {
270        let signature = match signature {
271            Signature::Structure(_) => signature.clone(),
272            s => Signature::structure([s.clone()]),
273        };
274
275        Ok(StructureSeed {
276            signature,
277            phantom: std::marker::PhantomData,
278        })
279    }
280}
281
282impl Serialize for Structure<'_> {
283    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
284    where
285        S: Serializer,
286    {
287        let mut structure = serializer.serialize_tuple_struct("Structure", self.fields.len())?;
288        for field in &self.fields {
289            field.serialize_value_as_tuple_struct_field(&mut structure)?;
290        }
291        structure.end()
292    }
293}
294
295macro_rules! tuple_impls {
296    ($($len:expr => ($($n:tt $name:ident)+))+) => {
297        $(
298            impl<'a, $($name),+> From<($($name),+,)> for Structure<'a>
299            where
300                $($name: DynamicType + Into<Value<'a>>,)+
301            {
302                #[inline]
303                fn from(value: ($($name),+,)) -> Self {
304                    StructureBuilder::new()
305                    $(
306                        .add_field(value. $n)
307                    )+
308                    .build().unwrap()
309                }
310            }
311
312            impl<'a, E, $($name),+> TryFrom<Structure<'a>> for ($($name),+,)
313            where
314                $($name: TryFrom<Value<'a>, Error = E>,)+
315                crate::Error: From<E>,
316
317            {
318                type Error = crate::Error;
319
320                fn try_from(mut s: Structure<'a>) -> core::result::Result<Self, Self::Error> {
321                    Ok((
322                    $(
323                         $name::try_from(s.fields.remove(0))?,
324                    )+
325                    ))
326                }
327            }
328
329            impl<'a, E, $($name),+> TryFrom<Value<'a>> for ($($name),+,)
330            where
331                $($name: TryFrom<Value<'a>, Error = E>,)+
332                crate::Error: From<E>,
333
334            {
335                type Error = crate::Error;
336
337                fn try_from(v: Value<'a>) -> core::result::Result<Self, Self::Error> {
338                    Self::try_from(Structure::try_from(v)?)
339                }
340            }
341
342            impl<E, $($name),+> TryFrom<OwnedValue> for ($($name),+,)
343            where
344                $($name: TryFrom<Value<'static>, Error = E>,)+
345                crate::Error: From<E>,
346
347            {
348                type Error = crate::Error;
349
350                fn try_from(v: OwnedValue) -> core::result::Result<Self, Self::Error> {
351                    Self::try_from(Value::from(v))
352                }
353            }
354        )+
355    }
356}
357
358tuple_impls! {
359    1 => (0 T0)
360    2 => (0 T0 1 T1)
361    3 => (0 T0 1 T1 2 T2)
362    4 => (0 T0 1 T1 2 T2 3 T3)
363    5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
364    6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
365    7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
366    8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
367    9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
368    10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
369    11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
370    12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
371    13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
372    14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
373    15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
374    16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
375}
376
377/// Owned [`Structure`]
378#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
379pub struct OwnedStructure(pub Structure<'static>);
380
381/// Use this to deserialize an [`OwnedStructure`].
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub struct OwnedStructureSeed(Signature);
384
385impl DynamicType for OwnedStructure {
386    fn signature(&self) -> Signature {
387        self.0.signature().clone()
388    }
389}
390
391impl DynamicType for OwnedStructureSeed {
392    fn signature(&self) -> Signature {
393        self.0.clone()
394    }
395}
396
397impl DynamicDeserialize<'_> for OwnedStructure {
398    type Deserializer = OwnedStructureSeed;
399
400    fn deserializer_for_signature(signature: &Signature) -> zvariant::Result<Self::Deserializer> {
401        Structure::deserializer_for_signature(signature)
402            .map(|StructureSeed { signature, .. }| OwnedStructureSeed(signature))
403    }
404}
405
406impl<'de> DeserializeSeed<'de> for OwnedStructureSeed {
407    type Value = OwnedStructure;
408    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
409        deserializer
410            .deserialize_seq(StructureVisitor { signature: self.0 })
411            .and_then(|s| match s.try_to_owned() {
412                Ok(s) => Ok(OwnedStructure(s)),
413                Err(e) => Err(D::Error::custom(e)),
414            })
415    }
416}
417
418impl Serialize for OwnedStructure {
419    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
420    where
421        S: Serializer,
422    {
423        self.0.serialize(serializer)
424    }
425}