#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(std::io::Error),
InvalidBox,
NonZeroPadding,
InvalidFloat,
InvalidEnum {
name: &'static str,
value: u32,
},
ValidationFailed(&'static str),
ProfileConformance(&'static str),
CannotSkip,
NotAligned,
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(e) => {
write!(f, "I/O error: {}", e)
}
Self::InvalidBox => write!(f, "invalid container"),
Self::NonZeroPadding => {
write!(f, "PadZeroToByte() read non-zero bits")
}
Self::InvalidFloat => {
write!(f, "F16() read NaN or Infinity")
}
Self::InvalidEnum { name, value } => {
write!(f, "Enum({}) read invalid enum value of {}", name, value)
}
Self::ValidationFailed(msg) => {
write!(f, "bitstream validation failed: {msg}")
}
Self::ProfileConformance(msg) => {
write!(f, "not supported by current profile: {msg}")
}
Self::CannotSkip => {
write!(f, "target bookmark already passed")
}
Self::NotAligned => {
write!(f, "bitstream is unaligned")
}
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl Error {
pub fn unexpected_eof(&self) -> bool {
if let Error::Io(e) = self {
return e.kind() == std::io::ErrorKind::UnexpectedEof;
}
false
}
}
pub type Result<T> = std::result::Result<T, Error>;