matter-clusters 0.1.0

Matter protocol cluster definitions (generated from the spec).
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
//! BasicInformation cluster (0x0028).
//! @generated by `cargo xtask codegen` — do not edit.

#![allow(
    clippy::all,
    clippy::pedantic,
    dead_code,
    unreachable_pub,
    unused_imports
)]

use crate::datatypes::SemanticTagStruct;
use crate::error::ClusterError;
use crate::types::Nullable;
use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};

/// Cluster ID.
pub const CLUSTER_ID: u32 = 0x0028;
/// Cluster revision.
pub const CLUSTER_REVISION: u16 = 5;

/// Command IDs (requests and responses).
pub mod command_id {}

/// Attribute IDs (cluster-specific).
pub mod attribute_id {
    /// `DataModelRevision`.
    pub const DATA_MODEL_REVISION: u32 = 0x0000;
    /// `VendorName`.
    pub const VENDOR_NAME: u32 = 0x0001;
    /// `VendorId`.
    pub const VENDOR_ID: u32 = 0x0002;
    /// `ProductName`.
    pub const PRODUCT_NAME: u32 = 0x0003;
    /// `ProductId`.
    pub const PRODUCT_ID: u32 = 0x0004;
    /// `NodeLabel`.
    pub const NODE_LABEL: u32 = 0x0005;
    /// `Location`.
    pub const LOCATION: u32 = 0x0006;
    /// `HardwareVersion`.
    pub const HARDWARE_VERSION: u32 = 0x0007;
    /// `HardwareVersionString`.
    pub const HARDWARE_VERSION_STRING: u32 = 0x0008;
    /// `SoftwareVersion`.
    pub const SOFTWARE_VERSION: u32 = 0x0009;
    /// `SoftwareVersionString`.
    pub const SOFTWARE_VERSION_STRING: u32 = 0x000A;
    /// `ManufacturingDate`.
    pub const MANUFACTURING_DATE: u32 = 0x000B;
    /// `PartNumber`.
    pub const PART_NUMBER: u32 = 0x000C;
    /// `ProductUrl`.
    pub const PRODUCT_URL: u32 = 0x000D;
    /// `ProductLabel`.
    pub const PRODUCT_LABEL: u32 = 0x000E;
    /// `SerialNumber`.
    pub const SERIAL_NUMBER: u32 = 0x000F;
    /// `LocalConfigDisabled`.
    pub const LOCAL_CONFIG_DISABLED: u32 = 0x0010;
    /// `Reachable`.
    pub const REACHABLE: u32 = 0x0011;
    /// `UniqueId`.
    pub const UNIQUE_ID: u32 = 0x0012;
    /// `CapabilityMinima`.
    pub const CAPABILITY_MINIMA: u32 = 0x0013;
    /// `ProductAppearance`.
    pub const PRODUCT_APPEARANCE: u32 = 0x0014;
    /// `SpecificationVersion`.
    pub const SPECIFICATION_VERSION: u32 = 0x0015;
    /// `MaxPathsPerInvoke`.
    pub const MAX_PATHS_PER_INVOKE: u32 = 0x0016;
    /// `ConfigurationVersion`.
    pub const CONFIGURATION_VERSION: u32 = 0x0018;
}

/// `CapabilityMinimaStruct` struct.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct CapabilityMinimaStruct {
    /// Field CaseSessionsPerFabric (tag 0).
    pub case_sessions_per_fabric: u16,
    /// Field SubscriptionsPerFabric (tag 1).
    pub subscriptions_per_fabric: u16,
}

