devela/media/image/
error.rs

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// devela::media::image::error
//
//!
//

// use crate::Mismatch; use crate::IntErrorKind;
#[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
use crate::IoErrorKind;

#[doc = crate::TAG_RESULT!()]
/// An image-related result.
pub type ImageResult<T> = crate::Result<T, ImageError>;

#[doc = crate::TAG_ERROR_COMPOSITE!()]
/// An image-related error.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageError {
    /// Invalid image size, with an optional width and height.
    // InvalidImageSize(Mismatch<SizeUsize, SizeUsize>), // TODO
    InvalidImageSize(Option<(usize, usize)>), // TEMP

    /// Invalid pixel value.
    InvalidPixel, // IMPROVE add optional data

    /// Invalid magic number
    InvalidMagicNumber,

    /* from std */
    ///
    // WAIT: [Derive Copy and Hash for IntErrorKind](https://github.com/rust-lang/rust/pull/131923)
    // InvalidParsedInteger(IntErrorKind), // Does not implement Copy
    InvalidParsedInteger,

    /// A `core::fmt::Error`.
    FmtError,

    /// An `I/O` error.
    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
    #[cfg_attr(
        feature = "nightly_doc",
        doc(cfg(any(feature = "std", all(not(feature = "std"), feature = "io"))))
    )]
    IoError(IoErrorKind),
}

mod core_impls {
    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
    use crate::IoError;
    use crate::{Display, FmtResult, Formatter, ImageError};
    use core::fmt;

    impl crate::Error for ImageError {}
    impl crate::ExtError for ImageError {
        type Kind = ();
        fn error_eq(&self, other: &Self) -> bool {
            self == other
        }
        fn error_kind(&self) -> Self::Kind {}
    }

    impl Display for ImageError {
        fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
            use ImageError as E;
            match self {
                E::InvalidImageSize(o) => write!(f, "InvalidImageSize: {o:?}"),
                E::InvalidMagicNumber => write!(f, "Invalid magic number."),
                E::InvalidPixel => write!(f, "Invalid pixel."),
                //
                // E::InvalidParsedInteger(k) => write!(f, "Invalid parsed integer: {k:?}."),
                E::InvalidParsedInteger => write!(f, "Invalid parsed integer."),
                E::FmtError => write!(f, "A core::fmt::Error."),
                #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
                E::IoError(e) => write!(f, "An I/O Error: {e:?}"),
            }
        }
    }

    // IMPROVE
    impl From<crate::ParseIntError> for ImageError {
        fn from(_: crate::ParseIntError) -> Self {
            // Self::InvalidParsedInteger(e.kind().clone())
            Self::InvalidParsedInteger
        }
    }
    // IMPROVE
    impl From<fmt::Error> for ImageError {
        fn from(_: fmt::Error) -> Self {
            Self::FmtError
        }
    }
    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
    impl From<IoError> for ImageError {
        fn from(e: IoError) -> Self {
            Self::IoError(e.kind())
        }
    }
}