1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Errors

use std::{
    fmt::{self, Display},
    io,
};

/// It handles internal, I/O and formatter errors
#[non_exhaustive]
#[derive(Debug)]
pub enum MoshError {
    /// Data format is not supported.
    DecodingError(png::DecodingError),
    /// i.e. wrong data size/formatter failure.
    EncodingError(png::EncodingError),
    /// I/O errors.
    IoError(io::Error),
    /// Allocation failed.
    OutOfMemory,
    /// Unsupported color type.
    UnsupportedColorType,
}

impl std::error::Error for MoshError {}

impl Display for MoshError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DecodingError(e) => Display::fmt(e, f),
            Self::EncodingError(e) => Display::fmt(e, f),
            Self::IoError(e) => Display::fmt(e, f),
            Self::OutOfMemory => f.write_str("Out of memory"),
            Self::UnsupportedColorType => f.write_str("Unsupported color type"),
        }
    }
}

impl From<io::Error> for MoshError {
    fn from(e: io::Error) -> Self {
        Self::IoError(e)
    }
}

impl From<png::DecodingError> for MoshError {
    fn from(e: png::DecodingError) -> Self {
        Self::DecodingError(e)
    }
}

impl From<png::EncodingError> for MoshError {
    fn from(e: png::EncodingError) -> Self {
        Self::EncodingError(e)
    }
}