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