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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{Icon, ResampleError};
use std::{
    convert::From,
    error::Error,
    fmt::{self, Debug, Display, Formatter},
    io
};

/// The error type for operations of the `Encode` trait.
pub enum EncodingError<K: Icon + Send + Sync> {
    /// The icon already includes an icon associated with this icon.
    AlreadyIncluded(K),
    /// A resampling error.
    Resample(ResampleError),
    /// The icon aready stores the maximum number of icons possible.
    Full(u16)
}

impl<K: Icon + Send + Sync> Display for EncodingError<K> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AlreadyIncluded(_) => write!(
                f,
                "The Encode already contains an icon associated with this icon"
            ),
            Self::Resample(err) => <ResampleError as Display>::fmt(&err, f),
            Self::Full(max_n) => write!(
                f,
                "The icon has already reached it's maximum capacity ({} icons)",
                max_n
            )
        }
    }
}

impl<K: Icon + Send + Sync + Debug> Debug for EncodingError<K> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::AlreadyIncluded(e) => write!(
                f,
                "EncodingError::AlreadyIncluded({:?})",
                e
            ),
            Self::Resample(err) => write!(f, "EncodingError::Resample({:?})", err),
            Self::Full(n) => write!(f, "EncodingError::Full({})", n)
        }
    }
}

impl<K: Icon + Send + Sync + Debug> Error for EncodingError<K> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        if let Self::Resample(ref err) = self {
            err.source()
        } else {
            None
        }
    }
}

impl<K: Icon + Send + Sync> From<ResampleError> for EncodingError<K> {
    fn from(err: ResampleError) -> Self {
        Self::Resample(err)
    }
}

impl<K: Icon + Send + Sync> From<io::Error> for EncodingError<K> {
    fn from(err: io::Error) -> Self {
        Self::from(ResampleError::from(err))
    }
}

impl<K: Icon + Send + Sync> Into<io::Error> for EncodingError<K> {
    fn into(self) -> io::Error {
        if let Self::Resample(err) = self {
            err.into()
        } else {
            io::Error::new(io::ErrorKind::InvalidInput, format!("{}", self))
        }
    }
}