/rust/registry/src/index.crates.io-1949cf8c6b5b557f/jpeg-encoder-0.6.1/src/error.rs
Line | Count | Source |
1 | | use alloc::fmt::Display; |
2 | | #[cfg(feature = "std")] |
3 | | use std::error::Error; |
4 | | |
5 | | /// # The error type for encoding |
6 | | #[derive(Debug)] |
7 | | pub 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")] |
32 | | impl From<std::io::Error> for EncodingError { |
33 | 0 | fn from(err: std::io::Error) -> EncodingError { |
34 | 0 | EncodingError::IoError(err) |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl Display for EncodingError { |
39 | 0 | fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result { |
40 | | use EncodingError::*; |
41 | 0 | match self { |
42 | 0 | InvalidAppSegment(nr) => write!(f, "Invalid app segment number: {}", nr), |
43 | 0 | AppSegmentTooLarge(length) => write!( |
44 | 0 | f, |
45 | 0 | "App segment exceeds maximum allowed data length of 65533: {}", |
46 | | length |
47 | | ), |
48 | 0 | IccTooLarge(length) => write!( |
49 | 0 | f, |
50 | 0 | "ICC profile exceeds maximum allowed data length: {}", |
51 | | length |
52 | | ), |
53 | 0 | BadImageData { length, required } => write!( |
54 | 0 | f, |
55 | 0 | "Image data too small for dimensions and color_type: {} need at least {}", |
56 | | length, required |
57 | | ), |
58 | 0 | ZeroImageDimensions { width, height } => { |
59 | 0 | write!(f, "Image dimensions must be non zero: {}x{}", width, height) |
60 | | } |
61 | | #[cfg(feature = "std")] |
62 | 0 | IoError(err) => err.fmt(f), |
63 | 0 | Write(err) => write!(f, "{}", err), |
64 | | } |
65 | 0 | } |
66 | | } |
67 | | |
68 | | #[cfg(feature = "std")] |
69 | | impl Error for EncodingError { |
70 | 0 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
71 | 0 | match self { |
72 | 0 | EncodingError::IoError(err) => Some(err), |
73 | 0 | _ => None, |
74 | | } |
75 | 0 | } |
76 | | } |