/// `ColorEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorEnum {
    /// Black = 0.
    Black,
    /// Navy = 1.
    Navy,
    /// Green = 2.
    Green,
    /// Teal = 3.
    Teal,
    /// Maroon = 4.
    Maroon,
    /// Purple = 5.
    Purple,
    /// Olive = 6.
    Olive,
    /// Gray = 7.
    Gray,
    /// Blue = 8.
    Blue,
    /// Lime = 9.
    Lime,
    /// Aqua = 10.
    Aqua,
    /// Red = 11.
    Red,
    /// Fuchsia = 12.
    Fuchsia,
    /// Yellow = 13.
    Yellow,
    /// White = 14.
    White,
    /// Nickel = 15.
    Nickel,
    /// Chrome = 16.
    Chrome,
    /// Brass = 17.
    Brass,
    /// Copper = 18.
    Copper,
    /// Silver = 19.
    Silver,
    /// Gold = 20.
    Gold,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl ColorEnum {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Black,
            1 => Self::Navy,
            2 => Self::Green,
            3 => Self::Teal,
            4 => Self::Maroon,
            5 => Self::Purple,
            6 => Self::Olive,
            7 => Self::Gray,
            8 => Self::Blue,
            9 => Self::Lime,
            10 => Self::Aqua,
            11 => Self::Red,
            12 => Self::Fuchsia,
            13 => Self::Yellow,
            14 => Self::White,
            15 => Self::Nickel,
            16 => Self::Chrome,
            17 => Self::Brass,
            18 => Self::Copper,
            19 => Self::Silver,
            20 => Self::Gold,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Black => 0,
            Self::Navy => 1,
            Self::Green => 2,
            Self::Teal => 3,
            Self::Maroon => 4,
            Self::Purple => 5,
            Self::Olive => 6,
            Self::Gray => 7,
            Self::Blue => 8,
            Self::Lime => 9,
            Self::Aqua => 10,
            Self::Red => 11,
            Self::Fuchsia => 12,
            Self::Yellow => 13,
            Self::White => 14,
            Self::Nickel => 15,
            Self::Chrome => 16,
            Self::Brass => 17,
            Self::Copper => 18,
            Self::Silver => 19,
            Self::Gold => 20,
            Self::Unknown(v) => v,
        }
    }
}

/// `ProductAppearanceStruct` struct.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ProductAppearanceStruct {
    /// Field Finish (tag 0).
    pub finish: ProductFinishEnum,
    /// Field PrimaryColor (tag 1).
    pub primary_color: Nullable<ColorEnum>,
}

/// `ProductFinishEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ProductFinishEnum {
    /// Other = 0.
    Other,
    /// Matte = 1.
    Matte,
    /// Satin = 2.
    Satin,
    /// Polished = 3.
    Polished,
    /// Rugged = 4.
    Rugged,
    /// Fabric = 5.
    Fabric,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl ProductFinishEnum {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Other,
            1 => Self::Matte,
            2 => Self::Satin,
            3 => Self::Polished,
            4 => Self::Rugged,
            5 => Self::Fabric,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Other => 0,
            Self::Matte => 1,
            Self::Satin => 2,
            Self::Polished => 3,
            Self::Rugged => 4,
            Self::Fabric => 5,
            Self::Unknown(v) => v,
        }
    }
}

impl CapabilityMinimaStruct {
    /// Decode the fields of an already-opened anonymous structure
    /// (reader positioned after the struct start; consumes to its end).
    ///
    /// # Errors
    /// Returns [`ClusterError`] on a malformed structure or missing required field.
    pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
        let mut f_case_sessions_per_fabric: Option<u16> = None;
        let mut f_subscriptions_per_fabric: Option<u16> = None;
        loop {
            match r.next()? {
                Some(Element::ContainerEnd) => break,
                Some(Element::Scalar {
                    tag: Tag::Context(0),
                    value: Value::Uint(v),
                }) => {
                    f_case_sessions_per_fabric = Some(
                        u16::try_from(v)
                            .map_err(|_| ClusterError::InvalidLength("CaseSessionsPerFabric"))?,
                    )
                }
                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Uint(v),
                }) => {
                    f_subscriptions_per_fabric = Some(
                        u16::try_from(v)
                            .map_err(|_| ClusterError::InvalidLength("SubscriptionsPerFabric"))?,
                    )
                }
                None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
                Some(Element::ContainerStart { .. }) => r.skip_container()?,
                Some(_) => {} // unknown/future scalar — skip
            }
        }
        Ok(Self {
            case_sessions_per_fabric: f_case_sessions_per_fabric
                .ok_or(ClusterError::MissingField("CaseSessionsPerFabric"))?,
            subscriptions_per_fabric: f_subscriptions_per_fabric
                .ok_or(ClusterError::MissingField("SubscriptionsPerFabric"))?,
        })
    }
    /// Decode from a standalone anonymous TLV structure.
    ///
    /// # Errors
    /// Returns [`ClusterError`] if the bytes are not an anonymous structure or a field is malformed.
    pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
        let mut r = TlvReader::new(tlv);
        match r.next()? {
            Some(Element::ContainerStart {
                kind: ContainerKind::Structure,
                ..
            }) => {}
            _ => {
                return Err(ClusterError::UnexpectedType {
                    context: "CapabilityMinimaStruct",
                })
            }
        }
        Self::decode_from(&mut r)
    }
    /// Write this struct's fields into an already-open container.
    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
    pub fn write_fields(&self, w: &mut TlvWriter<'_>) {
        w.put_uint(Tag::Context(0), u64::from(self.case_sessions_per_fabric))
            .expect("infallible: vec writer");
        w.put_uint(Tag::Context(1), u64::from(self.subscriptions_per_fabric))
            .expect("infallible: vec writer");
    }
    /// Encode as a standalone anonymous TLV structure.
    #[must_use]
    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous)
            .expect("infallible: vec writer");
        self.write_fields(&mut w);
        w.end_container().expect("infallible: vec writer");
        buf
    }
}

