hex_conservative/
error.rsuse core::fmt;
use crate::write_err;
#[macro_export]
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HexToBytesError {
InvalidChar(InvalidCharError),
OddLengthString(OddLengthStringError),
}
impl fmt::Display for HexToBytesError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use HexToBytesError::*;
match *self {
InvalidChar(ref e) => write_err!(f, "invalid char, failed to create bytes from hex"; e),
OddLengthString(ref e) =>
write_err!(f, "odd length, failed to create bytes from hex"; e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for HexToBytesError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use HexToBytesError::*;
match *self {
InvalidChar(ref e) => Some(e),
OddLengthString(ref e) => Some(e),
}
}
}
impl From<InvalidCharError> for HexToBytesError {
#[inline]
fn from(e: InvalidCharError) -> Self { Self::InvalidChar(e) }
}
impl From<OddLengthStringError> for HexToBytesError {
#[inline]
fn from(e: OddLengthStringError) -> Self { Self::OddLengthString(e) }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidCharError {
pub(crate) invalid: u8,
pub(crate) pos: usize,
}
impl InvalidCharError {
pub fn invalid_char(&self) -> u8 { self.invalid }
pub fn pos(&self) -> usize { self.pos }
}
impl fmt::Display for InvalidCharError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid hex char {} at pos {}", self.invalid, self.pos)
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidCharError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OddLengthStringError {
pub(crate) len: usize,
}
impl OddLengthStringError {
pub fn length(&self) -> usize { self.len }
}
impl fmt::Display for OddLengthStringError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "odd hex string length {}", self.len)
}
}
#[cfg(feature = "std")]
impl std::error::Error for OddLengthStringError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HexToArrayError {
InvalidChar(InvalidCharError),
InvalidLength(InvalidLengthError),
}
impl fmt::Display for HexToArrayError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use HexToArrayError::*;
match *self {
InvalidChar(ref e) => crate::write_err!(f, "failed to parse hex digit"; e),
InvalidLength(ref e) => write_err!(f, "failed to parse hex"; e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for HexToArrayError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use HexToArrayError::*;
match *self {
InvalidChar(ref e) => Some(e),
InvalidLength(ref e) => Some(e),
}
}
}
impl From<InvalidCharError> for HexToArrayError {
#[inline]
fn from(e: InvalidCharError) -> Self { Self::InvalidChar(e) }
}
impl From<InvalidLengthError> for HexToArrayError {
#[inline]
fn from(e: InvalidLengthError) -> Self { Self::InvalidLength(e) }
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InvalidLengthError {
pub expected: usize,
pub invalid: usize,
}
impl fmt::Display for InvalidLengthError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invilad hex string length {} (expected {})", self.invalid, self.expected)
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidLengthError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}