use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecodeError {
UnexpectedEof {
needed: usize,
remaining: usize,
},
InvalidBool(u8),
InvalidUtf8,
LengthOverflow {
length: usize,
limit: usize,
},
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
DecodeError::UnexpectedEof { needed, remaining } => {
write!(f, "unexpected eof: needed {needed} bytes, {remaining} remaining")
}
DecodeError::InvalidBool(byte) => {
write!(f, "invalid bool: 0x{byte:02X} is neither 0x00 nor 0x01")
}
DecodeError::InvalidUtf8 => f.write_str("invalid utf-8 in string"),
DecodeError::LengthOverflow { length, limit } => {
write!(f, "length overflow: length {length} exceeds limit {limit}")
}
}
}
}
impl std::error::Error for DecodeError {}