matc/clusters/codec/
label_cluster.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct Label {
15 pub label: Option<String>,
16 pub value: Option<String>,
17}
18
19pub 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
36pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
48 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
64pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
69 vec![
70 (0x0000, "LabelList"),
71 ]
72}
73