Add from/to TLV for i16, i32 and i64

This commit is contained in:
ivmarkov 2023-06-16 18:42:11 +00:00
parent 28ec278756
commit 8e2340363f
2 changed files with 29 additions and 2 deletions

View file

@ -357,6 +357,14 @@ impl<'a> TLVElement<'a> {
} }
} }
pub fn i16(&self) -> Result<i16, Error> {
match self.element_type {
ElementType::S8(a) => Ok(a.into()),
ElementType::S16(a) => Ok(a),
_ => Err(ErrorCode::TLVTypeMismatch.into()),
}
}
pub fn u16(&self) -> Result<u16, Error> { pub fn u16(&self) -> Result<u16, Error> {
match self.element_type { match self.element_type {
ElementType::U8(a) => Ok(a.into()), ElementType::U8(a) => Ok(a.into()),
@ -365,6 +373,15 @@ impl<'a> TLVElement<'a> {
} }
} }
pub fn i32(&self) -> Result<i32, Error> {
match self.element_type {
ElementType::S8(a) => Ok(a.into()),
ElementType::S16(a) => Ok(a.into()),
ElementType::S32(a) => Ok(a),
_ => Err(ErrorCode::TLVTypeMismatch.into()),
}
}
pub fn u32(&self) -> Result<u32, Error> { pub fn u32(&self) -> Result<u32, Error> {
match self.element_type { match self.element_type {
ElementType::U8(a) => Ok(a.into()), ElementType::U8(a) => Ok(a.into()),
@ -374,6 +391,16 @@ impl<'a> TLVElement<'a> {
} }
} }
pub fn i64(&self) -> Result<i64, Error> {
match self.element_type {
ElementType::S8(a) => Ok(a.into()),
ElementType::S16(a) => Ok(a.into()),
ElementType::S32(a) => Ok(a.into()),
ElementType::S64(a) => Ok(a),
_ => Err(ErrorCode::TLVTypeMismatch.into()),
}
}
pub fn u64(&self) -> Result<u64, Error> { pub fn u64(&self) -> Result<u64, Error> {
match self.element_type { match self.element_type {
ElementType::U8(a) => Ok(a.into()), ElementType::U8(a) => Ok(a.into()),

View file

@ -91,7 +91,7 @@ macro_rules! fromtlv_for {
}; };
} }
fromtlv_for!(u8 u16 u32 u64 bool); fromtlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool);
pub trait ToTLV { pub trait ToTLV {
fn to_tlv(&self, tw: &mut TLVWriter, tag: TagType) -> Result<(), Error>; fn to_tlv(&self, tw: &mut TLVWriter, tag: TagType) -> Result<(), Error>;
@ -139,7 +139,7 @@ impl<'a, T: ToTLV> ToTLV for &'a [T] {
} }
// Generate ToTLV for standard data types // Generate ToTLV for standard data types
totlv_for!(i8 u8 u16 u32 u64 bool); totlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool);
// We define a few common data types that will be required here // We define a few common data types that will be required here
// //