use alloc::string::String;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TiffError {
#[error("TIFF decode error: {0}")]
Decode(String),
#[error("TIFF encode error: {0}")]
Encode(String),
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("unsupported: {0}")]
Unsupported(String),
#[error("limit exceeded: {0}")]
LimitExceeded(String),
#[error("stopped: {0}")]
Stopped(enough::StopReason),
#[error("I/O error: {0}")]
Io(std::io::Error),
#[error("buffer error: {0}")]
Buffer(zenpixels::BufferError),
}
impl From<enough::StopReason> for TiffError {
fn from(reason: enough::StopReason) -> Self {
TiffError::Stopped(reason)
}
}
impl From<tiff::TiffError> for TiffError {
fn from(e: tiff::TiffError) -> Self {
match e {
tiff::TiffError::FormatError(ref fe) => {
TiffError::Decode(alloc::format!("format error: {fe}"))
}
tiff::TiffError::UnsupportedError(ref ue) => {
TiffError::Unsupported(alloc::format!("{ue}"))
}
tiff::TiffError::IoError(io) => TiffError::Io(io),
tiff::TiffError::LimitsExceeded => {
TiffError::LimitExceeded("tiff decoder limits exceeded".into())
}
tiff::TiffError::IntSizeError => {
TiffError::LimitExceeded("image dimensions exceed platform limits".into())
}
tiff::TiffError::UsageError(ref ue) => {
TiffError::InvalidInput(alloc::format!("usage error: {ue}"))
}
}
}
}
impl From<zenpixels::BufferError> for TiffError {
fn from(e: zenpixels::BufferError) -> Self {
TiffError::Buffer(e)
}
}
impl From<whereat::At<zenpixels::BufferError>> for TiffError {
fn from(e: whereat::At<zenpixels::BufferError>) -> Self {
TiffError::Buffer(e.decompose().0)
}
}
#[cfg(feature = "zencodec")]
impl From<zencodec::UnsupportedOperation> for TiffError {
fn from(op: zencodec::UnsupportedOperation) -> Self {
TiffError::Unsupported(alloc::format!("{op}"))
}
}
pub type Result<T> = core::result::Result<T, whereat::At<TiffError>>;