Skip to main content

matter_kit/clusters/generated/
globals.rs

1// Generated by `cargo xtask` from the CSA data model 1.6. Do not edit.
2//
3// Globals — cluster 0xFFFF0000.
4//
5// Regenerate with `cargo xtask clusters --dm <checkout>/data_model/1.6`; `cargo xtask check`
6// is the CI gate that keeps this file honest. The XML itself is not vendored here — it
7// carries the CSA's own copyright notice and is not redistributable.
8
9//! Globals (cluster `0xFFFF0000`, revision 1).
10//!
11//! 0 identifiers, 0 feature bits, and the specification's own conformance for
12//! every element. `CLUSTER` is the whole table.
13
14#![allow(unused_imports)]
15
16use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
17use crate::dm::meta::{EventPriority, Reporting};
18use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
19use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
20use crate::error::Result;
21use crate::im::{AttributeId, ClusterId, CommandId, EventId};
22use crate::tlv::{
23    ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
24};
25
26/// The cluster id.
27pub const ID: ClusterId = 0xFFFF0000;
28
29/// The highest revision the specification's history defines — what `ClusterRevision`
30/// must report (Core §7.13.1).
31pub const REVISION: u16 = 1;
32
33/// This cluster defines no features, so its `FeatureMap` is always zero.
34pub const FEATURES: &[Feature] = &[];
35
36/// `AtomicRequestTypeEnum`.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
38#[repr(u8)]
39pub enum AtomicRequestTypeEnum {
40    /// Begin an atomic write
41    BeginWrite = 0,
42    /// Commit an atomic write
43    CommitWrite = 1,
44    /// Rollback an atomic write, discarding any pending changes
45    RollbackWrite = 2,
46}
47
48impl AtomicRequestTypeEnum {
49    /// Reads a wire value, or `None` for one this revision does not define.
50    #[must_use]
51    pub const fn from_value(value: u8) -> Option<Self> {
52        match value {
53            0 => Some(Self::BeginWrite),
54            1 => Some(Self::CommitWrite),
55            2 => Some(Self::RollbackWrite),
56            _ => None,
57        }
58    }
59
60    /// Its wire value.
61    #[must_use]
62    pub const fn value(self) -> u8 {
63        self as u8
64    }
65}
66
67impl ToTlv for AtomicRequestTypeEnum {
68    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
69        self.value().to_tlv(w, tag)
70    }
71}
72
73impl<'a> FromTlv<'a> for AtomicRequestTypeEnum {
74    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
75        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
76        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
77    }
78}
79
80/// `CertificationTypeEnum`.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
82#[repr(u8)]
83pub enum CertificationTypeEnum {
84    /// DeviceAttestationPKI
85    DeviceAttestationPKI = 0,
86    /// OperationalPKI
87    OperationalPKI = 1,
88    /// VIDSignerPKI
89    VIDSignerPKI = 2,
90}
91
92impl CertificationTypeEnum {
93    /// Reads a wire value, or `None` for one this revision does not define.
94    #[must_use]
95    pub const fn from_value(value: u8) -> Option<Self> {
96        match value {
97            0 => Some(Self::DeviceAttestationPKI),
98            1 => Some(Self::OperationalPKI),
99            2 => Some(Self::VIDSignerPKI),
100            _ => None,
101        }
102    }
103
104    /// Its wire value.
105    #[must_use]
106    pub const fn value(self) -> u8 {
107        self as u8
108    }
109}
110
111impl ToTlv for CertificationTypeEnum {
112    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
113        self.value().to_tlv(w, tag)
114    }
115}
116
117impl<'a> FromTlv<'a> for CertificationTypeEnum {
118    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
119        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
120        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
121    }
122}
123
124/// `MeasurementTypeEnum`.
125#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
126#[repr(u8)]
127pub enum MeasurementTypeEnum {
128    /// `Unspecified`.
129    Unspecified = 0,
130    /// Voltage in millivolts (mV)
131    Voltage = 1,
132    /// Active current in milliamps (mA)
133    ActiveCurrent = 2,
134    /// Reactive current in milliamps (mA)
135    ReactiveCurrent = 3,
136    /// Apparent current in milliamps (mA)
137    ApparentCurrent = 4,
138    /// Active power in milliwatts (mW)
139    ActivePower = 5,
140    /// Reactive power in millivolt-amps reactive (mVAR)
141    ReactivePower = 6,
142    /// Apparent power in millivolt-amps (mVA)
143    ApparentPower = 7,
144    /// Root mean squared voltage in millivolts (mV)
145    RMSVoltage = 8,
146    /// Root mean squared current in milliamps (mA)
147    RMSCurrent = 9,
148    /// Root mean squared power in milliwatts (mW)
149    RMSPower = 10,
150    /// AC frequency in millihertz (mHz)
151    Frequency = 11,
152    /// Power Factor ratio in +/- 1/100ths of a percent.
153    PowerFactor = 12,
154    /// AC neutral current in milliamps (mA)
155    NeutralCurrent = 13,
156    /// Electrical energy in milliwatt-hours (mWh)
157    ElectricalEnergy = 14,
158    /// Reactive power in millivolt-amp-hours reactive (mVARh)
159    ReactiveEnergy = 15,
160    /// Apparent power in millivolt-amp-hours (mVAh)
161    ApparentEnergy = 16,
162    /// Soil moisture in percent
163    SoilMoisture = 17,
164}
165
166impl MeasurementTypeEnum {
167    /// Reads a wire value, or `None` for one this revision does not define.
168    #[must_use]
169    pub const fn from_value(value: u8) -> Option<Self> {
170        match value {
171            0 => Some(Self::Unspecified),
172            1 => Some(Self::Voltage),
173            2 => Some(Self::ActiveCurrent),
174            3 => Some(Self::ReactiveCurrent),
175            4 => Some(Self::ApparentCurrent),
176            5 => Some(Self::ActivePower),
177            6 => Some(Self::ReactivePower),
178            7 => Some(Self::ApparentPower),
179            8 => Some(Self::RMSVoltage),
180            9 => Some(Self::RMSCurrent),
181            10 => Some(Self::RMSPower),
182            11 => Some(Self::Frequency),
183            12 => Some(Self::PowerFactor),
184            13 => Some(Self::NeutralCurrent),
185            14 => Some(Self::ElectricalEnergy),
186            15 => Some(Self::ReactiveEnergy),
187            16 => Some(Self::ApparentEnergy),
188            17 => Some(Self::SoilMoisture),
189            _ => None,
190        }
191    }
192
193    /// Its wire value.
194    #[must_use]
195    pub const fn value(self) -> u8 {
196        self as u8
197    }
198}
199
200impl ToTlv for MeasurementTypeEnum {
201    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
202        self.value().to_tlv(w, tag)
203    }
204}
205
206impl<'a> FromTlv<'a> for MeasurementTypeEnum {
207    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
208        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
209        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
210    }
211}
212
213/// `PowerThresholdSourceEnum`.
214#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
215#[repr(u8)]
216pub enum PowerThresholdSourceEnum {
217    /// The power threshold comes from a signed contract
218    Contract = 0,
219    /// The power threshold comes from a legal regulator
220    Regulator = 1,
221    /// The power threshold comes from a certified limits of the meter
222    Equipment = 2,
223}
224
225impl PowerThresholdSourceEnum {
226    /// Reads a wire value, or `None` for one this revision does not define.
227    #[must_use]
228    pub const fn from_value(value: u8) -> Option<Self> {
229        match value {
230            0 => Some(Self::Contract),
231            1 => Some(Self::Regulator),
232            2 => Some(Self::Equipment),
233            _ => None,
234        }
235    }
236
237    /// Its wire value.
238    #[must_use]
239    pub const fn value(self) -> u8 {
240        self as u8
241    }
242}
243
244impl ToTlv for PowerThresholdSourceEnum {
245    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
246        self.value().to_tlv(w, tag)
247    }
248}
249
250impl<'a> FromTlv<'a> for PowerThresholdSourceEnum {
251    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
252        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
253        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
254    }
255}
256
257/// `SoftwareVersionCertificationStatusEnum`.
258#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
259#[repr(u8)]
260pub enum SoftwareVersionCertificationStatusEnum {
261    /// dev-test
262    DevTest = 0,
263    /// provisional
264    Provisional = 1,
265    /// certified
266    Certified = 2,
267    /// revoked
268    Revoked = 3,
269}
270
271impl SoftwareVersionCertificationStatusEnum {
272    /// Reads a wire value, or `None` for one this revision does not define.
273    #[must_use]
274    pub const fn from_value(value: u8) -> Option<Self> {
275        match value {
276            0 => Some(Self::DevTest),
277            1 => Some(Self::Provisional),
278            2 => Some(Self::Certified),
279            3 => Some(Self::Revoked),
280            _ => None,
281        }
282    }
283
284    /// Its wire value.
285    #[must_use]
286    pub const fn value(self) -> u8 {
287        self as u8
288    }
289}
290
291impl ToTlv for SoftwareVersionCertificationStatusEnum {
292    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
293        self.value().to_tlv(w, tag)
294    }
295}
296
297impl<'a> FromTlv<'a> for SoftwareVersionCertificationStatusEnum {
298    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
299        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
300        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
301    }
302}
303
304/// `StreamUsageEnum`.
305#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
306#[repr(u8)]
307pub enum StreamUsageEnum {
308    /// Internal video stream.
309    Internal = 0,
310    /// Stream for recording clips.
311    Recording = 1,
312    /// Stream for analysis and entity detection.
313    Analysis = 2,
314    /// Stream for liveview.
315    LiveView = 3,
316}
317
318impl StreamUsageEnum {
319    /// Reads a wire value, or `None` for one this revision does not define.
320    #[must_use]
321    pub const fn from_value(value: u8) -> Option<Self> {
322        match value {
323            0 => Some(Self::Internal),
324            1 => Some(Self::Recording),
325            2 => Some(Self::Analysis),
326            3 => Some(Self::LiveView),
327            _ => None,
328        }
329    }
330
331    /// Its wire value.
332    #[must_use]
333    pub const fn value(self) -> u8 {
334        self as u8
335    }
336}
337
338impl ToTlv for StreamUsageEnum {
339    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
340        self.value().to_tlv(w, tag)
341    }
342}
343
344impl<'a> FromTlv<'a> for StreamUsageEnum {
345    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
346        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
347        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
348    }
349}
350
351/// `TariffPriceTypeEnum`.
352#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
353#[repr(u8)]
354pub enum TariffPriceTypeEnum {
355    /// Standard tariff price
356    Standard = 0,
357    /// Price during CPP events
358    Critical = 1,
359    /// Price during VPP events
360    Virtual = 2,
361    /// Price incentives
362    Incentive = 3,
363    /// Price incentive signals
364    IncentiveSignal = 4,
365}
366
367impl TariffPriceTypeEnum {
368    /// Reads a wire value, or `None` for one this revision does not define.
369    #[must_use]
370    pub const fn from_value(value: u8) -> Option<Self> {
371        match value {
372            0 => Some(Self::Standard),
373            1 => Some(Self::Critical),
374            2 => Some(Self::Virtual),
375            3 => Some(Self::Incentive),
376            4 => Some(Self::IncentiveSignal),
377            _ => None,
378        }
379    }
380
381    /// Its wire value.
382    #[must_use]
383    pub const fn value(self) -> u8 {
384        self as u8
385    }
386}
387
388impl ToTlv for TariffPriceTypeEnum {
389    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
390        self.value().to_tlv(w, tag)
391    }
392}
393
394impl<'a> FromTlv<'a> for TariffPriceTypeEnum {
395    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
396        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
397        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
398    }
399}
400
401/// `TariffUnitEnum`.
402#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
403#[repr(u8)]
404pub enum TariffUnitEnum {
405    /// Kilowatt Hours
406    KWh = 0,
407    /// Kilovolt-amp Hours
408    KVAh = 1,
409}
410
411impl TariffUnitEnum {
412    /// Reads a wire value, or `None` for one this revision does not define.
413    #[must_use]
414    pub const fn from_value(value: u8) -> Option<Self> {
415        match value {
416            0 => Some(Self::KWh),
417            1 => Some(Self::KVAh),
418            _ => None,
419        }
420    }
421
422    /// Its wire value.
423    #[must_use]
424    pub const fn value(self) -> u8 {
425        self as u8
426    }
427}
428
429impl ToTlv for TariffUnitEnum {
430    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
431        self.value().to_tlv(w, tag)
432    }
433}
434
435impl<'a> FromTlv<'a> for TariffUnitEnum {
436    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
437        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
438        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
439    }
440}
441
442/// `ThreeLevelAutoEnum`.
443#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
444#[repr(u8)]
445pub enum ThreeLevelAutoEnum {
446    /// Automatic Level
447    Auto = 0,
448    /// Low Level
449    Low = 1,
450    /// Medium Level
451    Medium = 2,
452    /// High Level
453    High = 3,
454}
455
456impl ThreeLevelAutoEnum {
457    /// Reads a wire value, or `None` for one this revision does not define.
458    #[must_use]
459    pub const fn from_value(value: u8) -> Option<Self> {
460        match value {
461            0 => Some(Self::Auto),
462            1 => Some(Self::Low),
463            2 => Some(Self::Medium),
464            3 => Some(Self::High),
465            _ => None,
466        }
467    }
468
469    /// Its wire value.
470    #[must_use]
471    pub const fn value(self) -> u8 {
472        self as u8
473    }
474}
475
476impl ToTlv for ThreeLevelAutoEnum {
477    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
478        self.value().to_tlv(w, tag)
479    }
480}
481
482impl<'a> FromTlv<'a> for ThreeLevelAutoEnum {
483    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
484        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
485        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
486    }
487}
488
489/// `WebRTCEndReasonEnum`.
490#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
491#[repr(u8)]
492pub enum WebRTCEndReasonEnum {
493    /// No media connection could be established to the other party
494    ICEFailed = 0,
495    /// The call timed out whilst waiting for ICE candidate gathering to complete
496    ICETimeout = 1,
497    /// The user chose to end the call
498    UserHangup = 2,
499    /// The remote party is busy
500    UserBusy = 3,
501    /// The call was replaced by another call
502    Replaced = 4,
503    /// An error code when there is no local mic/camera to use. This may be because the hardware isn't plugged in, or the user has explicitly denied access
504    NoUserMedia = 5,
505    /// The call timed out whilst waiting for the offer/answer step to complete
506    InviteTimeout = 6,
507    /// The call was answered from a different device
508    AnsweredElsewhere = 7,
509    /// The was unable to continue due to not enough resources or available streams
510    OutOfResources = 8,
511    /// The call ended due to a media timeout
512    MediaTimeout = 9,
513    /// The call ended due to hitting a low power condition
514    LowPower = 10,
515    /// The call ended due to the camera being set into a privacy mode.
516    PrivacyMode = 11,
517    /// Unknown or unspecified reason
518    UnknownReason = 12,
519}
520
521impl WebRTCEndReasonEnum {
522    /// Reads a wire value, or `None` for one this revision does not define.
523    #[must_use]
524    pub const fn from_value(value: u8) -> Option<Self> {
525        match value {
526            0 => Some(Self::ICEFailed),
527            1 => Some(Self::ICETimeout),
528            2 => Some(Self::UserHangup),
529            3 => Some(Self::UserBusy),
530            4 => Some(Self::Replaced),
531            5 => Some(Self::NoUserMedia),
532            6 => Some(Self::InviteTimeout),
533            7 => Some(Self::AnsweredElsewhere),
534            8 => Some(Self::OutOfResources),
535            9 => Some(Self::MediaTimeout),
536            10 => Some(Self::LowPower),
537            11 => Some(Self::PrivacyMode),
538            12 => Some(Self::UnknownReason),
539            _ => None,
540        }
541    }
542
543    /// Its wire value.
544    #[must_use]
545    pub const fn value(self) -> u8 {
546        self as u8
547    }
548}
549
550impl ToTlv for WebRTCEndReasonEnum {
551    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
552        self.value().to_tlv(w, tag)
553    }
554}
555
556impl<'a> FromTlv<'a> for WebRTCEndReasonEnum {
557    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
558        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
559        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
560    }
561}
562
563bitflags::bitflags! {
564    /// `WildcardPathFlagsBitmap`.
565    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
566    pub struct WildcardPathFlagsBitmap: u16 {
567        /// Skip the Root Node endpoint (endpoint 0) during wildcard expansion.
568        const WILDCARD_SKIP_ROOT_NODE = 1 << 0;
569        /// Skip several large global attributes during wildcard expansion.
570        const WILDCARD_SKIP_GLOBAL_ATTRIBUTES = 1 << 1;
571        /// Skip the AttributeList global attribute during wildcard expansion.
572        const WILDCARD_SKIP_ATTRIBUTE_LIST = 1 << 2;
573        /// `DoNotUse`, bit 3.
574        const DO_NOT_USE = 1 << 3;
575        /// Skip the AcceptedCommandList and GeneratedCommandList global attributes during wildcard expansion.
576        const WILDCARD_SKIP_COMMAND_LISTS = 1 << 4;
577        /// Skip any manufacturer-specific clusters or attributes during wildcard expansion.
578        const WILDCARD_SKIP_CUSTOM_ELEMENTS = 1 << 5;
579        /// Skip any Fixed (F) quality attributes during wildcard expansion.
580        const WILDCARD_SKIP_FIXED_ATTRIBUTES = 1 << 6;
581        /// Skip any Changes Omitted ++(C)++ quality attributes during wildcard expansion.
582        const WILDCARD_SKIP_CHANGES_OMITTED_ATTRIBUTES = 1 << 7;
583        /// Skip all clusters with the Diagnostics (K) quality during wildcard expansion.
584        const WILDCARD_SKIP_DIAGNOSTICS_CLUSTERS = 1 << 8;
585    }
586}
587
588impl ToTlv for WildcardPathFlagsBitmap {
589    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
590        self.bits().to_tlv(w, tag)
591    }
592}
593
594impl<'a> FromTlv<'a> for WildcardPathFlagsBitmap {
595    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
596        let bits = <u16 as FromTlv<'a>>::from_tlv(reader, element)?;
597        Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
598    }
599}
600
601/// `AtomicAttributeStatusStruct`.
602#[derive(Debug, Clone, Copy, PartialEq)]
603pub struct AtomicAttributeStatusStruct {
604    /// `AttributeID`, field 0.
605    pub attribute_id: u32,
606    /// `StatusCode`, field 1.
607    pub status_code: u8,
608}
609
610impl ToTlv for AtomicAttributeStatusStruct {
611    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
612        w.start_structure(tag)?;
613        self.attribute_id.to_tlv(w, Tag::Context(0))?;
614        self.status_code.to_tlv(w, Tag::Context(1))?;
615        w.end_container()
616    }
617}
618
619impl<'a> FromTlv<'a> for AtomicAttributeStatusStruct {
620    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
621        if element.value.container() != Some(ContainerKind::Structure) {
622            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
623        }
624        let mut attribute_id = None;
625        let mut status_code = None;
626        loop {
627            let Some(field) = reader.next_element()? else {
628                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
629            };
630            if field.value == Value::EndOfContainer {
631                break;
632            }
633            match field.tag.context() {
634                Some(0) => attribute_id = Some(<u32>::from_tlv(reader, &field)?),
635                Some(1) => status_code = Some(<u8>::from_tlv(reader, &field)?),
636                // A field this revision does not define. §7.19.2: a receiver
637                // ignores what it does not know rather than refusing the whole
638                // structure, which is what lets a newer peer talk to an older one.
639                _ => reader.skip_value(&field)?,
640            }
641        }
642        Ok(Self {
643            attribute_id: attribute_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
644            status_code: status_code.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
645        })
646    }
647}
648
649/// `CurrencyStruct`.
650#[derive(Debug, Clone, Copy, PartialEq)]
651pub struct CurrencyStruct {
652    /// `Currency`, field 0.
653    pub currency: u16,
654    /// `DecimalPoints`, field 1.
655    pub decimal_points: u8,
656}
657
658impl ToTlv for CurrencyStruct {
659    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
660        w.start_structure(tag)?;
661        self.currency.to_tlv(w, Tag::Context(0))?;
662        self.decimal_points.to_tlv(w, Tag::Context(1))?;
663        w.end_container()
664    }
665}
666
667impl<'a> FromTlv<'a> for CurrencyStruct {
668    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
669        if element.value.container() != Some(ContainerKind::Structure) {
670            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
671        }
672        let mut currency = None;
673        let mut decimal_points = None;
674        loop {
675            let Some(field) = reader.next_element()? else {
676                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
677            };
678            if field.value == Value::EndOfContainer {
679                break;
680            }
681            match field.tag.context() {
682                Some(0) => currency = Some(<u16>::from_tlv(reader, &field)?),
683                Some(1) => decimal_points = Some(<u8>::from_tlv(reader, &field)?),
684                // A field this revision does not define. §7.19.2: a receiver
685                // ignores what it does not know rather than refusing the whole
686                // structure, which is what lets a newer peer talk to an older one.
687                _ => reader.skip_value(&field)?,
688            }
689        }
690        Ok(Self {
691            currency: currency.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
692            decimal_points: decimal_points
693                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
694        })
695    }
696}
697
698/// `ICECandidateStruct`.
699#[derive(Debug, Clone, Copy, PartialEq)]
700pub struct ICECandidateStruct<'a> {
701    /// `Candidate`, field 0.
702    pub candidate: &'a str,
703    /// `SDPMid`, field 1.
704    pub sdp_mid: Nullable<&'a str>,
705    /// `SDPMLineIndex`, field 2.
706    pub sdpm_line_index: Nullable<u16>,
707}
708
709impl<'a> ToTlv for ICECandidateStruct<'a> {
710    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
711        w.start_structure(tag)?;
712        self.candidate.to_tlv(w, Tag::Context(0))?;
713        self.sdp_mid.to_tlv(w, Tag::Context(1))?;
714        self.sdpm_line_index.to_tlv(w, Tag::Context(2))?;
715        w.end_container()
716    }
717}
718
719impl<'a> FromTlv<'a> for ICECandidateStruct<'a> {
720    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
721        if element.value.container() != Some(ContainerKind::Structure) {
722            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
723        }
724        let mut candidate = None;
725        let mut sdp_mid = None;
726        let mut sdpm_line_index = None;
727        loop {
728            let Some(field) = reader.next_element()? else {
729                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
730            };
731            if field.value == Value::EndOfContainer {
732                break;
733            }
734            match field.tag.context() {
735                Some(0) => candidate = Some(<&'a str>::from_tlv(reader, &field)?),
736                Some(1) => sdp_mid = Some(<Nullable<&'a str>>::from_tlv(reader, &field)?),
737                Some(2) => sdpm_line_index = Some(<Nullable<u16>>::from_tlv(reader, &field)?),
738                // A field this revision does not define. §7.19.2: a receiver
739                // ignores what it does not know rather than refusing the whole
740                // structure, which is what lets a newer peer talk to an older one.
741                _ => reader.skip_value(&field)?,
742            }
743        }
744        Ok(Self {
745            candidate: candidate.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
746            sdp_mid: sdp_mid.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
747            sdpm_line_index: sdpm_line_index
748                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
749        })
750    }
751}
752
753/// `ICEServerStruct`.
754#[derive(Debug, Clone, Copy, PartialEq)]
755pub struct ICEServerStruct<'a> {
756    /// `URLs`, field 0.
757    pub ur_ls: TlvList<'a, &'a str>,
758    /// `Username`, field 1.
759    pub username: Option<&'a str>,
760    /// `Credential`, field 2.
761    pub credential: Option<&'a str>,
762    /// `CAID`, field 3.
763    pub caid: Option<u16>,
764}
765
766impl<'a> ToTlv for ICEServerStruct<'a> {
767    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
768        w.start_structure(tag)?;
769        self.ur_ls.to_tlv(w, Tag::Context(0))?;
770        if let Some(value) = &self.username {
771            value.to_tlv(w, Tag::Context(1))?;
772        }
773        if let Some(value) = &self.credential {
774            value.to_tlv(w, Tag::Context(2))?;
775        }
776        if let Some(value) = &self.caid {
777            value.to_tlv(w, Tag::Context(3))?;
778        }
779        w.end_container()
780    }
781}
782
783impl<'a> FromTlv<'a> for ICEServerStruct<'a> {
784    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
785        if element.value.container() != Some(ContainerKind::Structure) {
786            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
787        }
788        let mut ur_ls = None;
789        let mut username = None;
790        let mut credential = None;
791        let mut caid = None;
792        loop {
793            let Some(field) = reader.next_element()? else {
794                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
795            };
796            if field.value == Value::EndOfContainer {
797                break;
798            }
799            match field.tag.context() {
800                Some(0) => ur_ls = Some(<TlvList<'a, &'a str>>::from_tlv(reader, &field)?),
801                Some(1) => username = Some(<&'a str>::from_tlv(reader, &field)?),
802                Some(2) => credential = Some(<&'a str>::from_tlv(reader, &field)?),
803                Some(3) => caid = Some(<u16>::from_tlv(reader, &field)?),
804                // A field this revision does not define. §7.19.2: a receiver
805                // ignores what it does not know rather than refusing the whole
806                // structure, which is what lets a newer peer talk to an older one.
807                _ => reader.skip_value(&field)?,
808            }
809        }
810        Ok(Self {
811            ur_ls: ur_ls.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
812            username,
813            credential,
814            caid,
815        })
816    }
817}
818
819/// `LocationDescriptorStruct`.
820#[derive(Debug, Clone, Copy, PartialEq)]
821pub struct LocationDescriptorStruct<'a> {
822    /// `LocationName`, field 0.
823    pub location_name: &'a str,
824    /// `FloorNumber`, field 1.
825    pub floor_number: Nullable<i16>,
826    /// `AreaType`, field 2.
827    pub area_type: Nullable<u8>,
828}
829
830impl<'a> ToTlv for LocationDescriptorStruct<'a> {
831    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
832        w.start_structure(tag)?;
833        self.location_name.to_tlv(w, Tag::Context(0))?;
834        self.floor_number.to_tlv(w, Tag::Context(1))?;
835        self.area_type.to_tlv(w, Tag::Context(2))?;
836        w.end_container()
837    }
838}
839
840impl<'a> FromTlv<'a> for LocationDescriptorStruct<'a> {
841    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
842        if element.value.container() != Some(ContainerKind::Structure) {
843            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
844        }
845        let mut location_name = None;
846        let mut floor_number = None;
847        let mut area_type = None;
848        loop {
849            let Some(field) = reader.next_element()? else {
850                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
851            };
852            if field.value == Value::EndOfContainer {
853                break;
854            }
855            match field.tag.context() {
856                Some(0) => location_name = Some(<&'a str>::from_tlv(reader, &field)?),
857                Some(1) => floor_number = Some(<Nullable<i16>>::from_tlv(reader, &field)?),
858                Some(2) => area_type = Some(<Nullable<u8>>::from_tlv(reader, &field)?),
859                // A field this revision does not define. §7.19.2: a receiver
860                // ignores what it does not know rather than refusing the whole
861                // structure, which is what lets a newer peer talk to an older one.
862                _ => reader.skip_value(&field)?,
863            }
864        }
865        Ok(Self {
866            location_name: location_name.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
867            floor_number: floor_number.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
868            area_type: area_type.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
869        })
870    }
871}
872
873/// `MeasurementAccuracyRangeStruct`.
874#[derive(Debug, Clone, Copy, PartialEq)]
875pub struct MeasurementAccuracyRangeStruct {
876    /// `RangeMin`, field 0.
877    pub range_min: i64,
878    /// `RangeMax`, field 1.
879    pub range_max: i64,
880    /// `PercentMax`, field 2.
881    pub percent_max: Option<u16>,
882    /// `PercentMin`, field 3.
883    pub percent_min: Option<u16>,
884    /// `PercentTypical`, field 4.
885    pub percent_typical: Option<u16>,
886    /// `FixedMax`, field 5.
887    pub fixed_max: Option<u64>,
888    /// `FixedMin`, field 6.
889    pub fixed_min: Option<u64>,
890    /// `FixedTypical`, field 7.
891    pub fixed_typical: Option<u64>,
892}
893
894impl ToTlv for MeasurementAccuracyRangeStruct {
895    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
896        w.start_structure(tag)?;
897        self.range_min.to_tlv(w, Tag::Context(0))?;
898        self.range_max.to_tlv(w, Tag::Context(1))?;
899        if let Some(value) = &self.percent_max {
900            value.to_tlv(w, Tag::Context(2))?;
901        }
902        if let Some(value) = &self.percent_min {
903            value.to_tlv(w, Tag::Context(3))?;
904        }
905        if let Some(value) = &self.percent_typical {
906            value.to_tlv(w, Tag::Context(4))?;
907        }
908        if let Some(value) = &self.fixed_max {
909            value.to_tlv(w, Tag::Context(5))?;
910        }
911        if let Some(value) = &self.fixed_min {
912            value.to_tlv(w, Tag::Context(6))?;
913        }
914        if let Some(value) = &self.fixed_typical {
915            value.to_tlv(w, Tag::Context(7))?;
916        }
917        w.end_container()
918    }
919}
920
921impl<'a> FromTlv<'a> for MeasurementAccuracyRangeStruct {
922    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
923        if element.value.container() != Some(ContainerKind::Structure) {
924            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
925        }
926        let mut range_min = None;
927        let mut range_max = None;
928        let mut percent_max = None;
929        let mut percent_min = None;
930        let mut percent_typical = None;
931        let mut fixed_max = None;
932        let mut fixed_min = None;
933        let mut fixed_typical = None;
934        loop {
935            let Some(field) = reader.next_element()? else {
936                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
937            };
938            if field.value == Value::EndOfContainer {
939                break;
940            }
941            match field.tag.context() {
942                Some(0) => range_min = Some(<i64>::from_tlv(reader, &field)?),
943                Some(1) => range_max = Some(<i64>::from_tlv(reader, &field)?),
944                Some(2) => percent_max = Some(<u16>::from_tlv(reader, &field)?),
945                Some(3) => percent_min = Some(<u16>::from_tlv(reader, &field)?),
946                Some(4) => percent_typical = Some(<u16>::from_tlv(reader, &field)?),
947                Some(5) => fixed_max = Some(<u64>::from_tlv(reader, &field)?),
948                Some(6) => fixed_min = Some(<u64>::from_tlv(reader, &field)?),
949                Some(7) => fixed_typical = Some(<u64>::from_tlv(reader, &field)?),
950                // A field this revision does not define. §7.19.2: a receiver
951                // ignores what it does not know rather than refusing the whole
952                // structure, which is what lets a newer peer talk to an older one.
953                _ => reader.skip_value(&field)?,
954            }
955        }
956        Ok(Self {
957            range_min: range_min.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
958            range_max: range_max.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
959            percent_max,
960            percent_min,
961            percent_typical,
962            fixed_max,
963            fixed_min,
964            fixed_typical,
965        })
966    }
967}
968
969/// `MeasurementAccuracyStruct`.
970#[derive(Debug, Clone, Copy, PartialEq)]
971pub struct MeasurementAccuracyStruct<'a> {
972    /// `MeasurementType`, field 0.
973    pub measurement_type: MeasurementTypeEnum,
974    /// `Measured`, field 1.
975    pub measured: bool,
976    /// `MinMeasuredValue`, field 2.
977    pub min_measured_value: i64,
978    /// `MaxMeasuredValue`, field 3.
979    pub max_measured_value: i64,
980    /// `AccuracyRanges`, field 4.
981    pub accuracy_ranges: TlvList<'a, MeasurementAccuracyRangeStruct>,
982}
983
984impl<'a> ToTlv for MeasurementAccuracyStruct<'a> {
985    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
986        w.start_structure(tag)?;
987        self.measurement_type.to_tlv(w, Tag::Context(0))?;
988        self.measured.to_tlv(w, Tag::Context(1))?;
989        self.min_measured_value.to_tlv(w, Tag::Context(2))?;
990        self.max_measured_value.to_tlv(w, Tag::Context(3))?;
991        self.accuracy_ranges.to_tlv(w, Tag::Context(4))?;
992        w.end_container()
993    }
994}
995
996impl<'a> FromTlv<'a> for MeasurementAccuracyStruct<'a> {
997    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
998        if element.value.container() != Some(ContainerKind::Structure) {
999            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1000        }
1001        let mut measurement_type = None;
1002        let mut measured = None;
1003        let mut min_measured_value = None;
1004        let mut max_measured_value = None;
1005        let mut accuracy_ranges = None;
1006        loop {
1007            let Some(field) = reader.next_element()? else {
1008                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1009            };
1010            if field.value == Value::EndOfContainer {
1011                break;
1012            }
1013            match field.tag.context() {
1014                Some(0) => {
1015                    measurement_type = Some(<MeasurementTypeEnum>::from_tlv(reader, &field)?)
1016                }
1017                Some(1) => measured = Some(<bool>::from_tlv(reader, &field)?),
1018                Some(2) => min_measured_value = Some(<i64>::from_tlv(reader, &field)?),
1019                Some(3) => max_measured_value = Some(<i64>::from_tlv(reader, &field)?),
1020                Some(4) => {
1021                    accuracy_ranges = Some(<TlvList<'a, MeasurementAccuracyRangeStruct>>::from_tlv(
1022                        reader, &field,
1023                    )?)
1024                }
1025                // A field this revision does not define. §7.19.2: a receiver
1026                // ignores what it does not know rather than refusing the whole
1027                // structure, which is what lets a newer peer talk to an older one.
1028                _ => reader.skip_value(&field)?,
1029            }
1030        }
1031        Ok(Self {
1032            measurement_type: measurement_type
1033                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1034            measured: measured.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1035            min_measured_value: min_measured_value
1036                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1037            max_measured_value: max_measured_value
1038                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1039            accuracy_ranges: accuracy_ranges
1040                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1041        })
1042    }
1043}
1044
1045/// `PowerThresholdStruct`.
1046#[derive(Debug, Clone, Copy, PartialEq)]
1047pub struct PowerThresholdStruct {
1048    /// `PowerThreshold`, field 0.
1049    pub power_threshold: Option<i32>,
1050    /// `ApparentPowerThreshold`, field 1.
1051    pub apparent_power_threshold: Option<i32>,
1052    /// `PowerThresholdSource`, field 2.
1053    pub power_threshold_source: Nullable<PowerThresholdSourceEnum>,
1054}
1055
1056impl ToTlv for PowerThresholdStruct {
1057    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
1058        w.start_structure(tag)?;
1059        if let Some(value) = &self.power_threshold {
1060            value.to_tlv(w, Tag::Context(0))?;
1061        }
1062        if let Some(value) = &self.apparent_power_threshold {
1063            value.to_tlv(w, Tag::Context(1))?;
1064        }
1065        self.power_threshold_source.to_tlv(w, Tag::Context(2))?;
1066        w.end_container()
1067    }
1068}
1069
1070impl<'a> FromTlv<'a> for PowerThresholdStruct {
1071    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
1072        if element.value.container() != Some(ContainerKind::Structure) {
1073            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1074        }
1075        let mut power_threshold = None;
1076        let mut apparent_power_threshold = None;
1077        let mut power_threshold_source = None;
1078        loop {
1079            let Some(field) = reader.next_element()? else {
1080                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1081            };
1082            if field.value == Value::EndOfContainer {
1083                break;
1084            }
1085            match field.tag.context() {
1086                Some(0) => power_threshold = Some(<i32>::from_tlv(reader, &field)?),
1087                Some(1) => apparent_power_threshold = Some(<i32>::from_tlv(reader, &field)?),
1088                Some(2) => {
1089                    power_threshold_source = Some(<Nullable<PowerThresholdSourceEnum>>::from_tlv(
1090                        reader, &field,
1091                    )?)
1092                }
1093                // A field this revision does not define. §7.19.2: a receiver
1094                // ignores what it does not know rather than refusing the whole
1095                // structure, which is what lets a newer peer talk to an older one.
1096                _ => reader.skip_value(&field)?,
1097            }
1098        }
1099        Ok(Self {
1100            power_threshold,
1101            apparent_power_threshold,
1102            power_threshold_source: power_threshold_source
1103                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1104        })
1105    }
1106}
1107
1108/// `PriceStruct`.
1109#[derive(Debug, Clone, Copy, PartialEq)]
1110pub struct PriceStruct {
1111    /// `Amount`, field 0.
1112    pub amount: i64,
1113    /// `Currency`, field 1.
1114    pub currency: CurrencyStruct,
1115}
1116
1117impl ToTlv for PriceStruct {
1118    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
1119        w.start_structure(tag)?;
1120        self.amount.to_tlv(w, Tag::Context(0))?;
1121        self.currency.to_tlv(w, Tag::Context(1))?;
1122        w.end_container()
1123    }
1124}
1125
1126impl<'a> FromTlv<'a> for PriceStruct {
1127    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
1128        if element.value.container() != Some(ContainerKind::Structure) {
1129            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1130        }
1131        let mut amount = None;
1132        let mut currency = None;
1133        loop {
1134            let Some(field) = reader.next_element()? else {
1135                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1136            };
1137            if field.value == Value::EndOfContainer {
1138                break;
1139            }
1140            match field.tag.context() {
1141                Some(0) => amount = Some(<i64>::from_tlv(reader, &field)?),
1142                Some(1) => currency = Some(<CurrencyStruct>::from_tlv(reader, &field)?),
1143                // A field this revision does not define. §7.19.2: a receiver
1144                // ignores what it does not know rather than refusing the whole
1145                // structure, which is what lets a newer peer talk to an older one.
1146                _ => reader.skip_value(&field)?,
1147            }
1148        }
1149        Ok(Self {
1150            amount: amount.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1151            currency: currency.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1152        })
1153    }
1154}
1155
1156/// `SemanticTagStruct`.
1157#[derive(Debug, Clone, Copy, PartialEq)]
1158pub struct SemanticTagStruct<'a> {
1159    /// `MfgCode`, field 0.
1160    pub mfg_code: Nullable<crate::msg::VendorId>,
1161    /// `NamespaceID`, field 1.
1162    pub namespace_id: u8,
1163    /// `Tag`, field 2.
1164    pub tag: u8,
1165    /// `Label`, field 3.
1166    pub label: Option<Nullable<&'a str>>,
1167}
1168
1169impl<'a> ToTlv for SemanticTagStruct<'a> {
1170    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
1171        w.start_structure(tag)?;
1172        self.mfg_code.to_tlv(w, Tag::Context(0))?;
1173        self.namespace_id.to_tlv(w, Tag::Context(1))?;
1174        self.tag.to_tlv(w, Tag::Context(2))?;
1175        if let Some(value) = &self.label {
1176            value.to_tlv(w, Tag::Context(3))?;
1177        }
1178        w.end_container()
1179    }
1180}
1181
1182impl<'a> FromTlv<'a> for SemanticTagStruct<'a> {
1183    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
1184        if element.value.container() != Some(ContainerKind::Structure) {
1185            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1186        }
1187        let mut mfg_code = None;
1188        let mut namespace_id = None;
1189        let mut tag = None;
1190        let mut label = None;
1191        loop {
1192            let Some(field) = reader.next_element()? else {
1193                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1194            };
1195            if field.value == Value::EndOfContainer {
1196                break;
1197            }
1198            match field.tag.context() {
1199                Some(0) => {
1200                    mfg_code = Some(<Nullable<crate::msg::VendorId>>::from_tlv(reader, &field)?)
1201                }
1202                Some(1) => namespace_id = Some(<u8>::from_tlv(reader, &field)?),
1203                Some(2) => tag = Some(<u8>::from_tlv(reader, &field)?),
1204                Some(3) => label = Some(<Nullable<&'a str>>::from_tlv(reader, &field)?),
1205                // A field this revision does not define. §7.19.2: a receiver
1206                // ignores what it does not know rather than refusing the whole
1207                // structure, which is what lets a newer peer talk to an older one.
1208                _ => reader.skip_value(&field)?,
1209            }
1210        }
1211        Ok(Self {
1212            mfg_code: mfg_code.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1213            namespace_id: namespace_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1214            tag: tag.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1215            label,
1216        })
1217    }
1218}
1219
1220/// `ViewportStruct`.
1221#[derive(Debug, Clone, Copy, PartialEq)]
1222pub struct ViewportStruct {
1223    /// `X1`, field 0.
1224    pub x1: u16,
1225    /// `Y1`, field 1.
1226    pub y1: u16,
1227    /// `X2`, field 2.
1228    pub x2: u16,
1229    /// `Y2`, field 3.
1230    pub y2: u16,
1231}
1232
1233impl ToTlv for ViewportStruct {
1234    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
1235        w.start_structure(tag)?;
1236        self.x1.to_tlv(w, Tag::Context(0))?;
1237        self.y1.to_tlv(w, Tag::Context(1))?;
1238        self.x2.to_tlv(w, Tag::Context(2))?;
1239        self.y2.to_tlv(w, Tag::Context(3))?;
1240        w.end_container()
1241    }
1242}
1243
1244impl<'a> FromTlv<'a> for ViewportStruct {
1245    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
1246        if element.value.container() != Some(ContainerKind::Structure) {
1247            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1248        }
1249        let mut x1 = None;
1250        let mut y1 = None;
1251        let mut x2 = None;
1252        let mut y2 = None;
1253        loop {
1254            let Some(field) = reader.next_element()? else {
1255                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1256            };
1257            if field.value == Value::EndOfContainer {
1258                break;
1259            }
1260            match field.tag.context() {
1261                Some(0) => x1 = Some(<u16>::from_tlv(reader, &field)?),
1262                Some(1) => y1 = Some(<u16>::from_tlv(reader, &field)?),
1263                Some(2) => x2 = Some(<u16>::from_tlv(reader, &field)?),
1264                Some(3) => y2 = Some(<u16>::from_tlv(reader, &field)?),
1265                // A field this revision does not define. §7.19.2: a receiver
1266                // ignores what it does not know rather than refusing the whole
1267                // structure, which is what lets a newer peer talk to an older one.
1268                _ => reader.skip_value(&field)?,
1269            }
1270        }
1271        Ok(Self {
1272            x1: x1.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1273            y1: y1.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1274            x2: x2.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1275            y2: y2.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1276        })
1277    }
1278}
1279
1280/// `WebRTCSessionStruct`, fabric-scoped — §7.19.1.9's `FabricIndex` is field 254.
1281#[derive(Debug, Clone, Copy, PartialEq)]
1282pub struct WebRTCSessionStruct<'a> {
1283    /// `ID`, field 0.
1284    pub id: u16,
1285    /// `PeerNodeID`, field 1.
1286    pub peer_node_id: crate::msg::NodeId,
1287    /// `PeerEndpointID`, field 2.
1288    pub peer_endpoint_id: u16,
1289    /// `StreamUsage`, field 3.
1290    pub stream_usage: StreamUsageEnum,
1291    /// `VideoStreamID`, field 4.
1292    pub video_stream_id: Option<Nullable<u16>>,
1293    /// `AudioStreamID`, field 5.
1294    pub audio_stream_id: Option<Nullable<u16>>,
1295    /// `MetadataEnabled`, field 6.
1296    pub metadata_enabled: bool,
1297    /// `VideoStreams`, field 7.
1298    pub video_streams: Option<TlvList<'a, u16>>,
1299    /// `AudioStreams`, field 8.
1300    pub audio_streams: Option<TlvList<'a, u16>>,
1301    /// `FabricIndex`, field 254.
1302    pub fabric_index: crate::msg::FabricIndex,
1303}
1304
1305impl<'a> ToTlv for WebRTCSessionStruct<'a> {
1306    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
1307        w.start_structure(tag)?;
1308        self.id.to_tlv(w, Tag::Context(0))?;
1309        self.peer_node_id.to_tlv(w, Tag::Context(1))?;
1310        self.peer_endpoint_id.to_tlv(w, Tag::Context(2))?;
1311        self.stream_usage.to_tlv(w, Tag::Context(3))?;
1312        if let Some(value) = &self.video_stream_id {
1313            value.to_tlv(w, Tag::Context(4))?;
1314        }
1315        if let Some(value) = &self.audio_stream_id {
1316            value.to_tlv(w, Tag::Context(5))?;
1317        }
1318        self.metadata_enabled.to_tlv(w, Tag::Context(6))?;
1319        if let Some(value) = &self.video_streams {
1320            value.to_tlv(w, Tag::Context(7))?;
1321        }
1322        if let Some(value) = &self.audio_streams {
1323            value.to_tlv(w, Tag::Context(8))?;
1324        }
1325        self.fabric_index.to_tlv(w, Tag::Context(254))?;
1326        w.end_container()
1327    }
1328}
1329
1330impl<'a> FromTlv<'a> for WebRTCSessionStruct<'a> {
1331    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
1332        if element.value.container() != Some(ContainerKind::Structure) {
1333            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
1334        }
1335        let mut id = None;
1336        let mut peer_node_id = None;
1337        let mut peer_endpoint_id = None;
1338        let mut stream_usage = None;
1339        let mut video_stream_id = None;
1340        let mut audio_stream_id = None;
1341        let mut metadata_enabled = None;
1342        let mut video_streams = None;
1343        let mut audio_streams = None;
1344        let mut fabric_index = None;
1345        loop {
1346            let Some(field) = reader.next_element()? else {
1347                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
1348            };
1349            if field.value == Value::EndOfContainer {
1350                break;
1351            }
1352            match field.tag.context() {
1353                Some(0) => id = Some(<u16>::from_tlv(reader, &field)?),
1354                Some(1) => peer_node_id = Some(<crate::msg::NodeId>::from_tlv(reader, &field)?),
1355                Some(2) => peer_endpoint_id = Some(<u16>::from_tlv(reader, &field)?),
1356                Some(3) => stream_usage = Some(<StreamUsageEnum>::from_tlv(reader, &field)?),
1357                Some(4) => video_stream_id = Some(<Nullable<u16>>::from_tlv(reader, &field)?),
1358                Some(5) => audio_stream_id = Some(<Nullable<u16>>::from_tlv(reader, &field)?),
1359                Some(6) => metadata_enabled = Some(<bool>::from_tlv(reader, &field)?),
1360                Some(7) => video_streams = Some(<TlvList<'a, u16>>::from_tlv(reader, &field)?),
1361                Some(8) => audio_streams = Some(<TlvList<'a, u16>>::from_tlv(reader, &field)?),
1362                Some(254) => {
1363                    fabric_index = Some(<crate::msg::FabricIndex>::from_tlv(reader, &field)?)
1364                }
1365                // A field this revision does not define. §7.19.2: a receiver
1366                // ignores what it does not know rather than refusing the whole
1367                // structure, which is what lets a newer peer talk to an older one.
1368                _ => reader.skip_value(&field)?,
1369            }
1370        }
1371        Ok(Self {
1372            id: id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1373            peer_node_id: peer_node_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1374            peer_endpoint_id: peer_endpoint_id
1375                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1376            stream_usage: stream_usage.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1377            video_stream_id,
1378            audio_stream_id,
1379            metadata_enabled: metadata_enabled
1380                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1381            video_streams,
1382            audio_streams,
1383            fabric_index: fabric_index.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
1384        })
1385    }
1386}