1use alloc::string::String;
8use core::fmt::{Debug, Display, Formatter};
10
11use zune_core::bytestream::ZByteIoError;
12use zune_core::colorspace::ColorSpace;
13
14pub enum QoiErrors {
16 WrongMagicBytes,
20 InsufficientData(usize, usize),
27 UnknownChannels(u8),
31 UnknownColorspace(u8),
36 Generic(String),
38 GenericStatic(&'static str),
40 TooSmallOutput(usize, usize),
42 IoErrors(ZByteIoError)
43}
44
45impl Debug for QoiErrors {
46 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
47 match self {
48 QoiErrors::WrongMagicBytes => {
49 writeln!(f, "Wrong magic bytes, expected `qoif` as image start")
50 }
51 QoiErrors::InsufficientData(expected, found) => {
52 writeln!(
53 f,
54 "Insufficient data required {expected} but remaining stream has {found}"
55 )
56 }
57 QoiErrors::UnknownChannels(channel) => {
58 writeln!(
59 f,
60 "Unknown channel number {channel}, expected either 3 or 4"
61 )
62 }
63 QoiErrors::UnknownColorspace(colorspace) => {
64 writeln!(
65 f,
66 "Unknown colorspace number {colorspace}, expected either 0 or 1"
67 )
68 }
69 QoiErrors::Generic(val) => {
70 writeln!(f, "{val}")
71 }
72 QoiErrors::GenericStatic(val) => {
73 writeln!(f, "{val}")
74 }
75 QoiErrors::TooSmallOutput(expected, found) => {
76 writeln!(
77 f,
78 "Too small output size, expected {expected}, but found {found}"
79 )
80 }
81 QoiErrors::IoErrors(value) => {
82 writeln!(f, "I/O error {:?}", value)
83 }
84 }
85 }
86}
87
88impl From<&'static str> for QoiErrors {
89 fn from(r: &'static str) -> Self {
90 Self::GenericStatic(r)
91 }
92}
93
94impl From<ZByteIoError> for QoiErrors {
95 fn from(value: ZByteIoError) -> Self {
96 QoiErrors::IoErrors(value)
97 }
98}
99pub enum QoiEncodeErrors {
101 UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
106
107 TooLargeDimensions(usize),
110
111 Generic(&'static str),
112
113 IoError(ZByteIoError)
114}
115
116impl Debug for QoiEncodeErrors {
117 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
118 match self {
119 QoiEncodeErrors::UnsupportedColorspace(found, supported) => {
120 writeln!(f, "Cannot encode image with colorspace {found:?} into QOI, supported ones are {supported:?}")
121 }
122 QoiEncodeErrors::TooLargeDimensions(found) => {
123 writeln!(
124 f,
125 "Too large image dimensions {found}, QOI can only encode images less than {}",
126 u32::MAX
127 )
128 }
129 QoiEncodeErrors::Generic(val) => {
130 writeln!(f, "{}", val)
131 }
132 QoiEncodeErrors::IoError(v) => {
133 writeln!(f, "I/O error {:?}", v)
134 }
135 }
136 }
137}
138
139impl Display for QoiEncodeErrors {
140 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
141 writeln!(f, "{:?}", self)
142 }
143}
144impl Display for QoiErrors {
145 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
146 writeln!(f, "{:?}", self)
147 }
148}
149
150#[cfg(feature = "std")]
151impl std::error::Error for QoiEncodeErrors {}
152
153#[cfg(feature = "std")]
154impl std::error::Error for QoiErrors {}
155
156impl From<ZByteIoError> for QoiEncodeErrors {
157 fn from(value: ZByteIoError) -> Self {
158 Self::IoError(value)
159 }
160}