fidl_fuchsia_data_common/
fidl_fuchsia_data_common.rs

1// WARNING: This file is machine generated by fidlgen.
2
3#![warn(clippy::all)]
4#![allow(unused_parens, unused_mut, unused_imports, nonstandard_style)]
5
6use bitflags::bitflags;
7use fidl::encoding::{MessageBufFor, ProxyChannelBox, ResourceDialect};
8use futures::future::{self, MaybeDone, TryFutureExt};
9use zx_status;
10
11pub const MAX_KEY_LENGTH: u32 = 1024;
12
13pub const MAX_NUM_ENTRIES: u32 = 1024;
14
15pub const MAX_NUM_VALUE_ITEMS: u32 = 1024;
16
17pub const MAX_VALUE_LENGTH: u32 = 32768;
18
19/// A key/value pair in a `Dictionary`.
20#[derive(Clone, Debug, PartialEq)]
21pub struct DictionaryEntry {
22    pub key: String,
23    pub value: Option<Box<DictionaryValue>>,
24}
25
26impl fidl::Persistable for DictionaryEntry {}
27
28/// A dictionary is a sequence of key/value pairs.
29/// Keys must be unique and sorted in lexicographically increasing order.
30#[derive(Clone, Debug, Default, PartialEq)]
31pub struct Dictionary {
32    pub entries: Option<Vec<DictionaryEntry>>,
33    #[doc(hidden)]
34    pub __source_breaking: fidl::marker::SourceBreaking,
35}
36
37impl fidl::Persistable for Dictionary {}
38
39/// A dictionary's value is a string, a list of strings, or a list of objects.
40#[derive(Clone, Debug)]
41pub enum DictionaryValue {
42    Str(String),
43    StrVec(Vec<String>),
44    ObjVec(Vec<Dictionary>),
45    #[doc(hidden)]
46    __SourceBreaking {
47        unknown_ordinal: u64,
48    },
49}
50
51/// Pattern that matches an unknown `DictionaryValue` member.
52#[macro_export]
53macro_rules! DictionaryValueUnknown {
54    () => {
55        _
56    };
57}
58
59// Custom PartialEq so that unknown variants are not equal to themselves.
60impl PartialEq for DictionaryValue {
61    fn eq(&self, other: &Self) -> bool {
62        match (self, other) {
63            (Self::Str(x), Self::Str(y)) => *x == *y,
64            (Self::StrVec(x), Self::StrVec(y)) => *x == *y,
65            (Self::ObjVec(x), Self::ObjVec(y)) => *x == *y,
66            _ => false,
67        }
68    }
69}
70
71impl DictionaryValue {
72    #[inline]
73    pub fn ordinal(&self) -> u64 {
74        match *self {
75            Self::Str(_) => 1,
76            Self::StrVec(_) => 2,
77            Self::ObjVec(_) => 3,
78            Self::__SourceBreaking { unknown_ordinal } => unknown_ordinal,
79        }
80    }
81
82    #[inline]
83    pub fn unknown_variant_for_testing() -> Self {
84        Self::__SourceBreaking { unknown_ordinal: 0 }
85    }
86
87    #[inline]
88    pub fn is_unknown(&self) -> bool {
89        match self {
90            Self::__SourceBreaking { .. } => true,
91            _ => false,
92        }
93    }
94}
95
96impl fidl::Persistable for DictionaryValue {}
97
98mod internal {
99    use super::*;
100
101    impl fidl::encoding::ValueTypeMarker for DictionaryEntry {
102        type Borrowed<'a> = &'a Self;
103        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
104            value
105        }
106    }
107
108    unsafe impl fidl::encoding::TypeMarker for DictionaryEntry {
109        type Owned = Self;
110
111        #[inline(always)]
112        fn inline_align(_context: fidl::encoding::Context) -> usize {
113            8
114        }
115
116        #[inline(always)]
117        fn inline_size(_context: fidl::encoding::Context) -> usize {
118            32
119        }
120    }
121
122    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<DictionaryEntry, D>
123        for &DictionaryEntry
124    {
125        #[inline]
126        unsafe fn encode(
127            self,
128            encoder: &mut fidl::encoding::Encoder<'_, D>,
129            offset: usize,
130            _depth: fidl::encoding::Depth,
131        ) -> fidl::Result<()> {
132            encoder.debug_check_bounds::<DictionaryEntry>(offset);
133            // Delegate to tuple encoding.
134            fidl::encoding::Encode::<DictionaryEntry, D>::encode(
135                (
136                    <fidl::encoding::BoundedString<1024> as fidl::encoding::ValueTypeMarker>::borrow(&self.key),
137                    <fidl::encoding::OptionalUnion<DictionaryValue> as fidl::encoding::ValueTypeMarker>::borrow(&self.value),
138                ),
139                encoder, offset, _depth
140            )
141        }
142    }
143    unsafe impl<
144            D: fidl::encoding::ResourceDialect,
145            T0: fidl::encoding::Encode<fidl::encoding::BoundedString<1024>, D>,
146            T1: fidl::encoding::Encode<fidl::encoding::OptionalUnion<DictionaryValue>, D>,
147        > fidl::encoding::Encode<DictionaryEntry, D> for (T0, T1)
148    {
149        #[inline]
150        unsafe fn encode(
151            self,
152            encoder: &mut fidl::encoding::Encoder<'_, D>,
153            offset: usize,
154            depth: fidl::encoding::Depth,
155        ) -> fidl::Result<()> {
156            encoder.debug_check_bounds::<DictionaryEntry>(offset);
157            // Zero out padding regions. There's no need to apply masks
158            // because the unmasked parts will be overwritten by fields.
159            // Write the fields.
160            self.0.encode(encoder, offset + 0, depth)?;
161            self.1.encode(encoder, offset + 16, depth)?;
162            Ok(())
163        }
164    }
165
166    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for DictionaryEntry {
167        #[inline(always)]
168        fn new_empty() -> Self {
169            Self {
170                key: fidl::new_empty!(fidl::encoding::BoundedString<1024>, D),
171                value: fidl::new_empty!(fidl::encoding::OptionalUnion<DictionaryValue>, D),
172            }
173        }
174
175        #[inline]
176        unsafe fn decode(
177            &mut self,
178            decoder: &mut fidl::encoding::Decoder<'_, D>,
179            offset: usize,
180            _depth: fidl::encoding::Depth,
181        ) -> fidl::Result<()> {
182            decoder.debug_check_bounds::<Self>(offset);
183            // Verify that padding bytes are zero.
184            fidl::decode!(
185                fidl::encoding::BoundedString<1024>,
186                D,
187                &mut self.key,
188                decoder,
189                offset + 0,
190                _depth
191            )?;
192            fidl::decode!(
193                fidl::encoding::OptionalUnion<DictionaryValue>,
194                D,
195                &mut self.value,
196                decoder,
197                offset + 16,
198                _depth
199            )?;
200            Ok(())
201        }
202    }
203
204    impl Dictionary {
205        #[inline(always)]
206        fn max_ordinal_present(&self) -> u64 {
207            if let Some(_) = self.entries {
208                return 1;
209            }
210            0
211        }
212    }
213
214    impl fidl::encoding::ValueTypeMarker for Dictionary {
215        type Borrowed<'a> = &'a Self;
216        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
217            value
218        }
219    }
220
221    unsafe impl fidl::encoding::TypeMarker for Dictionary {
222        type Owned = Self;
223
224        #[inline(always)]
225        fn inline_align(_context: fidl::encoding::Context) -> usize {
226            8
227        }
228
229        #[inline(always)]
230        fn inline_size(_context: fidl::encoding::Context) -> usize {
231            16
232        }
233    }
234
235    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Dictionary, D>
236        for &Dictionary
237    {
238        unsafe fn encode(
239            self,
240            encoder: &mut fidl::encoding::Encoder<'_, D>,
241            offset: usize,
242            mut depth: fidl::encoding::Depth,
243        ) -> fidl::Result<()> {
244            encoder.debug_check_bounds::<Dictionary>(offset);
245            // Vector header
246            let max_ordinal: u64 = self.max_ordinal_present();
247            encoder.write_num(max_ordinal, offset);
248            encoder.write_num(fidl::encoding::ALLOC_PRESENT_U64, offset + 8);
249            // Calling encoder.out_of_line_offset(0) is not allowed.
250            if max_ordinal == 0 {
251                return Ok(());
252            }
253            depth.increment()?;
254            let envelope_size = 8;
255            let bytes_len = max_ordinal as usize * envelope_size;
256            #[allow(unused_variables)]
257            let offset = encoder.out_of_line_offset(bytes_len);
258            let mut _prev_end_offset: usize = 0;
259            if 1 > max_ordinal {
260                return Ok(());
261            }
262
263            // Write at offset+(ordinal-1)*envelope_size, since ordinals are one-based and envelopes
264            // are envelope_size bytes.
265            let cur_offset: usize = (1 - 1) * envelope_size;
266
267            // Zero reserved fields.
268            encoder.padding(offset + _prev_end_offset, cur_offset - _prev_end_offset);
269
270            // Safety:
271            // - bytes_len is calculated to fit envelope_size*max(member.ordinal).
272            // - Since cur_offset is envelope_size*(member.ordinal - 1) and the envelope takes
273            //   envelope_size bytes, there is always sufficient room.
274            fidl::encoding::encode_in_envelope_optional::<fidl::encoding::Vector<DictionaryEntry, 1024>, D>(
275            self.entries.as_ref().map(<fidl::encoding::Vector<DictionaryEntry, 1024> as fidl::encoding::ValueTypeMarker>::borrow),
276            encoder, offset + cur_offset, depth
277        )?;
278
279            _prev_end_offset = cur_offset + envelope_size;
280
281            Ok(())
282        }
283    }
284
285    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for Dictionary {
286        #[inline(always)]
287        fn new_empty() -> Self {
288            Self::default()
289        }
290
291        unsafe fn decode(
292            &mut self,
293            decoder: &mut fidl::encoding::Decoder<'_, D>,
294            offset: usize,
295            mut depth: fidl::encoding::Depth,
296        ) -> fidl::Result<()> {
297            decoder.debug_check_bounds::<Self>(offset);
298            let len = match fidl::encoding::decode_vector_header(decoder, offset)? {
299                None => return Err(fidl::Error::NotNullable),
300                Some(len) => len,
301            };
302            // Calling decoder.out_of_line_offset(0) is not allowed.
303            if len == 0 {
304                return Ok(());
305            };
306            depth.increment()?;
307            let envelope_size = 8;
308            let bytes_len = len * envelope_size;
309            let offset = decoder.out_of_line_offset(bytes_len)?;
310            // Decode the envelope for each type.
311            let mut _next_ordinal_to_read = 0;
312            let mut next_offset = offset;
313            let end_offset = offset + bytes_len;
314            _next_ordinal_to_read += 1;
315            if next_offset >= end_offset {
316                return Ok(());
317            }
318
319            // Decode unknown envelopes for gaps in ordinals.
320            while _next_ordinal_to_read < 1 {
321                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
322                _next_ordinal_to_read += 1;
323                next_offset += envelope_size;
324            }
325
326            let next_out_of_line = decoder.next_out_of_line();
327            let handles_before = decoder.remaining_handles();
328            if let Some((inlined, num_bytes, num_handles)) =
329                fidl::encoding::decode_envelope_header(decoder, next_offset)?
330            {
331                let member_inline_size = <fidl::encoding::Vector<DictionaryEntry, 1024> as fidl::encoding::TypeMarker>::inline_size(decoder.context);
332                if inlined != (member_inline_size <= 4) {
333                    return Err(fidl::Error::InvalidInlineBitInEnvelope);
334                }
335                let inner_offset;
336                let mut inner_depth = depth.clone();
337                if inlined {
338                    decoder.check_inline_envelope_padding(next_offset, member_inline_size)?;
339                    inner_offset = next_offset;
340                } else {
341                    inner_offset = decoder.out_of_line_offset(member_inline_size)?;
342                    inner_depth.increment()?;
343                }
344                let val_ref = self.entries.get_or_insert_with(
345                    || fidl::new_empty!(fidl::encoding::Vector<DictionaryEntry, 1024>, D),
346                );
347                fidl::decode!(fidl::encoding::Vector<DictionaryEntry, 1024>, D, val_ref, decoder, inner_offset, inner_depth)?;
348                if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize)
349                {
350                    return Err(fidl::Error::InvalidNumBytesInEnvelope);
351                }
352                if handles_before != decoder.remaining_handles() + (num_handles as usize) {
353                    return Err(fidl::Error::InvalidNumHandlesInEnvelope);
354                }
355            }
356
357            next_offset += envelope_size;
358
359            // Decode the remaining unknown envelopes.
360            while next_offset < end_offset {
361                _next_ordinal_to_read += 1;
362                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
363                next_offset += envelope_size;
364            }
365
366            Ok(())
367        }
368    }
369
370    impl fidl::encoding::ValueTypeMarker for DictionaryValue {
371        type Borrowed<'a> = &'a Self;
372        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
373            value
374        }
375    }
376
377    unsafe impl fidl::encoding::TypeMarker for DictionaryValue {
378        type Owned = Self;
379
380        #[inline(always)]
381        fn inline_align(_context: fidl::encoding::Context) -> usize {
382            8
383        }
384
385        #[inline(always)]
386        fn inline_size(_context: fidl::encoding::Context) -> usize {
387            16
388        }
389    }
390
391    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<DictionaryValue, D>
392        for &DictionaryValue
393    {
394        #[inline]
395        unsafe fn encode(
396            self,
397            encoder: &mut fidl::encoding::Encoder<'_, D>,
398            offset: usize,
399            _depth: fidl::encoding::Depth,
400        ) -> fidl::Result<()> {
401            encoder.debug_check_bounds::<DictionaryValue>(offset);
402            encoder.write_num::<u64>(self.ordinal(), offset);
403            match self {
404            DictionaryValue::Str(ref val) => {
405                fidl::encoding::encode_in_envelope::<fidl::encoding::BoundedString<32768>, D>(
406                    <fidl::encoding::BoundedString<32768> as fidl::encoding::ValueTypeMarker>::borrow(val),
407                    encoder, offset + 8, _depth
408                )
409            }
410            DictionaryValue::StrVec(ref val) => {
411                fidl::encoding::encode_in_envelope::<fidl::encoding::Vector<fidl::encoding::BoundedString<32768>, 1024>, D>(
412                    <fidl::encoding::Vector<fidl::encoding::BoundedString<32768>, 1024> as fidl::encoding::ValueTypeMarker>::borrow(val),
413                    encoder, offset + 8, _depth
414                )
415            }
416            DictionaryValue::ObjVec(ref val) => {
417                fidl::encoding::encode_in_envelope::<fidl::encoding::Vector<Dictionary, 1024>, D>(
418                    <fidl::encoding::Vector<Dictionary, 1024> as fidl::encoding::ValueTypeMarker>::borrow(val),
419                    encoder, offset + 8, _depth
420                )
421            }
422            DictionaryValue::__SourceBreaking { .. } => Err(fidl::Error::UnknownUnionTag),
423        }
424        }
425    }
426
427    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for DictionaryValue {
428        #[inline(always)]
429        fn new_empty() -> Self {
430            Self::__SourceBreaking { unknown_ordinal: 0 }
431        }
432
433        #[inline]
434        unsafe fn decode(
435            &mut self,
436            decoder: &mut fidl::encoding::Decoder<'_, D>,
437            offset: usize,
438            mut depth: fidl::encoding::Depth,
439        ) -> fidl::Result<()> {
440            decoder.debug_check_bounds::<Self>(offset);
441            #[allow(unused_variables)]
442            let next_out_of_line = decoder.next_out_of_line();
443            let handles_before = decoder.remaining_handles();
444            let (ordinal, inlined, num_bytes, num_handles) =
445                fidl::encoding::decode_union_inline_portion(decoder, offset)?;
446
447            let member_inline_size = match ordinal {
448            1 => <fidl::encoding::BoundedString<32768> as fidl::encoding::TypeMarker>::inline_size(decoder.context),
449            2 => <fidl::encoding::Vector<fidl::encoding::BoundedString<32768>, 1024> as fidl::encoding::TypeMarker>::inline_size(decoder.context),
450            3 => <fidl::encoding::Vector<Dictionary, 1024> as fidl::encoding::TypeMarker>::inline_size(decoder.context),
451            0 => return Err(fidl::Error::UnknownUnionTag),
452            _ => num_bytes as usize,
453        };
454
455            if inlined != (member_inline_size <= 4) {
456                return Err(fidl::Error::InvalidInlineBitInEnvelope);
457            }
458            let _inner_offset;
459            if inlined {
460                decoder.check_inline_envelope_padding(offset + 8, member_inline_size)?;
461                _inner_offset = offset + 8;
462            } else {
463                depth.increment()?;
464                _inner_offset = decoder.out_of_line_offset(member_inline_size)?;
465            }
466            match ordinal {
467                1 => {
468                    #[allow(irrefutable_let_patterns)]
469                    if let DictionaryValue::Str(_) = self {
470                        // Do nothing, read the value into the object
471                    } else {
472                        // Initialize `self` to the right variant
473                        *self = DictionaryValue::Str(fidl::new_empty!(
474                            fidl::encoding::BoundedString<32768>,
475                            D
476                        ));
477                    }
478                    #[allow(irrefutable_let_patterns)]
479                    if let DictionaryValue::Str(ref mut val) = self {
480                        fidl::decode!(
481                            fidl::encoding::BoundedString<32768>,
482                            D,
483                            val,
484                            decoder,
485                            _inner_offset,
486                            depth
487                        )?;
488                    } else {
489                        unreachable!()
490                    }
491                }
492                2 => {
493                    #[allow(irrefutable_let_patterns)]
494                    if let DictionaryValue::StrVec(_) = self {
495                        // Do nothing, read the value into the object
496                    } else {
497                        // Initialize `self` to the right variant
498                        *self = DictionaryValue::StrVec(fidl::new_empty!(
499                            fidl::encoding::Vector<fidl::encoding::BoundedString<32768>, 1024>,
500                            D
501                        ));
502                    }
503                    #[allow(irrefutable_let_patterns)]
504                    if let DictionaryValue::StrVec(ref mut val) = self {
505                        fidl::decode!(
506                            fidl::encoding::Vector<fidl::encoding::BoundedString<32768>, 1024>,
507                            D,
508                            val,
509                            decoder,
510                            _inner_offset,
511                            depth
512                        )?;
513                    } else {
514                        unreachable!()
515                    }
516                }
517                3 => {
518                    #[allow(irrefutable_let_patterns)]
519                    if let DictionaryValue::ObjVec(_) = self {
520                        // Do nothing, read the value into the object
521                    } else {
522                        // Initialize `self` to the right variant
523                        *self = DictionaryValue::ObjVec(
524                            fidl::new_empty!(fidl::encoding::Vector<Dictionary, 1024>, D),
525                        );
526                    }
527                    #[allow(irrefutable_let_patterns)]
528                    if let DictionaryValue::ObjVec(ref mut val) = self {
529                        fidl::decode!(fidl::encoding::Vector<Dictionary, 1024>, D, val, decoder, _inner_offset, depth)?;
530                    } else {
531                        unreachable!()
532                    }
533                }
534                #[allow(deprecated)]
535                ordinal => {
536                    for _ in 0..num_handles {
537                        decoder.drop_next_handle()?;
538                    }
539                    *self = DictionaryValue::__SourceBreaking { unknown_ordinal: ordinal };
540                }
541            }
542            if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize) {
543                return Err(fidl::Error::InvalidNumBytesInEnvelope);
544            }
545            if handles_before != decoder.remaining_handles() + (num_handles as usize) {
546                return Err(fidl::Error::InvalidNumHandlesInEnvelope);
547            }
548            Ok(())
549        }
550    }
551}