format tweaks

This commit is contained in:
Alex Janka 2024-02-22 11:19:49 +11:00
parent 0aa80012ac
commit 009aeabd45

View file

@ -19,6 +19,7 @@ impl FormatTlv for (u8, Vec<u8>) {
impl FormatTlv for (&u8, &Vec<u8>) {
fn as_tlv_string(&self) -> String {
let (tlv_type, data) = self;
let tlv_type_num = *self.0;
let tlv_type =
TlvType::try_from(**tlv_type).map_err(|e| format!("Unknown TLV type: {}", e.0));
match tlv_type {
@ -47,7 +48,10 @@ impl FormatTlv for (&u8, &Vec<u8>) {
)
})
.unwrap_or(String::from("TLV error type with no data!")),
_ => format!("{tlv_type:?}: {data:?} (length: {})", data.len()),
_ => format!(
"{tlv_type:?} ({tlv_type_num}): {data:?} (length: {})",
data.len()
),
},
Err(tlv_type) => format!("{tlv_type}: {data:?} (length: {})", data.len()),
}
@ -61,7 +65,7 @@ impl FormatTlv for HashMap<u8, Vec<u8>> {
}
}
impl FormatTlv for Vec<(u8, Vec<u8>)> {
impl FormatTlv for &[(u8, Vec<u8>)] {
fn as_tlv_string(&self) -> String {
self.iter()
.fold(String::new(), |a, v| format!("{a}\n{}", v.as_tlv_string()))
@ -97,7 +101,7 @@ pub fn decode(data: &[u8]) -> Result<HashMap<u8, Vec<u8>>, TlvCodecError> {
};
tlvs.insert(tlv_type, tlv_data);
}
log::info!("{}", tlvs.as_tlv_string());
log::info!("Decoded TLV: {}\n", tlvs.as_tlv_string());
Ok(tlvs)
}