matter-clusters 0.3.0

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

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

/// Attribute IDs (cluster-specific).
pub mod attribute_id {
    /// `AirQuality`.
    pub const AIR_QUALITY: u32 = 0x0000;
}

bitflags::bitflags! {
    /// `AirQuality` feature bits (FeatureMap).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct Feature: u32 {
        /// Fair (FAIR).
        const FAIR = 1 << 0;
        /// Moderate (MOD).
        const MOD = 1 << 1;
        /// VeryPoor (VPOOR).
        const VPOOR = 1 << 2;
        /// ExtremelyPoor (XPOOR).
        const XPOOR = 1 << 3;
    }
}

/// `AirQualityEnum` (enum8).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AirQualityEnum {
    /// Unknown = 0.
    Unknown,
    /// Good = 1.
    Good,
    /// Fair = 2.
    Fair,
    /// Moderate = 3.
    Moderate,
    /// Poor = 4.
    Poor,
    /// VeryPoor = 5.
    VeryPoor,
    /// ExtremelyPoor = 6.
    ExtremelyPoor,
    /// A value not known to this codegen revision.
    Unrecognized(u8),
}

impl AirQualityEnum {
    /// Decode from its raw discriminant (unknown → `Unrecognized`).
    #[must_use]
    pub fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::Unknown,
            1 => Self::Good,
            2 => Self::Fair,
            3 => Self::Moderate,
            4 => Self::Poor,
            5 => Self::VeryPoor,
            6 => Self::ExtremelyPoor,
            other => Self::Unrecognized(other),
        }
    }
    /// The raw discriminant.
    #[must_use]
    pub fn to_raw(self) -> u8 {
        match self {
            Self::Unknown => 0,
            Self::Good => 1,
            Self::Fair => 2,
            Self::Moderate => 3,
            Self::Poor => 4,
            Self::VeryPoor => 5,
            Self::ExtremelyPoor => 6,
            Self::Unrecognized(v) => v,
        }
    }
}

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