1 | //! Conversion range error |
2 | |
3 | use core::fmt; |
4 | |
5 | use crate::error; |
6 | |
7 | /// An error type indicating that a conversion failed because the target type could not store the |
8 | /// initial value. |
9 | #[derive (Debug, Clone, Copy, PartialEq, Eq)] |
10 | pub struct ConversionRange; |
11 | |
12 | impl fmt::Display for ConversionRange { |
13 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
14 | f.write_str(data:"Source value is out of range for the target type" ) |
15 | } |
16 | } |
17 | |
18 | #[cfg (feature = "std" )] |
19 | #[allow (clippy::std_instead_of_core)] |
20 | impl std::error::Error for ConversionRange {} |
21 | |
22 | impl From<ConversionRange> for crate::Error { |
23 | fn from(err: ConversionRange) -> Self { |
24 | Self::ConversionRange(err) |
25 | } |
26 | } |
27 | |
28 | impl TryFrom<crate::Error> for ConversionRange { |
29 | type Error = error::DifferentVariant; |
30 | |
31 | fn try_from(err: crate::Error) -> Result<Self, Self::Error> { |
32 | match err { |
33 | crate::Error::ConversionRange(err: ConversionRange) => Ok(err), |
34 | _ => Err(error::DifferentVariant), |
35 | } |
36 | } |
37 | } |
38 | |