1use alloc::string::String;
8use core::fmt::{Debug, Display, Formatter};
10
11use zune_core::colorspace::ColorSpace;
12
13pub enum QoiErrors {
15 WrongMagicBytes,
19 InsufficientData(usize, usize),
26 UnknownChannels(u8),
30 UnknownColorspace(u8),
35 Generic(String),
37 GenericStatic(&'static str),
39 TooSmallOutput(usize, usize)
41}
42
43impl Debug for QoiErrors {
44 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
45 match self {
46 QoiErrors::WrongMagicBytes => {
47 writeln!(f, "Wrong magic bytes, expected `qoif` as image start")
48 }
49 QoiErrors::InsufficientData(expected, found) => {
50 writeln!(
51 f,
52 "Insufficient data required {expected} but remaining stream has {found}"
53 )
54 }
55 QoiErrors::UnknownChannels(channel) => {
56 writeln!(
57 f,
58 "Unknown channel number {channel}, expected either 3 or 4"
59 )
60 }
61 QoiErrors::UnknownColorspace(colorspace) => {
62 writeln!(
63 f,
64 "Unknown colorspace number {colorspace}, expected either 0 or 1"
65 )
66 }
67 QoiErrors::Generic(val) => {
68 writeln!(f, "{val}")
69 }
70 QoiErrors::GenericStatic(val) => {
71 writeln!(f, "{val}")
72 }
73 QoiErrors::TooSmallOutput(expected, found) => {
74 writeln!(
75 f,
76 "Too small output size, expected {expected}, but found {found}"
77 )
78 }
79 }
80 }
81}
82
83impl From<&'static str> for QoiErrors {
84 fn from(r: &'static str) -> Self {
85 Self::GenericStatic(r)
86 }
87}
88
89pub enum QoiEncodeErrors {
91 UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
96
97 TooLargeDimensions(usize),
100
101 Generic(&'static str)
102}
103
104impl Debug for QoiEncodeErrors {
105 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
106 match self {
107 QoiEncodeErrors::UnsupportedColorspace(found, supported) => {
108 writeln!(f, "Cannot encode image with colorspace {found:?} into QOI, supported ones are {supported:?}")
109 }
110 QoiEncodeErrors::TooLargeDimensions(found) => {
111 writeln!(
112 f,
113 "Too large image dimensions {found}, QOI can only encode images less than {}",
114 u32::MAX
115 )
116 }
117 QoiEncodeErrors::Generic(val) => {
118 writeln!(f, "{}", val)
119 }
120 }
121 }
122}
123
124impl Display for QoiEncodeErrors {
125 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
126 writeln!(f, "{:?}", self)
127 }
128}
129impl Display for QoiErrors {
130 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
131 writeln!(f, "{:?}", self)
132 }
133}
134
135#[cfg(feature = "std")]
136impl std::error::Error for QoiEncodeErrors {}
137
138#[cfg(feature = "std")]
139impl std::error::Error for QoiErrors {}