matc/clusters/codec/
localization_time_format.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum CalendarType {
16 Buddhist = 0,
18 Chinese = 1,
20 Coptic = 2,
22 Ethiopian = 3,
24 Gregorian = 4,
26 Hebrew = 5,
28 Indian = 6,
30 Islamic = 7,
32 Japanese = 8,
34 Korean = 9,
36 Persian = 10,
38 Taiwanese = 11,
40 Useactivelocale = 255,
42}
43
44impl CalendarType {
45 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 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 _12hr = 0,
82 _24hr = 1,
84 Useactivelocale = 255,
86}
87
88impl HourFormat {
89 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 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
111pub 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
122pub 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
131pub 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
147pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
159 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
187pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
192 vec![
193 (0x0000, "HourFormat"),
194 (0x0001, "ActiveCalendarType"),
195 (0x0002, "SupportedCalendarTypes"),
196 ]
197}
198