1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use core::fmt;

use hex::FromHexError;

#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    InvalidPrefix { c0: char, c1: char },
    InvalidHexCharacter { c: char, index: usize },
    InvalidStringLength,
    InvalidStringLengthSlice { expected: usize, actual: usize },
    OddLength,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<FromHexError> for Error {
    fn from(v: FromHexError) -> Error {
        match v {
            FromHexError::InvalidHexCharacter { c, index } => Error::InvalidHexCharacter { c, index },
            FromHexError::InvalidStringLength => Error::InvalidStringLength,
            FromHexError::OddLength => Error::OddLength,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidPrefix { c0, c1 } => {
                write!(f, "Invalid hex prefix: expected `0x` but got `{c0}{c1}`")
            }
            Error::InvalidHexCharacter { c, index } => {
                write!(f, "Invalid hex character {:?} at position {}", c, index)
            }
            Error::InvalidStringLength => write!(f, "Invalid hex string length"),
            Error::InvalidStringLengthSlice { expected, actual } => write!(
                f,
                "Invalid hex string length for slice: expected {expected} got {actual}"
            ),
            Error::OddLength => write!(f, "Odd number of digits in hex string"),
        }
    }
}