zune_qoi/
errors.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software; You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
5 */
6
7use alloc::string::String;
8/// Errors possible during decoding.
9use core::fmt::{Debug, Display, Formatter};
10
11use zune_core::colorspace::ColorSpace;
12
13/// Possible Errors that may occur during decoding
14pub enum QoiErrors {
15    /// The image does not start with QOI magic bytes `qoif`
16    ///
17    /// Indicates that image is not a qoi file
18    WrongMagicBytes,
19    /// The input buffer doesn't have enough bytes to fully
20    /// reconstruct the image
21    ///
22    /// # Arguments
23    /// - 1st argument is the number of bytes we expected
24    /// - 2nd argument is number of bytes actually left
25    InsufficientData(usize, usize),
26    /// The header contains an invalid channel number
27    ///
28    /// The only supported types are `3` and `4`
29    UnknownChannels(u8),
30    /// The header contains an invalid colorspace value
31    ///
32    /// The should be `0` or `1`
33    /// but this can be ignored if strict is set to false
34    UnknownColorspace(u8),
35    /// Generic message
36    Generic(String),
37    /// Generic message does not need heap allocation
38    GenericStatic(&'static str),
39    /// To small output size
40    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
89/// Errors encountered during encoding
90pub enum QoiEncodeErrors {
91    /// Unsupported colorspace
92    ///
93    /// The first argument is the colorspace encountered
94    /// The second argument is list of supported colorspaces
95    UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
96
97    /// Too large dimensions
98    /// The dimensions cannot be correctly encoded to a width
99    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 {}