use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
#[allow(clippy::module_name_repetitions)]
pub enum FromHexError {
#[allow(missing_docs)]
InvalidHexCharacter { c: char, index: usize },
OddLength,
InvalidStringLength,
}
#[cfg(feature = "std")]
impl std::error::Error for FromHexError {}
impl fmt::Display for FromHexError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
FromHexError::InvalidHexCharacter { c, index } => {
write!(f, "Invalid character {c:?} at position {index}")
}
FromHexError::OddLength => f.write_str("Odd number of digits"),
FromHexError::InvalidStringLength => f.write_str("Invalid string length"),
}
}
}
#[cfg(all(test, feature = "alloc"))]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn test_display() {
assert_eq!(
FromHexError::InvalidHexCharacter { c: '\n', index: 5 }.to_string(),
"Invalid character '\\n' at position 5"
);
assert_eq!(FromHexError::OddLength.to_string(), "Odd number of digits");
assert_eq!(
FromHexError::InvalidStringLength.to_string(),
"Invalid string length"
);
}
}