Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InvalidLengthError #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading