matc/clusters/codec/
label_cluster.rs

1//! Matter TLV encoders and decoders for Label Cluster
2//! Cluster ID: 0x0000
3//!
4//! This file is automatically generated from Label-Cluster.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Struct definitions
14
15#[derive(Debug, serde::Serialize)]
16pub struct Label {
17    pub label: Option<String>,
18    pub value: Option<String>,
19}
20
21// Attribute decoders
22
23/// Decode LabelList attribute (0x0000)
24pub fn decode_label_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<Label>> {
25    let mut res = Vec::new();
26    if let tlv::TlvItemValue::List(v) = inp {
27        for item in v {
28            res.push(Label {
29                label: item.get_string_owned(&[0]),
30                value: item.get_string_owned(&[1]),
31            });
32        }
33    }
34    Ok(res)
35}
36
37
38// JSON dispatcher function
39
40/// Decode attribute value and return as JSON string
41///
42/// # Parameters
43/// * `cluster_id` - The cluster identifier
44/// * `attribute_id` - The attribute identifier
45/// * `tlv_value` - The TLV value to decode
46///
47/// # Returns
48/// JSON string representation of the decoded value or error
49pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
50    // Verify this is the correct cluster
51    if cluster_id != 0x0000 {
52        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0000, got {}\"}}", cluster_id);
53    }
54
55    match attribute_id {
56        0x0000 => {
57            match decode_label_list(tlv_value) {
58                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
59                Err(e) => format!("{{\"error\": \"{}\"}}", e),
60            }
61        }
62        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
63    }
64}
65
66/// Get list of all attributes supported by this cluster
67///
68/// # Returns
69/// Vector of tuples containing (attribute_id, attribute_name)
70pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
71    vec![
72        (0x0000, "LabelList"),
73    ]
74}
75