From d98d4b55c8b1f2bb5c4af3ade4ccf9f2f9a8a409 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 28 Aug 2023 17:21:48 +0200 Subject: [PATCH 1/2] convert UnsigedByteFieldError variants to struct variants --- CHANGELOG.md | 2 ++ src/util.rs | 69 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3f4fd..6c5dda9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The `Tlv` and `Lv` API return `&[u8]` instead of `Option<&[u8]>`. - `ByteConversionError` error variants `ToSliceTooSmall` and `FromSliceTooSmall` are struct variants now. `SizeMissmatch` was removed appropriately. +- `UnsignedByteFieldError` error variants `ValueTooLargeForWidth` and `InvalidWidth` are struct + variants now. ## Added diff --git a/src/util.rs b/src/util.rs index 31aab5c..e440c0e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -82,10 +82,16 @@ pub trait UnsignedEnumExt: UnsignedEnum + Debug + Copy + Clone + PartialEq + Eq pub enum UnsignedByteFieldError { /// Value is too large for specified width of byte field. The first value contains the width, /// the second value contains the detected value. - ValueTooLargeForWidth((usize, u64)), + ValueTooLargeForWidth { + width: usize, + value: u64, + }, /// Only 1, 2, 4 and 8 are allow width values. Optionally contains the expected width if /// applicable, for example for conversions. - InvalidWidth(usize, Option), + InvalidWidth { + found: usize, + expected: Option, + }, ByteConversionError(ByteConversionError), } @@ -101,10 +107,10 @@ impl Display for UnsignedByteFieldError { Self::ByteConversionError(e) => { write!(f, "low level byte conversion error: {e}") } - Self::InvalidWidth(val, _) => { - write!(f, "invalid width {val}, only 1, 2, 4 and 8 are allowed.") + Self::InvalidWidth { found, .. } => { + write!(f, "invalid width {found}, only 1, 2, 4 and 8 are allowed.") } - Self::ValueTooLargeForWidth((width, value)) => { + Self::ValueTooLargeForWidth { width, value } => { write!(f, "value {value} too large for width {width}") } } @@ -154,7 +160,10 @@ impl UnsignedByteField { width, u64::from_be_bytes(buf[0..8].try_into().unwrap()), )), - _ => Err(UnsignedByteFieldError::InvalidWidth(width, None)), + _ => Err(UnsignedByteFieldError::InvalidWidth { + found: width, + expected: None, + }), } } } @@ -248,7 +257,10 @@ impl TryFrom for UnsignedByteFieldU8 { fn try_from(value: UnsignedByteField) -> Result { if value.width != 1 { - return Err(UnsignedByteFieldError::InvalidWidth(value.width, Some(1))); + return Err(UnsignedByteFieldError::InvalidWidth { + found: value.width, + expected: Some(1), + }); } Ok(Self::new(value.value as u8)) } @@ -265,7 +277,10 @@ impl TryFrom for UnsignedByteFieldU16 { fn try_from(value: UnsignedByteField) -> Result { if value.width != 2 { - return Err(UnsignedByteFieldError::InvalidWidth(value.width, Some(2))); + return Err(UnsignedByteFieldError::InvalidWidth { + found: value.width, + expected: Some(2), + }); } Ok(Self::new(value.value as u16)) } @@ -282,7 +297,10 @@ impl TryFrom for UnsignedByteFieldU32 { fn try_from(value: UnsignedByteField) -> Result { if value.width != 4 { - return Err(UnsignedByteFieldError::InvalidWidth(value.width, Some(4))); + return Err(UnsignedByteFieldError::InvalidWidth { + found: value.width, + expected: Some(4), + }); } Ok(Self::new(value.value as u32)) } @@ -299,7 +317,10 @@ impl TryFrom for UnsignedByteFieldU64 { fn try_from(value: UnsignedByteField) -> Result { if value.width != 8 { - return Err(UnsignedByteFieldError::InvalidWidth(value.width, Some(8))); + return Err(UnsignedByteFieldError::InvalidWidth { + found: value.width, + expected: Some(8), + }); } Ok(Self::new(value.value)) } @@ -393,8 +414,11 @@ pub mod tests { assert!(conv_fails.is_err()); let err = conv_fails.unwrap_err(); match err { - UnsignedByteFieldError::InvalidWidth(width, Some(expected)) => { - assert_eq!(width, 2); + UnsignedByteFieldError::InvalidWidth { + found, + expected: Some(expected), + } => { + assert_eq!(found, 2); assert_eq!(expected, 1); } _ => { @@ -422,8 +446,11 @@ pub mod tests { assert!(conv_fails.is_err()); let err = conv_fails.unwrap_err(); match err { - UnsignedByteFieldError::InvalidWidth(width, Some(expected)) => { - assert_eq!(width, 4); + UnsignedByteFieldError::InvalidWidth { + found, + expected: Some(expected), + } => { + assert_eq!(found, 4); assert_eq!(expected, 2); } _ => { @@ -451,8 +478,11 @@ pub mod tests { assert!(conv_fails.is_err()); let err = conv_fails.unwrap_err(); match err { - UnsignedByteFieldError::InvalidWidth(width, Some(expected)) => { - assert_eq!(width, 8); + UnsignedByteFieldError::InvalidWidth { + found, + expected: Some(expected), + } => { + assert_eq!(found, 8); assert_eq!(expected, 4); } _ => { @@ -480,8 +510,11 @@ pub mod tests { assert!(conv_fails.is_err()); let err = conv_fails.unwrap_err(); match err { - UnsignedByteFieldError::InvalidWidth(width, Some(expected)) => { - assert_eq!(width, 4); + UnsignedByteFieldError::InvalidWidth { + found, + expected: Some(expected), + } => { + assert_eq!(found, 4); assert_eq!(expected, 8); } _ => { From 925b2aa8d80cebd2c6c0c59658077756fe3e47c1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 28 Aug 2023 17:22:36 +0200 Subject: [PATCH 2/2] remove obsolete comment --- src/util.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util.rs b/src/util.rs index e440c0e..b944763 100644 --- a/src/util.rs +++ b/src/util.rs @@ -80,8 +80,7 @@ pub trait UnsignedEnumExt: UnsignedEnum + Debug + Copy + Clone + PartialEq + Eq #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum UnsignedByteFieldError { - /// Value is too large for specified width of byte field. The first value contains the width, - /// the second value contains the detected value. + /// Value is too large for specified width of byte field. ValueTooLargeForWidth { width: usize, value: u64,