data type improvements
This commit is contained in:
parent
371937bf1f
commit
0077eaddb1
|
@ -24,7 +24,7 @@ pub struct DevicePairingData {
|
|||
#[serde(rename = "Connection")]
|
||||
pub connection: ConnectionType,
|
||||
#[serde(rename = "accessories")]
|
||||
pub accessories: Vec<Accessory>,
|
||||
pub accessories: Option<Vec<Accessory>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[derive(Debug)]
|
||||
pub enum TlvType {
|
||||
Method,
|
||||
Identifier,
|
||||
|
@ -41,3 +42,31 @@ impl From<TlvType> for u8 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TlvTypeDecodeError(pub u8);
|
||||
|
||||
impl TryFrom<u8> for TlvType {
|
||||
type Error = TlvTypeDecodeError;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, <TlvType as TryFrom<u8>>::Error> {
|
||||
match value {
|
||||
0 => Ok(TlvType::Method),
|
||||
1 => Ok(TlvType::Identifier),
|
||||
2 => Ok(TlvType::Salt),
|
||||
3 => Ok(TlvType::PublicKey),
|
||||
4 => Ok(TlvType::Proof),
|
||||
5 => Ok(TlvType::EncryptedData),
|
||||
6 => Ok(TlvType::State),
|
||||
7 => Ok(TlvType::Error),
|
||||
8 => Ok(TlvType::RetryDelay),
|
||||
9 => Ok(TlvType::Certificate),
|
||||
10 => Ok(TlvType::Signature),
|
||||
11 => Ok(TlvType::Permissions),
|
||||
12 => Ok(TlvType::FragmentData),
|
||||
13 => Ok(TlvType::FragmentLast),
|
||||
14 => Ok(TlvType::SessionID),
|
||||
_ => Err(TlvTypeDecodeError(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,35 @@ pub use data_types::TlvType;
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
pub trait FormatTlv {
|
||||
fn as_tlv_string(&self) -> String;
|
||||
}
|
||||
|
||||
impl FormatTlv for (u8, Vec<u8>) {
|
||||
fn as_tlv_string(&self) -> String {
|
||||
let (tlv_type, data) = self;
|
||||
(tlv_type, data).as_tlv_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatTlv for (&u8, &Vec<u8>) {
|
||||
fn as_tlv_string(&self) -> String {
|
||||
let (tlv_type, data) = self;
|
||||
let tlv_type = TlvType::try_from(**tlv_type).map_or_else(
|
||||
|e| format!("Unknown TLV type: {}", e.0),
|
||||
|v| format!("{v:?}"),
|
||||
);
|
||||
format!("{tlv_type}: {data:?} (length: {})", data.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatTlv for HashMap<u8, Vec<u8>> {
|
||||
fn as_tlv_string(&self) -> String {
|
||||
self.iter()
|
||||
.fold(String::new(), |a, v| format!("{a}\n{}", v.as_tlv_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode(data: &[u8]) -> Result<HashMap<u8, Vec<u8>>, TlvError> {
|
||||
let mut tlvs = HashMap::new();
|
||||
let mut data = data;
|
||||
|
|
Loading…
Reference in a new issue