use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FromHexError {
InvalidHexCharacter { c: char, index: usize },
OddLength,
InvalidStringLength,
}
#[cfg(feature = "std")]
impl std::error::Error for FromHexError {}
impl fmt::Display for FromHexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FromHexError::InvalidHexCharacter { c, index } => {
write!(f, "Invalid character {:?} at position {}", c, index)
}
FromHexError::OddLength => write!(f, "Odd number of digits"),
FromHexError::InvalidStringLength => write!(f, "Invalid string length"),
}
}
}
#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
#[cfg(feature = "alloc")]
use alloc::string::ToString;
use pretty_assertions::assert_eq;
#[test]
#[cfg(feature = "alloc")]
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"
);
}
}