use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::module_name_repetitions)]
pub enum FromHexError {
#[allow(missing_docs)]
InvalidHexCharacter { c: char, index: usize },
OddLength,
InvalidStringLength,
}
#[cfg(feature = "core-error")]
impl core::error::Error for FromHexError {}
#[cfg(all(feature = "std", not(feature = "core-error")))]
impl std::error::Error for FromHexError {}
impl fmt::Display for FromHexError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::InvalidHexCharacter { c, index } => {
write!(f, "invalid character {c:?} at position {index}")
}
Self::OddLength => f.write_str("odd number of digits"),
Self::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"
);
}
}