matter-clusters 0.1.0

Matter protocol cluster definitions (generated from the spec).
Documentation
//! IlluminanceMeasurement cluster (0x0400).
//! @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 = 0x0400;
/// Cluster revision.
pub const CLUSTER_REVISION: u16 = 4;

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

/// Attribute IDs (cluster-specific).
pub mod attribute_id {
    /// `MeasuredValue`.
    pub const MEASURED_VALUE: u32 = 0x0000;
    /// `MinMeasuredValue`.
    pub const MIN_MEASURED_VALUE: u32 = 0x0001;
    /// `MaxMeasuredValue`.
    pub const MAX_MEASURED_VALUE: u32 = 0x0002;
    /// `Tolerance`.
    pub const TOLERANCE: u32 = 0x0003;
    /// `LightSensorType`.
    pub const LIGHT_SENSOR_TYPE: u32 = 0x0004;
}

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

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

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

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

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

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

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