impl ProductAppearanceStruct {
    /// Decode the fields of an already-opened anonymous structure
    /// (reader positioned after the struct start; consumes to its end).
    ///
    /// # Errors
    /// Returns [`ClusterError`] on a malformed structure or missing required field.
    pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
        let mut f_finish: Option<ProductFinishEnum> = None;
        let mut f_primary_color: Option<Nullable<ColorEnum>> = None;
        loop {
            match r.next()? {
                Some(Element::ContainerEnd) => break,
                Some(Element::Scalar {
                    tag: Tag::Context(0),
                    value: Value::Uint(v),
                }) => {
                    f_finish = Some(ProductFinishEnum::from_raw(
                        u8::try_from(v).map_err(|_| ClusterError::InvalidLength("Finish"))?,
                    ))
                }
                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Null,
                }) => f_primary_color = Some(Nullable::Null),
                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Uint(v),
                }) => {
                    f_primary_color = Some(Nullable::Value(ColorEnum::from_raw(
                        u8::try_from(v).map_err(|_| ClusterError::InvalidLength("PrimaryColor"))?,
                    )))
                }
                None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
                Some(Element::ContainerStart { .. }) => r.skip_container()?,
                Some(_) => {} // unknown/future scalar — skip
            }
        }
        Ok(Self {
            finish: f_finish.ok_or(ClusterError::MissingField("Finish"))?,
            primary_color: f_primary_color.ok_or(ClusterError::MissingField("PrimaryColor"))?,
        })
    }
    /// Decode from a standalone anonymous TLV structure.
    ///
    /// # Errors
    /// Returns [`ClusterError`] if the bytes are not an anonymous structure or a field is malformed.
    pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
        let mut r = TlvReader::new(tlv);
        match r.next()? {
            Some(Element::ContainerStart {
                kind: ContainerKind::Structure,
                ..
            }) => {}
            _ => {
                return Err(ClusterError::UnexpectedType {
                    context: "ProductAppearanceStruct",
                })
            }
        }
        Self::decode_from(&mut r)
    }
    /// Write this struct's fields into an already-open container.
    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
    pub fn write_fields(&self, w: &mut TlvWriter<'_>) {
        w.put_uint(Tag::Context(0), u64::from(self.finish.to_raw()))
            .expect("infallible: vec writer");
        match &self.primary_color {
            Nullable::Null => w.put_null(Tag::Context(1)).expect("infallible: vec writer"),
            Nullable::Value(primary_color) => {
                w.put_uint(Tag::Context(1), u64::from((*primary_color).to_raw()))
                    .expect("infallible: vec writer");
            }
        }
    }
    /// Encode as a standalone anonymous TLV structure.
    #[must_use]
    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous)
            .expect("infallible: vec writer");
        self.write_fields(&mut w);
        w.end_container().expect("infallible: vec writer");
        buf
    }
}

