matc 0.1.2

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Account Login Cluster
//! Cluster ID: 0x050E
//!
//! This file is automatically generated from AccountLogin.xml

use crate::tlv;
use anyhow;


// Command encoders

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

/// Encode Login command (0x02)
pub fn encode_login(temp_account_identifier: String, setup_pin: String, node: u64) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::String(temp_account_identifier)).into(),
        (1, tlv::TlvItemValueEnc::String(setup_pin)).into(),
        (2, tlv::TlvItemValueEnc::UInt64(node)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

/// Encode Logout command (0x03)
pub fn encode_logout(node: u64) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::UInt64(node)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

#[derive(Debug, serde::Serialize)]
pub struct GetSetupPINResponse {
    pub setup_pin: Option<String>,
}

// Command response decoders

/// Decode GetSetupPINResponse command response (01)
pub fn decode_get_setup_pin_response(inp: &tlv::TlvItemValue) -> anyhow::Result<GetSetupPINResponse> {
    if let tlv::TlvItemValue::List(_fields) = inp {
        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
        Ok(GetSetupPINResponse {
                setup_pin: item.get_string_owned(&[0]),
        })
    } else {
        Err(anyhow::anyhow!("Expected struct fields"))
    }
}

#[derive(Debug, serde::Serialize)]
pub struct LoggedOutEvent {
    pub node: Option<u64>,
}

// Event decoders

/// Decode LoggedOut event (0x00, priority: critical)
pub fn decode_logged_out_event(inp: &tlv::TlvItemValue) -> anyhow::Result<LoggedOutEvent> {
    if let tlv::TlvItemValue::List(_fields) = inp {
        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
        Ok(LoggedOutEvent {
                                node: item.get_int(&[0]),
        })
    } else {
        Err(anyhow::anyhow!("Expected struct fields"))
    }
}