matc 0.1.2

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Content App Observer Cluster
//! Cluster ID: 0x0510
//!
//! This file is automatically generated from ContentAppObserver.xml

use crate::tlv;
use anyhow;


// Enum definitions

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum Status {
    /// Command succeeded
    Success = 0,
    /// Data field in command was not understood by the Observer
    Unexpecteddata = 1,
}

impl Status {
    /// Convert from u8 value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Status::Success),
            1 => Some(Status::Unexpecteddata),
            _ => None,
        }
    }

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

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

// Command encoders

/// Encode ContentAppMessage command (0x00)
pub fn encode_content_app_message(data: String, encoding_hint: String) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::String(data)).into(),
        (1, tlv::TlvItemValueEnc::String(encoding_hint)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

#[derive(Debug, serde::Serialize)]
pub struct ContentAppMessageResponse {
    pub status: Option<Status>,
    pub data: Option<String>,
    pub encoding_hint: Option<String>,
}

// Command response decoders

/// Decode ContentAppMessageResponse command response (01)
pub fn decode_content_app_message_response(inp: &tlv::TlvItemValue) -> anyhow::Result<ContentAppMessageResponse> {
    if let tlv::TlvItemValue::List(_fields) = inp {
        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
        Ok(ContentAppMessageResponse {
                status: item.get_int(&[0]).and_then(|v| Status::from_u8(v as u8)),
                data: item.get_string_owned(&[1]),
                encoding_hint: item.get_string_owned(&[2]),
        })
    } else {
        Err(anyhow::anyhow!("Expected struct fields"))
    }
}