jpeg_encoder/
error.rs

1use alloc::fmt::Display;
2#[cfg(feature = "std")]
3use std::error::Error;
4
5/// # The error type for encoding
6#[derive(Debug)]
7pub enum EncodingError {
8    /// An invalid app segment number has been used
9    InvalidAppSegment(u8),
10
11    /// App segment exceeds maximum allowed data length
12    AppSegmentTooLarge(usize),
13
14    /// Color profile exceeds maximum allowed data length
15    IccTooLarge(usize),
16
17    /// Image data is too short
18    BadImageData { length: usize, required: usize },
19
20    /// Width or height is zero
21    ZeroImageDimensions { width: u16, height: u16 },
22
23    /// An io error occurred during writing
24    #[cfg(feature = "std")]
25    IoError(std::io::Error),
26
27    /// An io error occurred during writing (Should be used in no_std cases instead of IoError)
28    Write(alloc::string::String),
29}
30
31#[cfg(feature = "std")]
32impl From<std::io::Error> for EncodingError {
33    fn from(err: std::io::Error) -> EncodingError {
34        EncodingError::IoError(err)
35    }
36}
37
38impl Display for EncodingError {
39    fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
40        use EncodingError::*;
41        match self {
42            InvalidAppSegment(nr) => write!(f, "Invalid app segment number: {}", nr),
43            AppSegmentTooLarge(length) => write!(
44                f,
45                "App segment exceeds maximum allowed data length of 65533: {}",
46                length
47            ),
48            IccTooLarge(length) => write!(
49                f,
50                "ICC profile exceeds maximum allowed data length: {}",
51                length
52            ),
53            BadImageData { length, required } => write!(
54                f,
55                "Image data too small for dimensions and color_type: {} need at least {}",
56                length, required
57            ),
58            ZeroImageDimensions { width, height } => {
59                write!(f, "Image dimensions must be non zero: {}x{}", width, height)
60            }
61            #[cfg(feature = "std")]
62            IoError(err) => err.fmt(f),
63            Write(err) => write!(f, "{}", err),
64        }
65    }
66}
67
68#[cfg(feature = "std")]
69impl Error for EncodingError {
70    fn source(&self) -> Option<&(dyn Error + 'static)> {
71        match self {
72            EncodingError::IoError(err) => Some(err),
73            _ => None,
74        }
75    }
76}