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