matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Time Format Localization Cluster
//! Cluster ID: 0x002C
//!
//! This file is automatically generated from LocalizationTimeFormat.xml

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// Enum definitions

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum CalendarType {
    /// Dates conveyed using the Buddhist calendar
    Buddhist = 0,
    /// Dates conveyed using the Chinese calendar
    Chinese = 1,
    /// Dates conveyed using the Coptic calendar
    Coptic = 2,
    /// Dates conveyed using the Ethiopian calendar
    Ethiopian = 3,
    /// Dates conveyed using the Gregorian calendar
    Gregorian = 4,
    /// Dates conveyed using the Hebrew calendar
    Hebrew = 5,
    /// Dates conveyed using the Indian calendar
    Indian = 6,
    /// Dates conveyed using the Islamic calendar
    Islamic = 7,
    /// Dates conveyed using the Japanese calendar
    Japanese = 8,
    /// Dates conveyed using the Korean calendar
    Korean = 9,
    /// Dates conveyed using the Persian calendar
    Persian = 10,
    /// Dates conveyed using the Taiwanese calendar
    Taiwanese = 11,
    /// calendar implied from active locale
    Useactivelocale = 255,
}

impl CalendarType {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(CalendarType::Buddhist),
            1 => Some(CalendarType::Chinese),
            2 => Some(CalendarType::Coptic),
            3 => Some(CalendarType::Ethiopian),
            4 => Some(CalendarType::Gregorian),
            5 => Some(CalendarType::Hebrew),
            6 => Some(CalendarType::Indian),
            7 => Some(CalendarType::Islamic),
            8 => Some(CalendarType::Japanese),
            9 => Some(CalendarType::Korean),
            10 => Some(CalendarType::Persian),
            11 => Some(CalendarType::Taiwanese),
            255 => Some(CalendarType::Useactivelocale),
            _ => None,
        }
    }

    /// Convert to u8 value
    pub fn to_u8(self) -> u8 {
        self as u8
    }
}

impl From<CalendarType> for u8 {
    fn from(val: CalendarType) -> Self {
        val as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum HourFormat {
    /// Time conveyed with a 12-hour clock
    _12hr = 0,
    /// Time conveyed with a 24-hour clock
    _24hr = 1,
    /// Use active locale clock
    Useactivelocale = 255,
}

impl HourFormat {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(HourFormat::_12hr),
            1 => Some(HourFormat::_24hr),
            255 => Some(HourFormat::Useactivelocale),
            _ => None,
        }
    }

    /// Convert to u8 value
    pub fn to_u8(self) -> u8 {
        self as u8
    }
}

impl From<HourFormat> for u8 {
    fn from(val: HourFormat) -> Self {
        val as u8
    }
}

// Attribute decoders

/// Decode HourFormat attribute (0x0000)
pub fn decode_hour_format(inp: &tlv::TlvItemValue) -> anyhow::Result<HourFormat> {
    if let tlv::TlvItemValue::Int(v) = inp {
        HourFormat::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
    } else {
        Err(anyhow::anyhow!("Expected Integer"))
    }
}

/// Decode ActiveCalendarType attribute (0x0001)
pub fn decode_active_calendar_type(inp: &tlv::TlvItemValue) -> anyhow::Result<CalendarType> {
    if let tlv::TlvItemValue::Int(v) = inp {
        CalendarType::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
    } else {
        Err(anyhow::anyhow!("Expected Integer"))
    }
}

/// Decode SupportedCalendarTypes attribute (0x0002)
pub fn decode_supported_calendar_types(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<CalendarType>> {
    let mut res = Vec::new();
    if let tlv::TlvItemValue::List(v) = inp {
        for item in v {
            if let tlv::TlvItemValue::Int(i) = &item.value {
                if let Some(enum_val) = CalendarType::from_u8(*i as u8) {
                    res.push(enum_val);
                }
            }
        }
    }
    Ok(res)
}


// JSON dispatcher function

/// Decode attribute value and return as JSON string
///
/// # Parameters
/// * `cluster_id` - The cluster identifier
/// * `attribute_id` - The attribute identifier
/// * `tlv_value` - The TLV value to decode
///
/// # Returns
/// JSON string representation of the decoded value or error
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
    // Verify this is the correct cluster
    if cluster_id != 0x002C {
        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002C, got {}\"}}", cluster_id);
    }

    match attribute_id {
        0x0000 => {
            match decode_hour_format(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0001 => {
            match decode_active_calendar_type(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0002 => {
            match decode_supported_calendar_types(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
    }
}

/// Get list of all attributes supported by this cluster
///
/// # Returns
/// Vector of tuples containing (attribute_id, attribute_name)
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x0000, "HourFormat"),
        (0x0001, "ActiveCalendarType"),
        (0x0002, "SupportedCalendarTypes"),
    ]
}

// Typed facade (invokes + reads)

/// Read `HourFormat` attribute from cluster `Time Format Localization`.
pub async fn read_hour_format(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<HourFormat> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TIME_FORMAT_LOCALIZATION, crate::clusters::defs::CLUSTER_TIME_FORMAT_LOCALIZATION_ATTR_ID_HOURFORMAT).await?;
    decode_hour_format(&tlv)
}

/// Read `ActiveCalendarType` attribute from cluster `Time Format Localization`.
pub async fn read_active_calendar_type(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<CalendarType> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TIME_FORMAT_LOCALIZATION, crate::clusters::defs::CLUSTER_TIME_FORMAT_LOCALIZATION_ATTR_ID_ACTIVECALENDARTYPE).await?;
    decode_active_calendar_type(&tlv)
}

/// Read `SupportedCalendarTypes` attribute from cluster `Time Format Localization`.
pub async fn read_supported_calendar_types(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<CalendarType>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TIME_FORMAT_LOCALIZATION, crate::clusters::defs::CLUSTER_TIME_FORMAT_LOCALIZATION_ATTR_ID_SUPPORTEDCALENDARTYPES).await?;
    decode_supported_calendar_types(&tlv)
}