/// Decode the `DataModelRevision` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_data_model_revision(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("DataModelRevision"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "DataModelRevision",
        }),
    }
}

/// Decode the `VendorName` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_vendor_name(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "VendorName",
        }),
    }
}

/// Decode the `VendorId` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_vendor_id(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("VendorId"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "VendorId",
        }),
    }
}

/// Decode the `ProductName` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_product_name(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "ProductName",
        }),
    }
}

/// Decode the `ProductId` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_product_id(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("ProductId"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "ProductId",
        }),
    }
}

/// Decode the `NodeLabel` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_node_label(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "NodeLabel",
        }),
    }
}

/// Encode the `NodeLabel` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_node_label(value: &String) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_utf8(Tag::Anonymous, &value)
        .expect("infallible: vec writer");
    buf
}

/// Decode the `Location` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_location(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "Location",
        }),
    }
}

/// Encode the `Location` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_location(value: &String) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_utf8(Tag::Anonymous, &value)
        .expect("infallible: vec writer");
    buf
}

/// Decode the `HardwareVersion` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_hardware_version(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("HardwareVersion"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "HardwareVersion",
        }),
    }
}

/// Decode the `HardwareVersionString` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_hardware_version_string(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "HardwareVersionString",
        }),
    }
}

/// Decode the `SoftwareVersion` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_software_version(tlv: &[u8]) -> Result<u32, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u32::try_from(v).map_err(|_| ClusterError::InvalidLength("SoftwareVersion"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "SoftwareVersion",
        }),
    }
}

/// Decode the `SoftwareVersionString` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_software_version_string(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "SoftwareVersionString",
        }),
    }
}

/// Decode the `ManufacturingDate` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_manufacturing_date(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "ManufacturingDate",
        }),
    }
}

/// Decode the `PartNumber` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_part_number(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "PartNumber",
        }),
    }
}

/// Decode the `ProductUrl` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_product_url(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "ProductUrl",
        }),
    }
}

/// Decode the `ProductLabel` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_product_label(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "ProductLabel",
        }),
    }
}

/// Decode the `SerialNumber` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_serial_number(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "SerialNumber",
        }),
    }
}

/// Decode the `LocalConfigDisabled` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_local_config_disabled(tlv: &[u8]) -> Result<bool, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Bool(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "LocalConfigDisabled",
        }),
    }
}

/// Encode the `LocalConfigDisabled` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_local_config_disabled(value: bool) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_bool(Tag::Anonymous, value)
        .expect("infallible: vec writer");
    buf
}

/// Decode the `Reachable` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_reachable(tlv: &[u8]) -> Result<bool, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Bool(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "Reachable",
        }),
    }
}

/// Decode the `UniqueId` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_unique_id(tlv: &[u8]) -> Result<String, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Utf8(v),
            ..
        }) => Ok(v),
        _ => Err(ClusterError::UnexpectedType {
            context: "UniqueId",
        }),
    }
}

/// Decode the `CapabilityMinima` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_capability_minima(tlv: &[u8]) -> Result<CapabilityMinimaStruct, ClusterError> {
    CapabilityMinimaStruct::decode(tlv)
}

/// Decode the `ProductAppearance` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_product_appearance(tlv: &[u8]) -> Result<ProductAppearanceStruct, ClusterError> {
    ProductAppearanceStruct::decode(tlv)
}

/// Decode the `SpecificationVersion` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_specification_version(tlv: &[u8]) -> Result<u32, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u32::try_from(v).map_err(|_| ClusterError::InvalidLength("SpecificationVersion"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "SpecificationVersion",
        }),
    }
}

/// Decode the `MaxPathsPerInvoke` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_max_paths_per_invoke(tlv: &[u8]) -> Result<u16, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("MaxPathsPerInvoke"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "MaxPathsPerInvoke",
        }),
    }
}

/// Decode the `ConfigurationVersion` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_configuration_version(tlv: &[u8]) -> Result<u32, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(u32::try_from(v).map_err(|_| ClusterError::InvalidLength("ConfigurationVersion"))?),
        _ => Err(ClusterError::UnexpectedType {
            context: "ConfigurationVersion",
        }),
    }
}