Skip to main content

agx/
error.rs

1//! Error types for the AgX library. See [`AgxError`].
2
3use thiserror::Error;
4
5/// All errors that can occur in the AgX rendering pipeline.
6///
7/// `AgxError` is the unified error type returned by every fallible AgX function:
8/// decoding raw or encoded images, applying presets, building LUTs, encoding output.
9/// Variants wrap underlying errors with a short context string so callers can match
10/// on the kind without losing the original message.
11#[derive(Debug, Error)]
12pub enum AgxError {
13    /// Failure during image decoding (raw, JPEG, PNG, TIFF, etc.).
14    #[error("Decode error: {0}")]
15    Decode(String),
16    /// Failure during image encoding to an output file or buffer.
17    #[error("Encode error: {0}")]
18    Encode(String),
19    /// Failure parsing or applying a preset.
20    #[error("Preset error: {0}")]
21    Preset(String),
22    /// Failure parsing or applying a LUT (e.g. invalid `.cube` file).
23    #[error("LUT error: {0}")]
24    Lut(String),
25    /// Underlying error from the `image` crate.
26    #[error("Image error: {0}")]
27    Image(#[from] image::ImageError),
28    /// Underlying I/O error from the standard library.
29    #[error("IO error: {0}")]
30    Io(#[from] std::io::Error),
31    /// Failure initializing the GPU runtime (no adapter, device creation failed, etc.).
32    #[error("GPU error: {0}")]
33    Gpu(String),
34}
35
36/// Convenience alias for `Result<T, AgxError>` used throughout the AgX API.
37pub type Result<T> = std::result::Result<T, AgxError>;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn error_display_decode() {
45        let err = AgxError::Decode("bad file".into());
46        assert_eq!(err.to_string(), "Decode error: bad file");
47    }
48
49    #[test]
50    fn error_display_encode() {
51        let err = AgxError::Encode("write failed".into());
52        assert_eq!(err.to_string(), "Encode error: write failed");
53    }
54
55    #[test]
56    fn error_display_preset() {
57        let err = AgxError::Preset("parse failed".into());
58        assert_eq!(err.to_string(), "Preset error: parse failed");
59    }
60
61    #[test]
62    fn error_display_lut() {
63        let err = AgxError::Lut("invalid size".into());
64        assert_eq!(err.to_string(), "LUT error: invalid size");
65    }
66
67    #[test]
68    fn error_display_gpu() {
69        let err = AgxError::Gpu("no adapter found".into());
70        assert_eq!(err.to_string(), "GPU error: no adapter found");
71    }
72
73    #[test]
74    fn error_from_io() {
75        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
76        let err: AgxError = io_err.into();
77        assert!(matches!(err, AgxError::Io(_)));
78    }
79}