1use alloc::fmt::Display;
2#[cfg(feature = "std")]
3use std::error::Error;
4
5#[derive(Debug)]
7pub enum EncodingError {
8 InvalidAppSegment(u8),
10
11 AppSegmentTooLarge(usize),
13
14 IccTooLarge(usize),
16
17 BadImageData { length: usize, required: usize },
19
20 ZeroImageDimensions { width: u16, height: u16 },
22
23 #[cfg(feature = "std")]
25 IoError(std::io::Error),
26
27 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}