Skip to content

Commit

Permalink
Add InvalidLengthError
Browse files Browse the repository at this point in the history
In order to make adding more information to our error types easier on
downstream hide the internals of the invalid length error.
  • Loading branch information
tcharding committed Oct 27, 2023
1 parent 2e25d6c commit e36d68f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a> fmt::UpperHex for DisplayALittleBitHexy<'a> {
}

/// Example Error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
/// Conversion error while parsing hex string.
Conversion(HexToBytesError),
Expand Down
44 changes: 36 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::{fmt, str};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::vec::Vec;
use crate::iter::HexToBytesIter;
use crate::write_err;

/// Trait for objects that can be deserialized from hex strings.
pub trait FromHex: Sized {
Expand Down Expand Up @@ -86,7 +87,9 @@ macro_rules! impl_fromhex_array {
}
Ok(ret)
} else {
Err(HexToArrayError::InvalidLength(2 * $len, 2 * iter.len()))
let expected = 2 * $len;
let got = 2 * iter.len();
Err(InvalidLengthError { expected, got }.into())
}
}
}
Expand Down Expand Up @@ -114,22 +117,21 @@ impl_fromhex_array!(384);
impl_fromhex_array!(512);

/// Hex decoding error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HexToArrayError {
/// Conversion error while parsing hex string.
Conversion(HexToBytesError),
/// Tried to parse fixed-length hash from a string with the wrong length (got, want).
InvalidLength(usize, usize),
InvalidLength(InvalidLengthError),
}

impl fmt::Display for HexToArrayError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use HexToArrayError::*;

match *self {
Conversion(ref e) => crate::write_err!(f, "conversion error"; e),
InvalidLength(got, want) =>
write!(f, "bad hex string length {} (expected {})", got, want),
Conversion(ref e) => write_err!(f, "conversion error"; e),
InvalidLength(ref e) => write_err!(f, "invalid length hex for array"; e),
}
}
}
Expand All @@ -141,7 +143,7 @@ impl std::error::Error for HexToArrayError {

match *self {
Conversion(ref e) => Some(e),
InvalidLength(_, _) => None,
InvalidLength(ref e) => Some(e),
}
}
}
Expand All @@ -151,6 +153,29 @@ impl From<HexToBytesError> for HexToArrayError {
fn from(e: HexToBytesError) -> Self { Self::Conversion(e) }
}

impl From<InvalidLengthError> for HexToArrayError {
#[inline]
fn from(e: InvalidLengthError) -> Self { Self::InvalidLength(e) }
}

/// Tried to parse fixed-length hash from a string with the wrong length.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidLengthError {
expected: usize,
got: usize,
}

impl fmt::Display for InvalidLengthError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "bad hex string length {} (expected {})", self.got, self.expected)
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidLengthError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -185,7 +210,10 @@ mod tests {
fn hex_to_array_error() {
use HexToArrayError::*;
let len_sixteen = "0123456789abcdef";
assert_eq!(<[u8; 4]>::from_hex(len_sixteen), Err(InvalidLength(8, 16)));
assert_eq!(
<[u8; 4]>::from_hex(len_sixteen),
Err(InvalidLength(InvalidLengthError { expected: 8, got: 16 }))
);
}

#[test]
Expand Down

0 comments on commit e36d68f

Please sign in to comment.