Error types cleanup + clippy fixes
This commit is contained in:
parent
82f07a3e8d
commit
cb10dd3629
5 changed files with 32 additions and 45 deletions
|
@ -73,7 +73,7 @@ where
|
||||||
_ => {
|
_ => {
|
||||||
cold();
|
cold();
|
||||||
if unlikely(data.len() < QOI_PADDING_SIZE) {
|
if unlikely(data.len() < QOI_PADDING_SIZE) {
|
||||||
return Err(Error::UnexpectedBufferEnd); // TODO: remove InputDataSize err
|
return Err(Error::UnexpectedBufferEnd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ use crate::pixel::{Pixel, SupportedChannels};
|
||||||
use crate::types::{Channels, ColorSpace};
|
use crate::types::{Channels, ColorSpace};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use crate::utils::GenericWriter;
|
use crate::utils::GenericWriter;
|
||||||
use crate::utils::{cold, unlikely, BytesMut, Writer};
|
use crate::utils::{unlikely, BytesMut, Writer};
|
||||||
|
|
||||||
#[allow(clippy::cast_possible_truncation, unused_assignments)]
|
#[allow(clippy::cast_possible_truncation, unused_assignments)]
|
||||||
fn qoi_encode_impl<W: Writer, const N: usize>(mut buf: W, data: &[u8]) -> Result<usize>
|
fn qoi_encode_impl<W: Writer, const N: usize>(mut buf: W, data: &[u8]) -> Result<usize>
|
||||||
|
|
63
src/error.rs
63
src/error.rs
|
@ -1,18 +1,21 @@
|
||||||
use core::convert::Infallible;
|
use core::convert::Infallible;
|
||||||
use core::fmt::{self, Display};
|
use core::fmt::{self, Display};
|
||||||
|
|
||||||
use crate::consts::{QOI_MAGIC, QOI_PIXELS_MAX};
|
use crate::consts::QOI_MAGIC;
|
||||||
|
|
||||||
|
/// Errors that can occur during encoding or decoding.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
InvalidMagic {
|
||||||
|
magic: u32,
|
||||||
|
},
|
||||||
InvalidChannels {
|
InvalidChannels {
|
||||||
channels: u8,
|
channels: u8,
|
||||||
},
|
},
|
||||||
EmptyImage {
|
InvalidColorSpace {
|
||||||
width: u32,
|
colorspace: u8,
|
||||||
height: u32,
|
|
||||||
},
|
},
|
||||||
ImageTooLarge {
|
InvalidImageDimensions {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
},
|
},
|
||||||
|
@ -21,61 +24,45 @@ pub enum Error {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
},
|
},
|
||||||
InputBufferTooSmall {
|
|
||||||
size: usize,
|
|
||||||
required: usize,
|
|
||||||
},
|
|
||||||
OutputBufferTooSmall {
|
OutputBufferTooSmall {
|
||||||
size: usize,
|
size: usize,
|
||||||
required: usize,
|
required: usize,
|
||||||
},
|
},
|
||||||
InvalidMagic {
|
|
||||||
magic: u32,
|
|
||||||
},
|
|
||||||
UnexpectedBufferEnd,
|
UnexpectedBufferEnd,
|
||||||
InvalidColorSpace {
|
|
||||||
colorspace: u8,
|
|
||||||
},
|
|
||||||
InvalidPadding,
|
InvalidPadding,
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Alias for `Result` with the error type `qoi_fast::Error`.
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
|
Self::InvalidMagic { magic } => {
|
||||||
|
write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic.to_be_bytes())
|
||||||
|
}
|
||||||
Self::InvalidChannels { channels } => {
|
Self::InvalidChannels { channels } => {
|
||||||
write!(f, "invalid number of channels: {}", channels)
|
write!(f, "invalid number of channels: {}", channels)
|
||||||
}
|
}
|
||||||
Self::EmptyImage { width, height } => {
|
|
||||||
write!(f, "image contains no pixels: {}x{}", width, height)
|
|
||||||
}
|
|
||||||
Self::ImageTooLarge { width, height } => {
|
|
||||||
let mp = QOI_PIXELS_MAX / 1_000_000;
|
|
||||||
write!(f, "image is too large: {}x{} (max={}Mp)", width, height, mp)
|
|
||||||
}
|
|
||||||
Self::InvalidImageLength { size, width, height } => {
|
|
||||||
write!(f, "invalid image length: {} for {}x{}", size, width, height)
|
|
||||||
}
|
|
||||||
Self::InputBufferTooSmall { size, required } => {
|
|
||||||
write!(f, "input buffer size too small: {} (minimum required: {})", size, required)
|
|
||||||
}
|
|
||||||
Self::OutputBufferTooSmall { size, required } => {
|
|
||||||
write!(f, "output buffer size too small: {} (minimum required: {})", size, required)
|
|
||||||
}
|
|
||||||
Self::InvalidMagic { magic } => {
|
|
||||||
write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic)
|
|
||||||
}
|
|
||||||
Self::UnexpectedBufferEnd => {
|
|
||||||
write!(f, "unexpected input buffer end while decoding")
|
|
||||||
}
|
|
||||||
Self::InvalidColorSpace { colorspace } => {
|
Self::InvalidColorSpace { colorspace } => {
|
||||||
write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
|
write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
|
||||||
}
|
}
|
||||||
|
Self::InvalidImageDimensions { width, height } => {
|
||||||
|
write!(f, "invalid image dimensions: {}x{}", width, height)
|
||||||
|
}
|
||||||
|
Self::InvalidImageLength { size, width, height } => {
|
||||||
|
write!(f, "invalid image length: {} bytes for {}x{}", size, width, height)
|
||||||
|
}
|
||||||
|
Self::OutputBufferTooSmall { size, required } => {
|
||||||
|
write!(f, "output buffer size too small: {} (required: {})", size, required)
|
||||||
|
}
|
||||||
|
Self::UnexpectedBufferEnd => {
|
||||||
|
write!(f, "unexpected input buffer end while decoding")
|
||||||
|
}
|
||||||
Self::InvalidPadding => {
|
Self::InvalidPadding => {
|
||||||
write!(f, "invalid padding (stream end marker)")
|
write!(f, "invalid padding (stream end marker mismatch)")
|
||||||
}
|
}
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
Self::IoError(ref err) => {
|
Self::IoError(ref err) => {
|
||||||
|
|
|
@ -44,10 +44,9 @@ impl Header {
|
||||||
pub const fn try_new(
|
pub const fn try_new(
|
||||||
width: u32, height: u32, channels: Channels, colorspace: ColorSpace,
|
width: u32, height: u32, channels: Channels, colorspace: ColorSpace,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
if unlikely(height == 0 || width == 0) {
|
let n_pixels = (width as usize).saturating_mul(height as usize);
|
||||||
return Err(Error::EmptyImage { width, height });
|
if unlikely(n_pixels == 0 || n_pixels > QOI_PIXELS_MAX) {
|
||||||
} else if unlikely((width as usize).saturating_mul(height as usize) > QOI_PIXELS_MAX) {
|
return Err(Error::InvalidImageDimensions { width, height });
|
||||||
return Err(Error::ImageTooLarge { width, height });
|
|
||||||
}
|
}
|
||||||
Ok(Self { width, height, channels, colorspace })
|
Ok(Self { width, height, channels, colorspace })
|
||||||
}
|
}
|
||||||
|
@ -83,7 +82,7 @@ impl Header {
|
||||||
pub(crate) fn decode(data: impl AsRef<[u8]>) -> Result<Self> {
|
pub(crate) fn decode(data: impl AsRef<[u8]>) -> Result<Self> {
|
||||||
let data = data.as_ref();
|
let data = data.as_ref();
|
||||||
if unlikely(data.len() < QOI_HEADER_SIZE) {
|
if unlikely(data.len() < QOI_HEADER_SIZE) {
|
||||||
return Err(Error::InputBufferTooSmall { size: data.len(), required: QOI_HEADER_SIZE });
|
return Err(Error::UnexpectedBufferEnd);
|
||||||
}
|
}
|
||||||
let v = cast_slice::<_, [u8; 4]>(&data[..12]);
|
let v = cast_slice::<_, [u8; 4]>(&data[..12]);
|
||||||
let magic = u32::from_be_bytes(v[0]);
|
let magic = u32::from_be_bytes(v[0]);
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub enum ColorSpace {
|
||||||
Linear = 1,
|
Linear = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::doc_markdown)]
|
||||||
impl ColorSpace {
|
impl ColorSpace {
|
||||||
/// Returns true if the color space is sRGB with linear alpha.
|
/// Returns true if the color space is sRGB with linear alpha.
|
||||||
pub const fn is_srgb(self) -> bool {
|
pub const fn is_srgb(self) -> bool {
|
||||||
|
|
Loading…
Reference in a new issue