matc/clusters/codec/
content_app_observer.rs1use crate::tlv;
7use anyhow;
8
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
13#[repr(u8)]
14pub enum Status {
15 Success = 0,
17 Unexpecteddata = 1,
19}
20
21impl Status {
22 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 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
43pub 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
64pub 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