use crate::tlv;
use anyhow;
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()?)
}
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()?)
}
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>,
}
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>,
}
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"))
}
}