1//! Different variant error
2
3use 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)]
8pub struct DifferentVariant;
9
10impl 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")]
17impl std::error::Error for DifferentVariant {}
18
19impl From<DifferentVariant> for crate::Error {
20 fn from(err: DifferentVariant) -> Self {
21 Self::DifferentVariant(err)
22 }
23}
24
25impl TryFrom<crate::Error> for DifferentVariant {
26 type Error = Self;
27
28 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
29 match err {
30 crate::Error::DifferentVariant(err: DifferentVariant) => Ok(err),
31 _ => Err(Self),
32 }
33 }
34}
35