matc 0.1.3

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

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// 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: Option<String>) -> anyhow::Result<Vec<u8>> {
    let mut tlv_fields: Vec<tlv::TlvItemEnc> = Vec::new();
    tlv_fields.push((0, tlv::TlvItemValueEnc::String(data)).into());
    if let Some(x) = encoding_hint { tlv_fields.push((1, tlv::TlvItemValueEnc::String(x)).into()); }
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(tlv_fields),
    };
    Ok(tlv.encode()?)
}

// Command listing

pub fn get_command_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x00, "ContentAppMessage"),
    ]
}

pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
    match cmd_id {
        0x00 => Some("ContentAppMessage"),
        _ => None,
    }
}

pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
    match cmd_id {
        0x00 => Some(vec![
            crate::clusters::codec::CommandField { tag: 0, name: "data", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
            crate::clusters::codec::CommandField { tag: 1, name: "encoding_hint", kind: crate::clusters::codec::FieldKind::String, optional: true, nullable: false },
        ]),
        _ => None,
    }
}

pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
    match cmd_id {
        0x00 => {
        let data = crate::clusters::codec::json_util::get_string(args, "data")?;
        let encoding_hint = crate::clusters::codec::json_util::get_opt_string(args, "encoding_hint")?;
        encode_content_app_message(data, encoding_hint)
        }
        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
    }
}

#[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"))
    }
}

// Typed facade (invokes + reads)

/// Invoke `ContentAppMessage` command on cluster `Content App Observer`.
pub async fn content_app_message(conn: &crate::controller::Connection, endpoint: u16, data: String, encoding_hint: Option<String>) -> anyhow::Result<ContentAppMessageResponse> {
    let tlv = conn.invoke_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CONTENT_APP_OBSERVER, crate::clusters::defs::CLUSTER_CONTENT_APP_OBSERVER_CMD_ID_CONTENTAPPMESSAGE, &encode_content_app_message(data, encoding_hint)?).await?;
    decode_content_app_message_response(&tlv)
}