#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("{0}")]
UnsupportedFormat(String),
#[error("Corrupt or truncated image")]
Corrupt(String),
#[error("Image exceeds the size or memory limits")]
LimitExceeded,
#[error("{0}")]
InvalidArgument(String),
#[error("Division by zero")]
DivisionByZero,
#[error("{0}")]
Failed(String),
}
impl Error {
pub fn php_class(&self) -> &'static str {
match self {
Error::UnsupportedFormat(_) => "Utopia\\Image\\Exception\\UnsupportedFormatException",
Error::Corrupt(_) => "Utopia\\Image\\Exception\\CorruptImageException",
Error::LimitExceeded => "Utopia\\Image\\Exception\\LimitExceededException",
Error::InvalidArgument(_) => "InvalidArgumentException",
Error::DivisionByZero => "DivisionByZeroError",
Error::Failed(_) => "Utopia\\Image\\Exception\\ImageException",
}
}
pub(crate) fn unsupported() -> Self {
Error::UnsupportedFormat("Unsupported image format".into())
}
pub(crate) fn unavailable(format: &str, direction: &str) -> Self {
Error::UnsupportedFormat(format!("{format} {direction} is not available"))
}
pub(crate) fn process(_: impl std::fmt::Display) -> Self {
Error::Failed("Failed to process image".into())
}
pub(crate) fn write(_: impl std::fmt::Display) -> Self {
Error::Failed("Failed to write image".into())
}
}
pub type Result<T> = std::result::Result<T, Error>;