| 1 | //! Different variant error |
| 2 | |
| 3 | use core::fmt; |
| 4 | |
| 5 | /// An error type indicating that a [`TryFrom`](core::convert::TryFrom) call failed because the |
| 6 | /// original value was of a different variant. |
| 7 | #[derive (Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | pub struct DifferentVariant; |
| 9 | |
| 10 | impl fmt::Display for DifferentVariant { |
| 11 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 12 | write!(f, "value was of a different variant than required" ) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | #[cfg (feature = "std" )] |
| 17 | #[allow (clippy::std_instead_of_core)] |
| 18 | impl std::error::Error for DifferentVariant {} |
| 19 | |
| 20 | impl From<DifferentVariant> for crate::Error { |
| 21 | fn from(err: DifferentVariant) -> Self { |
| 22 | Self::DifferentVariant(err) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl TryFrom<crate::Error> for DifferentVariant { |
| 27 | type Error = Self; |
| 28 | |
| 29 | fn try_from(err: crate::Error) -> Result<Self, Self::Error> { |
| 30 | match err { |
| 31 | crate::Error::DifferentVariant(err: DifferentVariant) => Ok(err), |
| 32 | _ => Err(Self), |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |