use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
BufferLength {
expected: usize,
actual: usize,
},
EmptyPhoto,
SizeMismatch {
first: (usize, usize),
second: (usize, usize),
},
PhotoTooSmall {
dimensions: (usize, usize),
minimum: usize,
},
Decode(DecodeError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BufferLength { expected, actual } => write!(
f,
"pixel buffer is {actual} bytes, but the given dimensions need {expected}"
),
Error::EmptyPhoto => f.write_str("photo has zero width or height"),
Error::SizeMismatch { first, second } => write!(
f,
"photos must have the same dimensions, got {}x{} and {}x{}",
first.0, first.1, second.0, second.1
),
Error::PhotoTooSmall {
dimensions,
minimum,
} => write!(
f,
"photo is {}x{}, but correspondence mapping needs at least {minimum}x{minimum}",
dimensions.0, dimensions.1
),
Error::Decode(inner) => write!(f, "could not decode mapping data: {inner}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Decode(inner) => Some(inner),
_ => None,
}
}
}
impl From<DecodeError> for Error {
fn from(inner: DecodeError) -> Self {
Error::Decode(inner)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecodeError {
NotAMapping,
UnsupportedVersion {
found: u16,
supported: u16,
},
Truncated {
expected: usize,
actual: usize,
},
InvalidHeader {
reason: &'static str,
},
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DecodeError::NotAMapping => f.write_str("missing magic number"),
DecodeError::UnsupportedVersion { found, supported } => {
write!(
f,
"format version {found}, but this build reads version {supported}"
)
}
DecodeError::Truncated { expected, actual } => {
write!(f, "expected {expected} bytes, found {actual}")
}
DecodeError::InvalidHeader { reason } => write!(f, "invalid header: {reason}"),
}
}
}
impl std::error::Error for DecodeError {}