use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeError {
InvalidCharacter {
character: char,
position: usize,
},
UnexpectedEndOfStream,
UndefinedSignal {
value: u64,
},
InvalidFinalBlock {
length: usize,
},
}
impl DecodeError {
pub fn code(&self) -> &'static str {
match self {
DecodeError::InvalidCharacter { .. } => "invalid_character",
DecodeError::UnexpectedEndOfStream => "unexpected_end_of_stream",
DecodeError::UndefinedSignal { .. } => "undefined_signal",
DecodeError::InvalidFinalBlock { .. } => "invalid_final_block",
}
}
pub fn position(&self) -> Option<usize> {
match self {
DecodeError::InvalidCharacter { position, .. } => Some(*position),
_ => None,
}
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DecodeError::InvalidCharacter { character, position } => write!(
f,
"invalid character {:?} at byte offset {} is not a member of Alphabet-N",
character, position
),
DecodeError::UnexpectedEndOfStream => {
write!(f, "unexpected end of stream: more Base85N characters were expected")
}
DecodeError::UndefinedSignal { value } => write!(
f,
"undefined signal: group value {} is in FUTURE_SIGNAL_SPACE",
value
),
DecodeError::InvalidFinalBlock { length } => {
write!(f, "invalid final block of {} character(s)", length)
}
}
}
}
impl std::error::Error for DecodeError {}