matc/clusters/codec/
content_app_observer.rs

1//! Matter TLV encoders and decoders for Content App Observer Cluster
2//! Cluster ID: 0x0510
3//!
4//! This file is automatically generated from ContentAppObserver.xml
5
6use crate::tlv;
7use anyhow;
8
9
10// Enum definitions
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
13#[repr(u8)]
14pub enum Status {
15    /// Command succeeded
16    Success = 0,
17    /// Data field in command was not understood by the Observer
18    Unexpecteddata = 1,
19}
20
21impl Status {
22    /// Convert from u8 value
23    pub fn from_u8(value: u8) -> Option<Self> {
24        match value {
25            0 => Some(Status::Success),
26            1 => Some(Status::Unexpecteddata),
27            _ => None,
28        }
29    }
30
31    /// Convert to u8 value
32    pub fn to_u8(self) -> u8 {
33        self as u8
34    }
35}
36
37impl From<Status> for u8 {
38    fn from(val: Status) -> Self {
39        val as u8
40    }
41}
42
43// Command encoders
44
45/// Encode ContentAppMessage command (0x00)
46pub fn encode_content_app_message(data: String, encoding_hint: String) -> anyhow::Result<Vec<u8>> {
47    let tlv = tlv::TlvItemEnc {
48        tag: 0,
49        value: tlv::TlvItemValueEnc::StructInvisible(vec![
50        (0, tlv::TlvItemValueEnc::String(data)).into(),
51        (1, tlv::TlvItemValueEnc::String(encoding_hint)).into(),
52        ]),
53    };
54    Ok(tlv.encode()?)
55}
56
57#[derive(Debug, serde::Serialize)]
58pub struct ContentAppMessageResponse {
59    pub status: Option<Status>,
60    pub data: Option<String>,
61    pub encoding_hint: Option<String>,
62}
63
64// Command response decoders
65
66/// Decode ContentAppMessageResponse command response (01)
67pub fn decode_content_app_message_response(inp: &tlv::TlvItemValue) -> anyhow::Result<ContentAppMessageResponse> {
68    if let tlv::TlvItemValue::List(_fields) = inp {
69        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
70        Ok(ContentAppMessageResponse {
71                status: item.get_int(&[0]).and_then(|v| Status::from_u8(v as u8)),
72                data: item.get_string_owned(&[1]),
73                encoding_hint: item.get_string_owned(&[2]),
74        })
75    } else {
76        Err(anyhow::anyhow!("Expected struct fields"))
77    }
78}
79