use std::fmt;
#[derive(Debug)]
pub enum Jp2Error {
Io(std::io::Error),
InvalidMarker(u16),
InvalidData(String),
UnsupportedFeature(String),
BufferTooSmall { need: usize, have: usize },
OutOfBounds { offset: usize, len: usize },
InvalidState(String),
}
impl fmt::Display for Jp2Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {e}"),
Self::InvalidMarker(m) => write!(f, "invalid marker: 0x{m:04X}"),
Self::InvalidData(msg) => write!(f, "invalid data: {msg}"),
Self::UnsupportedFeature(msg) => write!(f, "unsupported feature: {msg}"),
Self::BufferTooSmall { need, have } => {
write!(f, "buffer too small: need {need}, have {have}")
}
Self::OutOfBounds { offset, len } => {
write!(f, "out of bounds: offset {offset}, length {len}")
}
Self::InvalidState(msg) => write!(f, "invalid state: {msg}"),
}
}
}
impl std::error::Error for Jp2Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Jp2Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
pub type Result<T> = std::result::Result<T, Jp2Error>;