tlv8 wronglength error more descriptive

This commit is contained in:
Alex Janka 2024-03-05 10:06:06 +11:00
parent a4e3e37e6b
commit 03a4835ed8

View file

@ -80,7 +80,10 @@ pub fn decode(data: &[u8]) -> Result<HashMap<u8, Vec<u8>>, TlvCodecError> {
let tlv_len = (data[1] as usize) + 2; let tlv_len = (data[1] as usize) + 2;
let current; let current;
if tlv_len > data.len() { if tlv_len > data.len() {
return Err(TlvCodecError::WrongLength); return Err(TlvCodecError::WrongLength {
expected: tlv_len,
got: data.len(),
});
} }
(current, data) = data.split_at(tlv_len); (current, data) = data.split_at(tlv_len);
@ -90,7 +93,10 @@ pub fn decode(data: &[u8]) -> Result<HashMap<u8, Vec<u8>>, TlvCodecError> {
} }
if current.len() < tlv_len { if current.len() < tlv_len {
return Err(TlvCodecError::WrongLength); return Err(TlvCodecError::WrongLength {
expected: tlv_len,
got: current.len(),
});
} }
let tlv_type = current[0]; let tlv_type = current[0];
@ -110,7 +116,7 @@ pub enum TlvCodecError {
#[error("too short")] #[error("too short")]
TooShort, TooShort,
#[error("wrong length")] #[error("wrong length")]
WrongLength, WrongLength { expected: usize, got: usize },
} }
pub trait TlvEncode { pub trait TlvEncode {