matter-clusters 0.3.0

Matter protocol cluster definitions (generated from the spec).
Documentation
//! ThermostatUserInterfaceConfiguration cluster (0x0204).
//! @generated by `cargo xtask codegen` — do not edit.

#![allow(
    clippy::all,
    clippy::pedantic,
    dead_code,
    unreachable_pub,
    unused_imports
)]

use crate::datatypes::SemanticTagStruct;
use crate::error::ClusterError;
use crate::types::Nullable;
use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};

/// Cluster ID.
pub const CLUSTER_ID: u32 = 0x0204;
/// Cluster revision.
pub const CLUSTER_REVISION: u16 = 2;

/// Command IDs (requests and responses).
pub mod command_id {}

/// Attribute IDs (cluster-specific).
pub mod attribute_id {
    /// `TemperatureDisplayMode`.
    pub const TEMPERATURE_DISPLAY_MODE: u32 = 0x0000;
    /// `KeypadLockout`.
    pub const KEYPAD_LOCKOUT: u32 = 0x0001;
    /// `ScheduleProgrammingVisibility`.
    pub const SCHEDULE_PROGRAMMING_VISIBILITY: u32 = 0x0002;
}

/// `KeypadLockoutEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum KeypadLockoutEnum {
    /// NoLockout = 0.
    NoLockout,
    /// Lockout1 = 1.
    Lockout1,
    /// Lockout2 = 2.
    Lockout2,
    /// Lockout3 = 3.
    Lockout3,
    /// Lockout4 = 4.
    Lockout4,
    /// Lockout5 = 5.
    Lockout5,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl KeypadLockoutEnum {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::NoLockout,
            1 => Self::Lockout1,
            2 => Self::Lockout2,
            3 => Self::Lockout3,
            4 => Self::Lockout4,
            5 => Self::Lockout5,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::NoLockout => 0,
            Self::Lockout1 => 1,
            Self::Lockout2 => 2,
            Self::Lockout3 => 3,
            Self::Lockout4 => 4,
            Self::Lockout5 => 5,
            Self::Unknown(v) => v,
        }
    }
}

/// `ScheduleProgrammingVisibilityEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ScheduleProgrammingVisibilityEnum {
    /// ScheduleProgrammingPermitted = 0.
    ScheduleProgrammingPermitted,
    /// ScheduleProgrammingDenied = 1.
    ScheduleProgrammingDenied,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl ScheduleProgrammingVisibilityEnum {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::ScheduleProgrammingPermitted,
            1 => Self::ScheduleProgrammingDenied,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::ScheduleProgrammingPermitted => 0,
            Self::ScheduleProgrammingDenied => 1,
            Self::Unknown(v) => v,
        }
    }
}

/// `TemperatureDisplayModeEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TemperatureDisplayModeEnum {
    /// Celsius = 0.
    Celsius,
    /// Fahrenheit = 1.
    Fahrenheit,
    /// A value not known to this codegen revision.
    Unknown(u8),
}

impl TemperatureDisplayModeEnum {
    /// Decode from its raw discriminant (unknown → `Unknown`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Celsius,
            1 => Self::Fahrenheit,
            other => Self::Unknown(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Celsius => 0,
            Self::Fahrenheit => 1,
            Self::Unknown(v) => v,
        }
    }
}

/// Decode the `TemperatureDisplayMode` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_temperature_display_mode(
    tlv: &[u8],
) -> Result<TemperatureDisplayModeEnum, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(TemperatureDisplayModeEnum::from_raw(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("TemperatureDisplayMode"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "TemperatureDisplayMode",
        }),
    }
}

/// Encode the `TemperatureDisplayMode` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_temperature_display_mode(value: TemperatureDisplayModeEnum) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_uint(Tag::Anonymous, u64::from(value.to_raw()))
        .expect("infallible: vec writer");
    buf
}

/// Decode the `KeypadLockout` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_keypad_lockout(tlv: &[u8]) -> Result<KeypadLockoutEnum, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(KeypadLockoutEnum::from_raw(
            u8::try_from(v).map_err(|_| ClusterError::InvalidLength("KeypadLockout"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "KeypadLockout",
        }),
    }
}

/// Encode the `KeypadLockout` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_keypad_lockout(value: KeypadLockoutEnum) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_uint(Tag::Anonymous, u64::from(value.to_raw()))
        .expect("infallible: vec writer");
    buf
}

/// Decode the `ScheduleProgrammingVisibility` attribute value.
///
/// # Errors
/// Returns [`ClusterError`] on a type mismatch or out-of-range value.
pub fn decode_schedule_programming_visibility(
    tlv: &[u8],
) -> Result<ScheduleProgrammingVisibilityEnum, ClusterError> {
    let mut r = TlvReader::new(tlv);
    match r.next()? {
        Some(Element::Scalar {
            value: Value::Uint(v),
            ..
        }) => Ok(ScheduleProgrammingVisibilityEnum::from_raw(
            u8::try_from(v)
                .map_err(|_| ClusterError::InvalidLength("ScheduleProgrammingVisibility"))?,
        )),
        _ => Err(ClusterError::UnexpectedType {
            context: "ScheduleProgrammingVisibility",
        }),
    }
}

/// Encode the `ScheduleProgrammingVisibility` attribute value as a standalone TLV element.
#[must_use]
#[allow(clippy::expect_used, clippy::missing_panics_doc)] // Vec-backed TlvWriter is infallible.
pub fn encode_schedule_programming_visibility(value: ScheduleProgrammingVisibilityEnum) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.put_uint(Tag::Anonymous, u64::from(value.to_raw()))
        .expect("infallible: vec writer");
    buf
}