matc/clusters/codec/
localization_configuration.rs

1//! Generated Matter TLV encoders and decoders for Localization Configuration Cluster
2//! Cluster ID: 0x002B
3//! 
4//! This file is automatically generated from LocalizationConfiguration.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Attribute decoders
12
13/// Decode ActiveLocale attribute (0x0000)
14pub 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
22/// Decode SupportedLocales attribute (0x0001)
23pub 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
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 != 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
70/// Get list of all attributes supported by this cluster
71/// 
72/// # Returns
73/// Vector of tuples containing (attribute_id, attribute_name)
74pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
75    vec![
76        (0x0000, "ActiveLocale"),
77        (0x0001, "SupportedLocales"),
78    ]
79}
80