devela/media/image/
error.rs

1// devela::media::image::error
2//
3//!
4//
5
6// use crate::Mismatch; use crate::IntErrorKind;
7#[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
8use crate::IoErrorKind;
9
10#[doc = crate::TAG_RESULT!()]
11/// An image-related result.
12pub type ImageResult<T> = crate::Result<T, ImageError>;
13
14#[doc = crate::TAG_ERROR_COMPOSITE!()]
15/// An image-related error.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum ImageError {
18    /// Invalid image size, with an optional width and height.
19    // InvalidImageSize(Mismatch<SizeUsize, SizeUsize>), // TODO
20    InvalidImageSize(Option<(usize, usize)>), // TEMP
21
22    /// Invalid pixel value.
23    InvalidPixel, // IMPROVE add optional data
24
25    /// Invalid magic number
26    InvalidMagicNumber,
27
28    /* from std */
29    ///
30    // WAIT: [Derive Copy and Hash for IntErrorKind](https://github.com/rust-lang/rust/pull/131923)
31    // InvalidParsedInteger(IntErrorKind), // Does not implement Copy
32    InvalidParsedInteger,
33
34    /// A `core::fmt::Error`.
35    FmtError,
36
37    /// An `I/O` error.
38    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
39    #[cfg_attr(
40        feature = "nightly_doc",
41        doc(cfg(any(feature = "std", all(not(feature = "std"), feature = "io"))))
42    )]
43    IoError(IoErrorKind),
44}
45
46mod core_impls {
47    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
48    use crate::IoError;
49    use crate::{Display, FmtResult, Formatter, ImageError};
50    use core::fmt;
51
52    impl crate::Error for ImageError {}
53    impl crate::ExtError for ImageError {
54        type Kind = ();
55        fn error_eq(&self, other: &Self) -> bool {
56            self == other
57        }
58        fn error_kind(&self) -> Self::Kind {}
59    }
60
61    impl Display for ImageError {
62        fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
63            use ImageError as E;
64            match self {
65                E::InvalidImageSize(o) => write!(f, "InvalidImageSize: {o:?}"),
66                E::InvalidMagicNumber => write!(f, "Invalid magic number."),
67                E::InvalidPixel => write!(f, "Invalid pixel."),
68                //
69                // E::InvalidParsedInteger(k) => write!(f, "Invalid parsed integer: {k:?}."),
70                E::InvalidParsedInteger => write!(f, "Invalid parsed integer."),
71                E::FmtError => write!(f, "A core::fmt::Error."),
72                #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
73                E::IoError(e) => write!(f, "An I/O Error: {e:?}"),
74            }
75        }
76    }
77
78    // IMPROVE
79    impl From<crate::ParseIntError> for ImageError {
80        fn from(_: crate::ParseIntError) -> Self {
81            // Self::InvalidParsedInteger(e.kind().clone())
82            Self::InvalidParsedInteger
83        }
84    }
85    // IMPROVE
86    impl From<fmt::Error> for ImageError {
87        fn from(_: fmt::Error) -> Self {
88            Self::FmtError
89        }
90    }
91    #[cfg(any(feature = "std", all(not(feature = "std"), feature = "io")))]
92    impl From<IoError> for ImageError {
93        fn from(e: IoError) -> Self {
94            Self::IoError(e.kind())
95        }
96    }
97}