use std::fmt;
#[derive(Debug)]
pub enum DecodeError {
UnexpectedEof,
InvalidSyntax(&'static str),
Unsupported(&'static str),
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DecodeError::UnexpectedEof => write!(f, "unexpected end of bitstream"),
DecodeError::InvalidSyntax(msg) => write!(f, "invalid syntax: {}", msg),
DecodeError::Unsupported(msg) => write!(f, "unsupported: {}", msg),
}
}
}
impl std::error::Error for DecodeError {}
impl From<&'static str> for DecodeError {
fn from(msg: &'static str) -> Self {
if msg == "end of bitstream" {
DecodeError::UnexpectedEof
} else if msg.contains("not yet supported")
|| msg.contains("not supported")
|| msg.starts_with("only ")
|| msg.starts_with("unsupported")
{
DecodeError::Unsupported(msg)
} else {
DecodeError::InvalidSyntax(msg)
}
}
}