libmosh/
err.rs

1//! Errors
2
3use std::{
4    fmt::{self, Display},
5    io,
6};
7
8/// It handles internal, I/O and formatter errors
9#[non_exhaustive]
10#[derive(Debug)]
11pub enum MoshError {
12    /// Data format is not supported.
13    DecodingError(png::DecodingError),
14    /// i.e. wrong data size/formatter failure.
15    EncodingError(png::EncodingError),
16    /// I/O errors.
17    IoError(io::Error),
18    /// Allocation failed.
19    OutOfMemory,
20    /// Unsupported color type.
21    UnsupportedColorType,
22}
23
24impl std::error::Error for MoshError {}
25
26impl Display for MoshError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::DecodingError(e) => Display::fmt(e, f),
30            Self::EncodingError(e) => Display::fmt(e, f),
31            Self::IoError(e) => Display::fmt(e, f),
32            Self::OutOfMemory => f.write_str("Out of memory"),
33            Self::UnsupportedColorType => f.write_str("Unsupported color type"),
34        }
35    }
36}
37
38impl From<io::Error> for MoshError {
39    fn from(e: io::Error) -> Self {
40        Self::IoError(e)
41    }
42}
43
44impl From<png::DecodingError> for MoshError {
45    fn from(e: png::DecodingError) -> Self {
46        Self::DecodingError(e)
47    }
48}
49
50impl From<png::EncodingError> for MoshError {
51    fn from(e: png::EncodingError) -> Self {
52        Self::EncodingError(e)
53    }
54}