Skip to main content

utopia_image/
error.rs

1//! The crate's error type.
2
3/// Everything [`Image`](crate::Image) can fail with, one variant per PHP
4/// exception class. The messages are the library's own and say what the
5/// caller can act on; what a codec reported is kept for `Debug` only.
6///
7/// PHP: `Utopia\Image\Exception\ImageException` and its subclasses for what an
8/// image or the system makes fail, and `\InvalidArgumentException` for an
9/// argument that could never work.
10#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
11pub enum Error {
12    /// Data whose signature is not one of the [`Format`](crate::Format)s, an
13    /// output type that is not one, or a format this build has no codec for.
14    /// PHP: `Utopia\Image\Exception\UnsupportedFormatException`.
15    #[error("{0}")]
16    UnsupportedFormat(String),
17    /// Data of a known format that cannot be decoded; holds what the decoder
18    /// reported. PHP: `Utopia\Image\Exception\CorruptImageException`.
19    #[error("Corrupt or truncated image")]
20    Corrupt(String),
21    /// An image larger than its canvas's [`Limits`](crate::Limits).
22    /// PHP: `Utopia\Image\Exception\LimitExceededException`.
23    #[error("Image exceeds the size or memory limits")]
24    LimitExceeded,
25    /// A focal point, colour or size that is not valid.
26    /// PHP: `\InvalidArgumentException`.
27    #[error("{0}")]
28    InvalidArgument(String),
29    /// A crop whose target height rounds down to zero with a gravity other than
30    /// the centre or with a focal point. PHP: `DivisionByZeroError`.
31    #[error("Division by zero")]
32    DivisionByZero,
33    /// Anything else: an operation or encoder that failed, a file or directory
34    /// that could not be written. PHP: `Utopia\Image\Exception\ImageException`.
35    #[error("{0}")]
36    Failed(String),
37}
38
39impl Error {
40    /// The PHP exception class this error corresponds to.
41    ///
42    /// ```
43    /// use utopia_image::Error;
44    ///
45    /// assert_eq!(Error::LimitExceeded.php_class(), "Utopia\\Image\\Exception\\LimitExceededException");
46    /// ```
47    ///
48    /// ```php
49    /// use Utopia\Image\Exception\ImageException;
50    /// use Utopia\Image\Image;
51    ///
52    /// try { new Image('not an image'); } catch (ImageException $e) { echo get_class($e), "\n"; }
53    /// ```
54    pub fn php_class(&self) -> &'static str {
55        match self {
56            Error::UnsupportedFormat(_) => "Utopia\\Image\\Exception\\UnsupportedFormatException",
57            Error::Corrupt(_) => "Utopia\\Image\\Exception\\CorruptImageException",
58            Error::LimitExceeded => "Utopia\\Image\\Exception\\LimitExceededException",
59            Error::InvalidArgument(_) => "InvalidArgumentException",
60            Error::DivisionByZero => "DivisionByZeroError",
61            Error::Failed(_) => "Utopia\\Image\\Exception\\ImageException",
62        }
63    }
64
65    /// Data that is not an image of a supported format.
66    pub(crate) fn unsupported() -> Self {
67        Error::UnsupportedFormat("Unsupported image format".into())
68    }
69
70    /// A format this build cannot read (`input`) or write (`output`).
71    pub(crate) fn unavailable(format: &str, direction: &str) -> Self {
72        Error::UnsupportedFormat(format!("{format} {direction} is not available"))
73    }
74
75    /// An operation between decoding and encoding that failed.
76    pub(crate) fn process(_: impl std::fmt::Display) -> Self {
77        Error::Failed("Failed to process image".into())
78    }
79
80    /// An encoder or a file write that failed.
81    pub(crate) fn write(_: impl std::fmt::Display) -> Self {
82        Error::Failed("Failed to write image".into())
83    }
84}
85
86/// The result of every fallible operation of this crate.
87pub type Result<T> = std::result::Result<T, Error>;