Skip to main content

matter_clusters/gen/
binding.rs

1//! Binding cluster (0x001E).
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 = 0x001E;
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    /// `Binding`.
28    pub const BINDING: u32 = 0x0000;
29}
30
31/// `TargetStruct` struct.
32#[derive(Clone, Debug, PartialEq)]
33#[non_exhaustive]
34pub struct TargetStruct {
35    /// Field Node (tag 1).
36    pub node: Option<u64>,
37    /// Field Group (tag 2).
38    pub group: Option<u16>,
39    /// Field Endpoint (tag 3).
40    pub endpoint: Option<u16>,
41    /// Field Cluster (tag 4).
42    pub cluster: Option<u32>,
43    /// Field FabricIndex (tag 254).
44    pub fabric_index: u8,
45}
46
47impl TargetStruct {
48    /// Decode the fields of an already-opened anonymous structure
49    /// (reader positioned after the struct start; consumes to its end).
50    ///
51    /// # Errors
52    /// Returns [`ClusterError`] on a malformed structure or missing required field.
53    pub fn decode_from(r: &mut TlvReader<'_>) -> Result<Self, ClusterError> {
54        let mut f_node: Option<u64> = None;
55        let mut f_group: Option<u16> = None;
56        let mut f_endpoint: Option<u16> = None;
57        let mut f_cluster: Option<u32> = None;
58        let mut f_fabric_index: Option<u8> = None;
59        loop {
60            match r.next()? {
61                Some(Element::ContainerEnd) => break,
62                Some(Element::Scalar {
63                    tag: Tag::Context(1),
64                    value: Value::Uint(v),
65                }) => {
66                    f_node =
67                        Some(u64::try_from(v).map_err(|_| ClusterError::InvalidLength("Node"))?)
68                }
69                Some(Element::Scalar {
70                    tag: Tag::Context(2),
71                    value: Value::Uint(v),
72                }) => {
73                    f_group =
74                        Some(u16::try_from(v).map_err(|_| ClusterError::InvalidLength("Group"))?)
75                }
76                Some(Element::Scalar {
77                    tag: Tag::Context(3),
78                    value: Value::Uint(v),
79                }) => {
80                    f_endpoint = Some(
81                        u16::try_from(v).map_err(|_| ClusterError::InvalidLength("Endpoint"))?,
82                    )
83                }
84                Some(Element::Scalar {
85                    tag: Tag::Context(4),
86                    value: Value::Uint(v),
87                }) => {
88                    f_cluster =
89                        Some(u32::try_from(v).map_err(|_| ClusterError::InvalidLength("Cluster"))?)
90                }
91                Some(Element::Scalar {
92                    tag: Tag::Context(254),
93                    value: Value::Uint(v),
94                }) => {
95                    f_fabric_index = Some(
96                        u8::try_from(v).map_err(|_| ClusterError::InvalidLength("FabricIndex"))?,
97                    )
98                }
99                None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
100                Some(Element::ContainerStart { .. }) => r.skip_container()?,
101                Some(_) => {} // unknown/future scalar — skip
102            }
103        }
104        Ok(Self {
105            node: f_node,
106            group: f_group,
107            endpoint: f_endpoint,
108            cluster: f_cluster,
109            fabric_index: f_fabric_index.ok_or(ClusterError::MissingField("FabricIndex"))?,
110        })
111    }
112    /// Decode from a standalone anonymous TLV structure.
113    ///
114    /// # Errors
115    /// Returns [`ClusterError`] if the bytes are not an anonymous structure or a field is malformed.
116    pub fn decode(tlv: &[u8]) -> Result<Self, ClusterError> {
117        let mut r = TlvReader::new(tlv);
118        match r.next()? {
119            Some(Element::ContainerStart {
120                kind: ContainerKind::Structure,
121                ..
122            }) => {}
123            _ => {
124                return Err(ClusterError::UnexpectedType {
125                    context: "TargetStruct",
126                })
127            }
128        }
129        Self::decode_from(&mut r)
130    }
131    /// Write this struct's fields into an already-open container.
132    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
133    pub fn write_fields(&self, w: &mut TlvWriter<'_>) {
134        if let Some(node) = &self.node {
135            w.put_uint(Tag::Context(1), u64::from(*node))
136                .expect("infallible: vec writer");
137        }
138        if let Some(group) = &self.group {
139            w.put_uint(Tag::Context(2), u64::from(*group))
140                .expect("infallible: vec writer");
141        }
142        if let Some(endpoint) = &self.endpoint {
143            w.put_uint(Tag::Context(3), u64::from(*endpoint))
144                .expect("infallible: vec writer");
145        }
146        if let Some(cluster) = &self.cluster {
147            w.put_uint(Tag::Context(4), u64::from(*cluster))
148                .expect("infallible: vec writer");
149        }
150        w.put_uint(Tag::Context(254), u64::from(self.fabric_index))
151            .expect("infallible: vec writer");
152    }
153    /// Encode as a standalone anonymous TLV structure.
154    #[must_use]
155    #[allow(clippy::expect_used)] // Vec-backed TlvWriter is infallible.
156    pub fn encode(&self) -> Vec<u8> {
157        let mut buf = Vec::new();
158        let mut w = TlvWriter::new(&mut buf);
159        w.start_structure(Tag::Anonymous)
160            .expect("infallible: vec writer");
161        self.write_fields(&mut w);
162        w.end_container().expect("infallible: vec writer");
163        buf
164    }
165}
166
167/// Decode the `Binding` attribute value.
168///
169/// # Errors
170/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
171pub fn decode_binding(tlv: &[u8]) -> Result<Vec<TargetStruct>, ClusterError> {
172    let mut r = TlvReader::new(tlv);
173    match r.next()? {
174        Some(Element::ContainerStart {
175            kind: ContainerKind::Array,
176            ..
177        }) => {}
178        _ => return Err(ClusterError::UnexpectedType { context: "Binding" }),
179    }
180    let r = &mut r;
181    let mut out = Vec::new();
182    loop {
183        match r.next()? {
184            Some(Element::ContainerEnd) => break,
185            Some(Element::ContainerStart {
186                kind: ContainerKind::Structure,
187                ..
188            }) => {
189                out.push(TargetStruct::decode_from(r)?);
190            }
191            None => return Err(ClusterError::Tlv(matter_codec::Error::UnclosedContainer)),
192            Some(Element::ContainerStart { .. }) => r.skip_container()?,
193            Some(_) => {} // skip unknown scalar
194        }
195    }
196    Ok(out)
197}