/rust/registry/src/index.crates.io-1949cf8c6b5b557f/exr-1.74.2/src/error.rs
Line | Count | Source |
1 | | //! Error type definitions. |
2 | | |
3 | | pub use std::io::{Error as IoError, Result as IoResult}; |
4 | | use std::{borrow::Cow, convert::TryFrom, error, fmt, io::ErrorKind, num::TryFromIntError}; |
5 | | |
6 | | // Export types |
7 | | |
8 | | /// A result that may contain an exr error. |
9 | | pub type Result<T> = std::result::Result<T, Error>; |
10 | | |
11 | | /// A result that, if ok, contains nothing, and otherwise contains an exr error. |
12 | | pub type UnitResult = Result<()>; |
13 | | |
14 | | /// An error that may happen while reading or writing an exr file. |
15 | | /// Distinguishes between three types of errors: |
16 | | /// unsupported features, invalid data, and file system errors. |
17 | | #[derive(Debug)] |
18 | | pub enum Error { |
19 | | /// Reading or Writing the file has been aborted by the caller. |
20 | | /// This error will never be triggered by this crate itself, |
21 | | /// only by users of this library. |
22 | | /// It exists to be returned from a progress callback. |
23 | | Aborted, // FIXME remove?? is not used really? |
24 | | |
25 | | /// The contents of the file are not supported by |
26 | | /// this specific implementation of open exr, |
27 | | /// even though the data may be valid. |
28 | | NotSupported(Cow<'static, str>), |
29 | | |
30 | | /// The contents of the image are contradicting or insufficient. |
31 | | /// Also returned for `ErrorKind::UnexpectedEof` errors. |
32 | | Invalid(Cow<'static, str>), |
33 | | |
34 | | /// The underlying byte stream could not be read successfully, |
35 | | /// probably due to file system related errors. |
36 | | Io(IoError), |
37 | | } |
38 | | |
39 | | impl Error { |
40 | | /// Create an error of the variant `Invalid`. |
41 | 0 | pub(crate) fn invalid(message: impl Into<Cow<'static, str>>) -> Self { |
42 | 0 | Error::Invalid(message.into()) |
43 | 0 | } Unexecuted instantiation: <exr::error::Error>::invalid::<alloc::string::String> Unexecuted instantiation: <exr::error::Error>::invalid::<&str> |
44 | | |
45 | | /// Create an error of the variant `NotSupported`. |
46 | 0 | pub(crate) fn unsupported(message: impl Into<Cow<'static, str>>) -> Self { |
47 | 0 | Error::NotSupported(message.into()) |
48 | 0 | } Unexecuted instantiation: <exr::error::Error>::unsupported::<alloc::string::String> Unexecuted instantiation: <exr::error::Error>::unsupported::<&str> |
49 | | } |
50 | | |
51 | | /// Enable using the `?` operator on `std::io::Result`. |
52 | | impl From<IoError> for Error { |
53 | 0 | fn from(error: IoError) -> Self { |
54 | 0 | if error.kind() == ErrorKind::UnexpectedEof { |
55 | 0 | Error::invalid("reference to missing bytes") |
56 | | } else { |
57 | 0 | Error::Io(error) |
58 | | } |
59 | 0 | } |
60 | | } |
61 | | |
62 | | // TODO use `usize::try_from(x)?` everywhere |
63 | | // Note: This provides a generic error message for integer conversion failures. |
64 | | // For better error messages, prefer using `.map_err(|_| |
65 | | // Error::invalid(format!("specific context"))) instead of relying on this From |
66 | | // impl. |
67 | | impl From<TryFromIntError> for Error { |
68 | 0 | fn from(_: TryFromIntError) -> Self { |
69 | 0 | Error::invalid("integer conversion failed: value out of range") |
70 | 0 | } |
71 | | } |
72 | | |
73 | | impl error::Error for Error { |
74 | 0 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
75 | 0 | match *self { |
76 | 0 | Error::Io(ref err) => Some(err), |
77 | 0 | _ => None, |
78 | | } |
79 | 0 | } |
80 | | } |
81 | | |
82 | | impl fmt::Display for Error { |
83 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
84 | 0 | match self { |
85 | 0 | Error::Io(err) => err.fmt(formatter), |
86 | 0 | Error::NotSupported(message) => write!(formatter, "not supported: {}", message), |
87 | 0 | Error::Invalid(message) => write!(formatter, "invalid: {}", message), |
88 | 0 | Error::Aborted => write!(formatter, "cancelled"), |
89 | | } |
90 | 0 | } |
91 | | } |
92 | | |
93 | | /// Typically used during decoding. Return error on invalid range. |
94 | | #[inline] |
95 | 0 | pub(crate) fn i32_to_usize(value: i32, error_message: &'static str) -> Result<usize> { |
96 | 0 | usize::try_from(value).map_err(|_| { |
97 | 0 | if value < 0 { |
98 | 0 | Error::invalid(error_message) |
99 | | } else { |
100 | 0 | Error::unsupported(error_message) |
101 | | } |
102 | 0 | }) |
103 | 0 | } Unexecuted instantiation: exr::error::i32_to_usize Unexecuted instantiation: exr::error::i32_to_usize Unexecuted instantiation: exr::error::i32_to_usize |
104 | | |
105 | | /// Typically used during encoding. Return error on overflow. |
106 | | #[inline] |
107 | 0 | pub(crate) fn usize_to_u32(value: usize, error_message: &'static str) -> Result<u32> { |
108 | 0 | u32::try_from(value).map_err(|_| Error::invalid(error_message)) Unexecuted instantiation: exr::error::usize_to_u32::{closure#0}Unexecuted instantiation: exr::error::usize_to_u32::{closure#0}Unexecuted instantiation: exr::error::usize_to_u32::{closure#0} |
109 | 0 | } Unexecuted instantiation: exr::error::usize_to_u32 Unexecuted instantiation: exr::error::usize_to_u32 Unexecuted instantiation: exr::error::usize_to_u32 |
110 | | |
111 | | /// Typically used during encoding. Return error on invalid range. |
112 | | #[inline] |
113 | 0 | pub(crate) fn usize_to_u16(value: usize, error_message: &'static str) -> Result<u16> { |
114 | 0 | u16::try_from(value).map_err(|_| Error::invalid(error_message)) |
115 | 0 | } |
116 | | |
117 | | /// Typically used during decoding. Return error on invalid range. |
118 | | #[inline] |
119 | 0 | pub(crate) fn u64_to_usize(value: u64, error_message: &'static str) -> Result<usize> { |
120 | 0 | usize::try_from(value).map_err(|_| Error::unsupported(error_message)) |
121 | 0 | } Unexecuted instantiation: exr::error::u64_to_usize Unexecuted instantiation: exr::error::u64_to_usize Unexecuted instantiation: exr::error::u64_to_usize |
122 | | |
123 | | /// Typically used during decoding. Return error on invalid range. |
124 | | #[inline] |
125 | 0 | pub(crate) fn u32_to_usize(value: u32, error_message: &'static str) -> Result<usize> { |
126 | 0 | usize::try_from(value).map_err(|_| Error::unsupported(error_message)) |
127 | 0 | } Unexecuted instantiation: exr::error::u32_to_usize Unexecuted instantiation: exr::error::u32_to_usize |
128 | | |
129 | | /// Typically used during encoding. Return error on invalid range. |
130 | | #[inline] |
131 | 0 | pub(crate) fn usize_to_i32(value: usize, error_message: &'static str) -> Result<i32> { |
132 | 0 | i32::try_from(value).map_err(|_| Error::invalid(error_message)) |
133 | 0 | } Unexecuted instantiation: exr::error::usize_to_i32 Unexecuted instantiation: exr::error::usize_to_i32 Unexecuted instantiation: exr::error::usize_to_i32 |
134 | | |
135 | | /// Typically used during encoding. Return error on invalid range. |
136 | | #[inline] |
137 | 0 | pub(crate) fn usize_to_u64(value: usize, error_message: &'static str) -> Result<u64> { |
138 | 0 | u64::try_from(value).map_err(|_| Error::invalid(error_message)) Unexecuted instantiation: exr::error::usize_to_u64::{closure#0}Unexecuted instantiation: exr::error::usize_to_u64::{closure#0}Unexecuted instantiation: exr::error::usize_to_u64::{closure#0} |
139 | 0 | } Unexecuted instantiation: exr::error::usize_to_u64 Unexecuted instantiation: exr::error::usize_to_u64 Unexecuted instantiation: exr::error::usize_to_u64 |