1use core::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum FromHexError {
6 InvalidHexCharacter { c: char, index: usize },
9
10 OddLength,
13
14 InvalidStringLength,
18}
19
20#[cfg(feature = "std")]
21impl std::error::Error for FromHexError {}
22
23impl fmt::Display for FromHexError {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25 match *self {
26 FromHexError::InvalidHexCharacter { c, index } => {
27 write!(f, "Invalid character {:?} at position {}", c, index)
28 }
29 FromHexError::OddLength => write!(f, "Odd number of digits"),
30 FromHexError::InvalidStringLength => write!(f, "Invalid string length"),
31 }
32 }
33}
34
35#[cfg(test)]
36#[cfg(feature = "alloc")]
39mod tests {
40 use super::*;
41 #[cfg(feature = "alloc")]
42 use alloc::string::ToString;
43 use pretty_assertions::assert_eq;
44
45 #[test]
46 #[cfg(feature = "alloc")]
47 fn test_display() {
48 assert_eq!(
49 FromHexError::InvalidHexCharacter { c: '\n', index: 5 }.to_string(),
50 "Invalid character '\\n' at position 5"
51 );
52
53 assert_eq!(FromHexError::OddLength.to_string(), "Odd number of digits");
54 assert_eq!(
55 FromHexError::InvalidStringLength.to_string(),
56 "Invalid string length"
57 );
58 }
59}