matc/clusters/codec/
localization_time_format.rs

1//! 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// Enum definitions
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum CalendarType {
16    /// Dates conveyed using the Buddhist calendar
17    Buddhist = 0,
18    /// Dates conveyed using the Chinese calendar
19    Chinese = 1,
20    /// Dates conveyed using the Coptic calendar
21    Coptic = 2,
22    /// Dates conveyed using the Ethiopian calendar
23    Ethiopian = 3,
24    /// Dates conveyed using the Gregorian calendar
25    Gregorian = 4,
26    /// Dates conveyed using the Hebrew calendar
27    Hebrew = 5,
28    /// Dates conveyed using the Indian calendar
29    Indian = 6,
30    /// Dates conveyed using the Islamic calendar
31    Islamic = 7,
32    /// Dates conveyed using the Japanese calendar
33    Japanese = 8,
34    /// Dates conveyed using the Korean calendar
35    Korean = 9,
36    /// Dates conveyed using the Persian calendar
37    Persian = 10,
38    /// Dates conveyed using the Taiwanese calendar
39    Taiwanese = 11,
40    /// calendar implied from active locale
41    Useactivelocale = 255,
42}
43
44impl CalendarType {
45    /// Convert from u8 value
46    pub fn from_u8(value: u8) -> Option<Self> {
47        match value {
48            0 => Some(CalendarType::Buddhist),
49            1 => Some(CalendarType::Chinese),
50            2 => Some(CalendarType::Coptic),
51            3 => Some(CalendarType::Ethiopian),
52            4 => Some(CalendarType::Gregorian),
53            5 => Some(CalendarType::Hebrew),
54            6 => Some(CalendarType::Indian),
55            7 => Some(CalendarType::Islamic),
56            8 => Some(CalendarType::Japanese),
57            9 => Some(CalendarType::Korean),
58            10 => Some(CalendarType::Persian),
59            11 => Some(CalendarType::Taiwanese),
60            255 => Some(CalendarType::Useactivelocale),
61            _ => None,
62        }
63    }
64
65    /// Convert to u8 value
66    pub fn to_u8(self) -> u8 {
67        self as u8
68    }
69}
70
71impl From<CalendarType> for u8 {
72    fn from(val: CalendarType) -> Self {
73        val as u8
74    }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
78#[repr(u8)]
79pub enum HourFormat {
80    /// Time conveyed with a 12-hour clock
81    _12hr = 0,
82    /// Time conveyed with a 24-hour clock
83    _24hr = 1,
84    /// Use active locale clock
85    Useactivelocale = 255,
86}
87
88impl HourFormat {
89    /// Convert from u8 value
90    pub fn from_u8(value: u8) -> Option<Self> {
91        match value {
92            0 => Some(HourFormat::_12hr),
93            1 => Some(HourFormat::_24hr),
94            255 => Some(HourFormat::Useactivelocale),
95            _ => None,
96        }
97    }
98
99    /// Convert to u8 value
100    pub fn to_u8(self) -> u8 {
101        self as u8
102    }
103}
104
105impl From<HourFormat> for u8 {
106    fn from(val: HourFormat) -> Self {
107        val as u8
108    }
109}
110
111// Attribute decoders
112
113/// Decode HourFormat attribute (0x0000)
114pub fn decode_hour_format(inp: &tlv::TlvItemValue) -> anyhow::Result<HourFormat> {
115    if let tlv::TlvItemValue::Int(v) = inp {
116        HourFormat::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
117    } else {
118        Err(anyhow::anyhow!("Expected Integer"))
119    }
120}
121
122/// Decode ActiveCalendarType attribute (0x0001)
123pub fn decode_active_calendar_type(inp: &tlv::TlvItemValue) -> anyhow::Result<CalendarType> {
124    if let tlv::TlvItemValue::Int(v) = inp {
125        CalendarType::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
126    } else {
127        Err(anyhow::anyhow!("Expected Integer"))
128    }
129}
130
131/// Decode SupportedCalendarTypes attribute (0x0002)
132pub fn decode_supported_calendar_types(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<CalendarType>> {
133    let mut res = Vec::new();
134    if let tlv::TlvItemValue::List(v) = inp {
135        for item in v {
136            if let tlv::TlvItemValue::Int(i) = &item.value {
137                if let Some(enum_val) = CalendarType::from_u8(*i as u8) {
138                    res.push(enum_val);
139                }
140            }
141        }
142    }
143    Ok(res)
144}
145
146
147// JSON dispatcher function
148
149/// Decode attribute value and return as JSON string
150///
151/// # Parameters
152/// * `cluster_id` - The cluster identifier
153/// * `attribute_id` - The attribute identifier
154/// * `tlv_value` - The TLV value to decode
155///
156/// # Returns
157/// JSON string representation of the decoded value or error
158pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
159    // Verify this is the correct cluster
160    if cluster_id != 0x002C {
161        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002C, got {}\"}}", cluster_id);
162    }
163
164    match attribute_id {
165        0x0000 => {
166            match decode_hour_format(tlv_value) {
167                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
168                Err(e) => format!("{{\"error\": \"{}\"}}", e),
169            }
170        }
171        0x0001 => {
172            match decode_active_calendar_type(tlv_value) {
173                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
174                Err(e) => format!("{{\"error\": \"{}\"}}", e),
175            }
176        }
177        0x0002 => {
178            match decode_supported_calendar_types(tlv_value) {
179                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
180                Err(e) => format!("{{\"error\": \"{}\"}}", e),
181            }
182        }
183        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
184    }
185}
186
187/// Get list of all attributes supported by this cluster
188///
189/// # Returns
190/// Vector of tuples containing (attribute_id, attribute_name)
191pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
192    vec![
193        (0x0000, "HourFormat"),
194        (0x0001, "ActiveCalendarType"),
195        (0x0002, "SupportedCalendarTypes"),
196    ]
197}
198