libsixel_rs/
error.rs

1//! Types and functions for error handling.
2
3use alloc::string::String;
4
5use crate::std::{self, fmt};
6
7/// Result type for the library.
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// Error type for the library.
11#[repr(C)]
12#[derive(Clone, Debug, PartialEq)]
13pub enum Error {
14    Generic(i32),
15    Dither(String),
16    Frame(String),
17    PixelFormat(String),
18    Quant(String),
19    Output(String),
20    Io(String),
21    Rgb,
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Generic(err) => write!(f, "Generic error: {err}"),
28            Self::Rgb => write!(f, "RGB parsing/encoding error"),
29            Self::Dither(err) => write!(f, "Dither error: {err}"),
30            Self::Frame(err) => write!(f, "Frame error: {err}"),
31            Self::PixelFormat(err) => write!(f, "PixelFormat error: {err}"),
32            Self::Quant(err) => write!(f, "Quant error: {err}"),
33            Self::Output(err) => write!(f, "Output error: {err}"),
34            Self::Io(err) => write!(f, "I/O error: {err}"),
35        }
36    }
37}
38
39#[cfg(feature = "std")]
40impl From<std::io::Error> for Error {
41    fn from(err: std::io::Error) -> Self {
42        Self::Io(format!("{err}"))
43    }
44}