Skip to main content

matter_clusters/gen/
user_label.rs

1//! UserLabel cluster (0x0041).
2//! @generated by `cargo xtask codegen` — do not edit.
3
4#![allow(
5    clippy::all,
6    clippy::pedantic,
7    dead_code,
8    unreachable_pub,
9    unused_imports
10)]
11
12use crate::datatypes::SemanticTagStruct;
13use crate::error::ClusterError;
14use crate::types::Nullable;
15use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};
16
17/// Cluster ID.
18pub const CLUSTER_ID: u32 = 0x0041;
19/// Cluster revision.
20pub const CLUSTER_REVISION: u16 = 1;
21
22/// Command IDs (requests and responses).
23pub mod command_id {}
24
25/// Attribute IDs (cluster-specific).
26pub mod attribute_id {
27    /// `LabelList`.
28    pub const LABEL_LIST: u32 = 0x0000;
29}
30
31/// `LabelStruct` struct.
32#[derive(Clone, Debug, PartialEq)]
33#[non_exhaustive]
34pub struct LabelStruct {
35    /// Field Label (tag 0).
36    pub label: String,
37    /// Field Value (tag 1).
38    pub value: String,
39}
40
41impl LabelStruct {
42    /// Decode the fields of an already-opened anonymous structure
43    /// (reader positioned after the struct start; consumes to its end).
44    ///
45    /// # Errors
46    /// Returns [`ClusterError`] on a malformed structure or missing required field.
47    pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
48        let mut f_label: Option<String> = None;
49        let mut f_value: Option<String> = None;
50        loop {
51            match r.next()? {
52                Some(Element::ContainerEnd) => break,
53                Some(Element::Scalar {
54                    tag: Tag::Context(0),
55                    value: Value::Utf8(v),
56                }) => f_label = Some(v),
57                Some(Element::Scalar {
58                    tag: Tag::Context(1),
59                    value: Value::Utf8(v),
60                }) => f_value = Some(v),
61                None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
62                Some(Element::ContainerStart { .. }) => r.skip_container()?,
63                Some(_) => {} // unknown/future scalar — skip
64            }
65        }
66        Ok(Self {
67            label: f_label.ok_or(ClusterError::MissingField("Label"))?,
68            value: f_value.ok_or(ClusterError::MissingField("Value"))?,
69        })
70    }
71    /// Decode from a standalone anonymous TLV structure.
72    ///
73    /// # Errors
74    /// Returns [`ClusterError`] if the bytes are not an anonymous structure or a field is malformed.
75    pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
76        let mut r = TlvReader::new(tlv);
77        match r.next()? {
78            Some(Element::ContainerStart {
79                kind: ContainerKind::Structure,
80                ..
81            }) => {}
82            _ => {
83                return Err(ClusterError::UnexpectedType {
84                    context: "LabelStruct",
85                })
86            }
87        }
88        Self::decode_from(&mut r)
89    }
90    /// Write this struct's fields into an already-open container.
91    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
92    pub fn write_fields(&self, w: &mut TlvWriter<'_>) {
93        w.put_utf8(Tag::Context(0), &self.label)
94            .expect("infallible: vec writer");
95        w.put_utf8(Tag::Context(1), &self.value)
96            .expect("infallible: vec writer");
97    }
98    /// Encode as a standalone anonymous TLV structure.
99    #[must_use]
100    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
101    pub fn encode(&self) -> Vec<u8> {
102        let mut buf = Vec::new();
103        let mut w = TlvWriter::new(&mut buf);
104        w.start_structure(Tag::Anonymous)
105            .expect("infallible: vec writer");
106        self.write_fields(&mut w);
107        w.end_container().expect("infallible: vec writer");
108        buf
109    }
110}
111
112/// Decode the `LabelList` attribute value.
113///
114/// # Errors
115/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
116pub fn decode_label_list(tlv: &[u8]) -> Result<Vec<LabelStruct>, ClusterError> {
117    let mut r = TlvReader::new(tlv);
118    match r.next()? {
119        Some(Element::ContainerStart {
120            kind: ContainerKind::Array,
121            ..
122        }) => {}
123        _ => {
124            return Err(ClusterError::UnexpectedType {
125                context: "LabelList",
126            })
127        }
128    }
129    let r = &mut r;
130    let mut out = Vec::new();
131    loop {
132        match r.next()? {
133            Some(Element::ContainerEnd) => break,
134            Some(Element::ContainerStart {
135                kind: ContainerKind::Structure,
136                ..
137            }) => {
138                out.push(LabelStruct::decode_from(r)?);
139            }
140            None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
141            Some(Element::ContainerStart { .. }) => r.skip_container()?,
142            Some(_) => {} // skip unknown scalar
143        }
144    }
145    Ok(out)
146}