use std::fmt;
#[non_exhaustive]
#[derive(Debug)]
pub enum JpxError {
NotJpeg2000,
Malformed(String),
LimitExceeded {
what: &'static str,
actual: u64,
limit: u64,
},
Unsupported(&'static str),
}
impl fmt::Display for JpxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JpxError::NotJpeg2000 => {
write!(f, "not a JPEG 2000 file or codestream")
}
JpxError::Malformed(detail) => {
write!(f, "malformed JPEG 2000 data: {detail}")
}
JpxError::LimitExceeded {
what,
actual,
limit,
} => {
write!(f, "decode limit exceeded: {what} = {actual} > {limit}")
}
JpxError::Unsupported(what) => {
write!(f, "unsupported JPEG 2000 feature: {what}")
}
}
}
}
impl std::error::Error for JpxError {}
pub type Result<T> = std::result::Result<T, JpxError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_names_the_tripped_limit() {
let err = JpxError::LimitExceeded {
what: "max_pixels",
actual: 200,
limit: 100,
};
assert_eq!(
err.to_string(),
"decode limit exceeded: max_pixels = 200 > 100"
);
}
}