Skip to content

Commit

Permalink
Move error code to error module
Browse files Browse the repository at this point in the history
In preparation for adding a bunch more error types move the current ones
out of `parse` into `error`.
  • Loading branch information
tcharding committed Nov 6, 2023
1 parent 9c6df3e commit 881c098
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 69 deletions.
71 changes: 71 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: CC0-1.0

use core::fmt;

/// Formats error.
///
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
Expand All @@ -21,3 +23,72 @@ macro_rules! write_err {
}
}
}

/// Hex decoding error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum HexToBytesError {
/// Non-hexadecimal character.
InvalidChar(u8),
/// Purported hex string had odd length.
OddLengthString(usize),
}

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

match *self {
InvalidChar(ch) => write!(f, "invalid hex character {}", ch),
OddLengthString(ell) => write!(f, "odd hex string length {}", ell),
}
}
}

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

match self {
InvalidChar(_) | OddLengthString(_) => None,
}
}
}

/// Hex decoding error.
#[derive(Debug, Copy, 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),
}

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),
}
}
}

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

match *self {
Conversion(ref e) => Some(e),
InvalidLength(_, _) => None,
}
}
}

impl From<HexToBytesError> for HexToArrayError {
#[inline]
fn from(e: HexToBytesError) -> Self { Self::Conversion(e) }
}
72 changes: 3 additions & 69 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use core::{fmt, str};
use crate::alloc::vec::Vec;
use crate::iter::HexToBytesIter;

#[rustfmt::skip] // Keep public re-exports separate.
pub use crate::error::{HexToBytesError, HexToArrayError};

/// Trait for objects that can be deserialized from hex strings.
pub trait FromHex: Sized {
/// Error type returned while parsing hex string.
Expand Down Expand Up @@ -37,37 +40,6 @@ impl FromHex for Vec<u8> {
}
}

/// Hex decoding error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum HexToBytesError {
/// Non-hexadecimal character.
InvalidChar(u8),
/// Purported hex string had odd length.
OddLengthString(usize),
}

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

match *self {
InvalidChar(ch) => write!(f, "invalid hex character {}", ch),
OddLengthString(ell) => write!(f, "odd hex string length {}", ell),
}
}
}

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

match self {
InvalidChar(_) | OddLengthString(_) => None,
}
}
}

macro_rules! impl_fromhex_array {
($len:expr) => {
impl FromHex for [u8; $len] {
Expand Down Expand Up @@ -113,44 +85,6 @@ impl_fromhex_array!(256);
impl_fromhex_array!(384);
impl_fromhex_array!(512);

/// Hex decoding error.
#[derive(Debug, Copy, 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),
}

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),
}
}
}

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

match *self {
Conversion(ref e) => Some(e),
InvalidLength(_, _) => None,
}
}
}

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

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 881c098

Please sign in to comment.