Skip to main content

figif_core/
error.rs

1//! Error types for figif-core.
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Result type alias using [`FigifError`].
7pub type Result<T> = std::result::Result<T, FigifError>;
8
9/// Errors that can occur during GIF processing.
10#[derive(Debug, Error)]
11pub enum FigifError {
12    /// Failed to read or open a file.
13    #[error("failed to read file: {path}")]
14    FileRead {
15        /// Path to the file that could not be read.
16        path: PathBuf,
17        /// The underlying IO error.
18        #[source]
19        source: std::io::Error,
20    },
21
22    /// Failed to write a file.
23    #[error("failed to write file: {path}")]
24    FileWrite {
25        /// Path to the file that could not be written.
26        path: PathBuf,
27        /// The underlying IO error.
28        #[source]
29        source: std::io::Error,
30    },
31
32    /// GIF decoding failed.
33    #[error("failed to decode GIF: {reason}")]
34    DecodeError {
35        /// Description of the decoding failure.
36        reason: String,
37    },
38
39    /// GIF encoding failed.
40    #[error("failed to encode GIF: {reason}")]
41    EncodeError {
42        /// Description of the encoding failure.
43        reason: String,
44    },
45
46    /// Invalid frame index.
47    #[error("invalid frame index: {index} (total frames: {total})")]
48    InvalidFrameIndex {
49        /// The requested frame index.
50        index: usize,
51        /// Total number of frames available.
52        total: usize,
53    },
54
55    /// Invalid segment ID.
56    #[error("invalid segment ID: {id} (total segments: {total})")]
57    InvalidSegmentId {
58        /// The requested segment ID.
59        id: usize,
60        /// Total number of segments available.
61        total: usize,
62    },
63
64    /// Invalid configuration parameter.
65    #[error("invalid configuration: {message}")]
66    InvalidConfig {
67        /// Description of the configuration error.
68        message: String,
69    },
70
71    /// Frame dimensions mismatch.
72    #[error(
73        "frame dimension mismatch: expected {expected_width}x{expected_height}, got {actual_width}x{actual_height}"
74    )]
75    DimensionMismatch {
76        /// Expected frame width.
77        expected_width: u32,
78        /// Expected frame height.
79        expected_height: u32,
80        /// Actual frame width encountered.
81        actual_width: u32,
82        /// Actual frame height encountered.
83        actual_height: u32,
84    },
85
86    /// No frames in the GIF.
87    #[error("GIF contains no frames")]
88    NoFrames,
89
90    /// Image processing error.
91    #[error("image processing error: {reason}")]
92    ImageError {
93        /// Description of the image processing failure.
94        reason: String,
95    },
96
97    /// Hashing error.
98    #[error("hashing error: {reason}")]
99    HashError {
100        /// Description of the hashing failure.
101        reason: String,
102    },
103
104    /// The GIF data is empty or invalid.
105    #[error("empty or invalid GIF data")]
106    EmptyData,
107}
108
109impl From<gif::DecodingError> for FigifError {
110    fn from(err: gif::DecodingError) -> Self {
111        FigifError::DecodeError {
112            reason: err.to_string(),
113        }
114    }
115}
116
117impl From<image::ImageError> for FigifError {
118    fn from(err: image::ImageError) -> Self {
119        FigifError::ImageError {
120            reason: err.to_string(),
121        }
122    }
123}