matc/clusters/codec/
label_cluster.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, serde::Serialize)]
16pub struct Label {
17 pub label: Option<String>,
18 pub value: Option<String>,
19}
20
21pub 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
38pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
50 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
66pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
71 vec![
72 (0x0000, "LabelList"),
73 ]
74}
75