1use core::fmt;
5
6use hex::FromHexError;
7
8#[derive(Debug, PartialEq, Eq)]
9pub enum Error {
10 InvalidPrefix { c0: char, c1: char },
11 InvalidHexCharacter { c: char, index: usize },
12 InvalidStringLength,
13 InvalidStringLengthSlice { expected: usize, actual: usize },
14 OddLength,
15}
16
17#[cfg(feature = "std")]
18impl std::error::Error for Error {}
19
20impl From<FromHexError> for Error {
21 fn from(v: FromHexError) -> Error {
22 match v {
23 FromHexError::InvalidHexCharacter { c, index } => Error::InvalidHexCharacter { c, index },
24 FromHexError::InvalidStringLength => Error::InvalidStringLength,
25 FromHexError::OddLength => Error::OddLength,
26 }
27 }
28}
29
30impl fmt::Display for Error {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Error::InvalidPrefix { c0, c1 } => {
34 write!(f, "Invalid hex prefix: expected `0x` but got `{c0}{c1}`")
35 }
36 Error::InvalidHexCharacter { c, index } => {
37 write!(f, "Invalid hex character {:?} at position {}", c, index)
38 }
39 Error::InvalidStringLength => write!(f, "Invalid hex string length"),
40 Error::InvalidStringLengthSlice { expected, actual } => write!(
41 f,
42 "Invalid hex string length for slice: expected {expected} got {actual}"
43 ),
44 Error::OddLength => write!(f, "Odd number of digits in hex string"),
45 }
46 }
47}