matc/clusters/codec/
label_cluster.rs

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