1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::{Decode, Encode, EncodingFormat};
use crate::{SharedData, Signature};
use crate::{Variant, VariantError};

/// An ordered collection of items of arbitrary types.
///
/// This is mostly just a way to support custom data structures. In the future, we'll hopefully
/// provide a way to easily map a Rust struct into a `Structure` (probably through an [attribute
/// macro]).
///
/// # Example
///
/// ```
/// use core::convert::TryInto;
///
/// use zvariant::{Array, Decode, Encode, EncodingFormat};
/// use zvariant::Structure;
///
/// // Create a complex structure
/// let top_struct = Structure::new()
///     // top-most simple fields
///     .add_field(u8::max_value())
///     .add_field(u32::max_value())
///     // top-most inner structure
///     .add_field(
///         Structure::new()
///             // 2nd level simple fields
///             .add_field(i64::max_value())
///             .add_field(true)
///             .add_field(i64::max_value())
///             // 2nd level array field
///             .add_field(Array::from(vec!["Hello", "World"])),
///     )
///     // one more top-most simple field
///     .add_field("hello");
/// assert!(top_struct.signature() == "(yu(xbxas)s)");
///
/// // Encode it
/// let format = EncodingFormat::default();
/// let encoding = top_struct.encode(format);
/// assert!(encoding.len() == 70);
///
/// // Then decode it
/// let top_struct = Structure::decode(encoding, top_struct.signature(), format).unwrap();
///
/// // Now check if everything is as expected
/// let fields = top_struct.fields();
///
/// // top-most simple fields
/// assert!(*u8::from_variant(&fields[0]).unwrap() == u8::max_value());
/// assert!(*u32::from_variant(&fields[1]).unwrap() == u32::max_value());
/// assert!(String::from_variant(&fields[3]).unwrap() == "hello");
///
/// // top-most inner structure
/// let inner = Structure::from_variant(&fields[2]).unwrap();
/// let inner_fields = inner.fields();
///
/// // 2nd level simple fields
/// assert!(*i64::from_variant(&inner_fields[0]).unwrap() == i64::max_value());
/// assert!(*bool::from_variant(&inner_fields[1]).unwrap() == true);
///
/// // 2nd level array field
/// let array = Array::from_variant(&inner_fields[3]).unwrap();
/// let as_: Vec<&String> = array.try_into().unwrap();
/// assert!(as_ == ["Hello", "World"]);
/// ```
///
/// [attribute macro]: https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros
#[derive(Debug, Clone, Default)]
pub struct Structure(Vec<Variant>);

impl Structure {
    /// Get all the fields, consuming `self`.
    pub fn take_fields(self) -> Vec<Variant> {
        self.0
    }

    /// Get a reference to all the fields of `self`.
    pub fn fields(&self) -> &[Variant] {
        &self.0
    }

    /// Create a new `Structure`.
    ///
    /// Same as `Structure::default()`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Append `field` to `self`.
    ///
    /// This method returns `Self` so that you can use the builder pattern to create a complex
    /// structure.
    pub fn add_field<T>(mut self, field: T) -> Self
    where
        T: Encode,
    {
        self.0.push(field.to_variant());

        self
    }
}

impl Encode for Structure {
    // The real single character signature for STRUCT is `r` but that's not actually used in practice for D-Bus at least
    // (the spec clearly states that this signature must never appear on the bus). The openning and closing braces are
    // used in practice and that's why we'll declare the opening brace as the signature for this type.
    const SIGNATURE_CHAR: char = '(';
    const SIGNATURE_STR: &'static str = "(";
    const ALIGNMENT: usize = 8;

    fn encode_into(&self, bytes: &mut Vec<u8>, format: EncodingFormat) {
        Self::add_padding(bytes, format);

        // Since a Structure always starts at 8-byte boundry, the fields and their children are
        // already aligned correctly.
        for field in &self.0 {
            field.encode_value_into(bytes, format);
        }
    }

    fn signature(&self) -> Signature {
        let mut signature = String::from("(");
        for field in &self.0 {
            signature.push_str(&field.value_signature());
        }
        signature.push_str(")");
        Signature::from(signature)
    }

    fn to_variant(self) -> Variant {
        Variant::Structure(self)
    }

    fn is(variant: &Variant) -> bool {
        if let Variant::Structure(_) = variant {
            true
        } else {
            false
        }
    }
}

