matter-clusters 0.1.0

Matter protocol cluster definitions (generated from the spec).
Documentation
//! Switch cluster (0x003B).
//! @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 = 0x003B;
/// 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 {
    /// `NumberOfPositions`.
    pub const NUMBER_OF_POSITIONS: u32 = 0x0000;
    /// `CurrentPosition`.
    pub const CURRENT_POSITION: u32 = 0x0001;
    /// `MultiPressMax`.
    pub const MULTI_PRESS_MAX: u32 = 0x0002;
}

bitflags::bitflags! {
    /// `Switch` feature bits (FeatureMap).
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct Feature: u32 {
        /// LatchingSwitch (LS).
        const LS = 1 << 0;
        /// MomentarySwitch (MS).
        const MS = 1 << 1;
        /// MomentarySwitchRelease (MSR).
        const MSR = 1 << 2;
        /// MomentarySwitchLongPress (MSL).
        const MSL = 1 << 3;
        /// MomentarySwitchMultiPress (MSM).
        const MSM = 1 << 4;
        /// ActionSwitch (AS).
        const AS = 1 << 5;
    }
}

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

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

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