matc/clusters/codec/
localization_configuration.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_active_locale(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
15 if let tlv::TlvItemValue::String(v) = inp {
16 Ok(v.clone())
17 } else {
18 Err(anyhow::anyhow!("Expected String"))
19 }
20}
21
22pub fn decode_supported_locales(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
24 let mut res = Vec::new();
25 if let tlv::TlvItemValue::List(v) = inp {
26 for item in v {
27 if let tlv::TlvItemValue::String(s) = &item.value {
28 res.push(s.clone());
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 != 0x002B {
50 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002B, got {}\"}}", cluster_id);
51 }
52
53 match attribute_id {
54 0x0000 => {
55 match decode_active_locale(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 0x0001 => {
61 match decode_supported_locales(tlv_value) {
62 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
63 Err(e) => format!("{{\"error\": \"{}\"}}", e),
64 }
65 }
66 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
67 }
68}
69
70pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
75 vec![
76 (0x0000, "ActiveLocale"),
77 (0x0001, "SupportedLocales"),
78 ]
79}
80