impl Decode for Structure {
    fn slice_data(
        data: impl Into<SharedData>,
        signature: impl Into<Signature>,
        format: EncodingFormat,
    ) -> Result<SharedData, VariantError> {
        let data = data.into();
        let padding = Self::padding(data.position(), format);
        if data.len() < padding {
            return Err(VariantError::InsufficientData);
        }
        let signature = Self::ensure_correct_signature(signature)?;

        let mut extracted = padding;
        let mut i = 1;
        let last_index = signature.len() - 1;
        while i < last_index {
            let child_signature = crate::decode::slice_signature(&signature[i..last_index])?;
            let slice = crate::decode::slice_data(
                data.tail(extracted as usize),
                child_signature.as_str(),
                format,
            )?;
            extracted += slice.len();
            if extracted > data.len() {
                return Err(VariantError::InsufficientData);
            }

            i += child_signature.len();
        }
        if extracted == 0 {
            return Err(VariantError::ExcessData);
        }

        Ok(data.head(extracted))
    }

    fn decode(
        data: impl Into<SharedData>,
        signature: impl Into<Signature>,
        format: EncodingFormat,
    ) -> Result<Self, VariantError> {
        // Similar to slice_data, except we create variants.
        let data = data.into();
        let padding = Self::padding(data.position(), format);
        let signature = signature.into();
        if data.len() < padding || signature.len() < 3 {
            return Err(VariantError::InsufficientData);
        }
        let signature = Self::ensure_correct_signature(signature)?;

        let encoding = data.tail(padding);
        let fields = variants_from_struct_data(&encoding, signature, format)?;

        Ok(Self(fields))
    }

    fn ensure_correct_signature(
        signature: impl Into<Signature>,
    ) -> Result<Signature, VariantError> {
        let signature = signature.into();
        if signature.len() < 3 {
            return Err(VariantError::InsufficientData);
        }
        if !signature.starts_with('(') || !signature.ends_with(')') {
            return Err(VariantError::IncorrectType);
        }

        let mut i = 1;
        while i < signature.len() - 1 {
            // Ensure we've only valid child signatures
            let child_signature = crate::decode::slice_signature(&signature[i..])?;
            i += child_signature.len();
        }

        Ok(signature)
    }

    fn slice_signature(signature: impl Into<Signature>) -> Result<Signature, VariantError> {
        let signature = signature.into();
        if !signature.starts_with('(') {
            return Err(VariantError::IncorrectType);
        }

        let mut open_braces = 1;
        let mut i = 1;
        while i < signature.len() {
            if &signature[i..=i] == ")" {
                open_braces -= 1;

                if open_braces == 0 {
                    break;
                }
            } else if &signature[i..=i] == "(" {
                open_braces += 1;
            }

            i += 1;
        }
        if &signature[i..=i] != ")" {
            return Err(VariantError::IncorrectType);
        }

        Ok(Signature::from(&signature[0..=i]))
    }

    fn take_from_variant(variant: Variant) -> Result<Self, VariantError> {
        if let Variant::Structure(value) = variant {
            Ok(value)
        } else {
            Err(VariantError::IncorrectType)
        }
    }

    fn from_variant(variant: &Variant) -> Result<&Self, VariantError> {
        if let Variant::Structure(value) = variant {
            Ok(value)
        } else {
            Err(VariantError::IncorrectType)
        }
    }
}

fn variants_from_struct_data(
    data: &SharedData,
    signature: impl Into<Signature>,
    format: EncodingFormat,
) -> Result<Vec<Variant>, VariantError> {
    // Assuming simple types here but it's OK to have more capacity than needed
    let signature = signature.into();
    let mut fields = Vec::with_capacity(signature.len());
    let mut extracted = 0;
    let mut i = 1;
    let last_index = signature.len() - 1;
    while i < last_index {
        let child_signature = crate::slice_signature(&signature[i..last_index])?;

        let child_slice =
            crate::decode::slice_data(data.tail(extracted), child_signature.as_str(), format)?;
        extracted += child_slice.len();
        if extracted > data.len() {
            return Err(VariantError::InsufficientData);
        }
        let variant = Variant::from_data_slice(child_slice, child_signature.as_str(), format)?;
        fields.push(variant);

        i += child_signature.len();
    }
    if extracted == 0 {
        return Err(VariantError::ExcessData);
    }

    Ok(fields)
}