matc/clusters/codec/
localization_time_format.rs

1//! Generated Matter TLV encoders and decoders for Time Format Localization Cluster
2//! Cluster ID: 0x002C
3//! 
4//! This file is automatically generated from LocalizationTimeFormat.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Attribute decoders
12
13/// Decode HourFormat attribute (0x0000)
14pub fn decode_hour_format(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
15    if let tlv::TlvItemValue::Int(v) = inp {
16        Ok(*v as u8)
17    } else {
18        Err(anyhow::anyhow!("Expected Integer"))
19    }
20}
21
22/// Decode ActiveCalendarType attribute (0x0001)
23pub fn decode_active_calendar_type(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
24    if let tlv::TlvItemValue::Int(v) = inp {
25        Ok(*v as u8)
26    } else {
27        Err(anyhow::anyhow!("Expected Integer"))
28    }
29}
30
31/// Decode SupportedCalendarTypes attribute (0x0002)
32pub fn decode_supported_calendar_types(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
33    let mut res = Vec::new();
34    if let tlv::TlvItemValue::List(v) = inp {
35        for item in v {
36            if let tlv::TlvItemValue::Int(i) = &item.value {
37                res.push(*i as u8);
38            }
39        }
40    }
41    Ok(res)
42}
43
44
45// JSON dispatcher function
46
47/// Decode attribute value and return as JSON string
48/// 
49/// # Parameters
50/// * `cluster_id` - The cluster identifier
51/// * `attribute_id` - The attribute identifier
52/// * `tlv_value` - The TLV value to decode
53/// 
54/// # Returns
55/// JSON string representation of the decoded value or error
56pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
57    // Verify this is the correct cluster
58    if cluster_id != 0x002C {
59        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002C, got {}\"}}", cluster_id);
60    }
61    
62    match attribute_id {
63        0x0000 => {
64            match decode_hour_format(tlv_value) {
65                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
66                Err(e) => format!("{{\"error\": \"{}\"}}", e),
67            }
68        }
69        0x0001 => {
70            match decode_active_calendar_type(tlv_value) {
71                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
72                Err(e) => format!("{{\"error\": \"{}\"}}", e),
73            }
74        }
75        0x0002 => {
76            match decode_supported_calendar_types(tlv_value) {
77                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
78                Err(e) => format!("{{\"error\": \"{}\"}}", e),
79            }
80        }
81        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
82    }
83}
84
85/// Get list of all attributes supported by this cluster
86/// 
87/// # Returns
88/// Vector of tuples containing (attribute_id, attribute_name)
89pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
90    vec![
91        (0x0000, "HourFormat"),
92        (0x0001, "ActiveCalendarType"),
93        (0x0002, "SupportedCalendarTypes"),
94    ]
95}
96