sysfuss/
errors.rs

1/// Multi-error convenience enum
2#[derive(Debug, Clone)]
3pub enum EitherErr2<T1, T2> {
4    /// Type 1
5    First(T1),
6    /// Type 2
7    Second(T2),
8}
9
10impl <T1, T2> EitherErr2<T1, T2> {
11    /// Is this the first error type?
12    pub fn is_type1(&self) -> bool {
13        matches!(self, Self::First(_))
14    }
15    
16    /// Is this the second error type?
17    pub fn is_type2(&self) -> bool {
18        matches!(self, Self::Second(_))
19    }
20}
21
22impl <T> EitherErr2<std::convert::Infallible, T> {
23    /// Convert to non-infallible error
24    pub fn map_infallible_first(self) -> T {
25        match self {
26            Self::First(_e) => panic!("Infallible error cannot exist"),
27            Self::Second(e) => e,
28        }
29    }
30}
31
32impl <T> EitherErr2<T, std::convert::Infallible> {
33    /// Convert to non-infallible error
34    pub fn map_infallible_second(self) -> T {
35        match self {
36            Self::First(e) => e,
37            Self::Second(_e) => panic!("Infallible error cannot exist"),
38        }
39    }
40}
41
42impl <T1: std::fmt::Display, T2: std::fmt::Display> std::fmt::Display for EitherErr2<T1, T2> {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            Self::First(x) => write!(f, "{}", x),
46            Self::Second(x) => write!(f, "{}", x),
47        }
48    }
49}
50
51impl <T1: std::error::Error, T2: std::error::Error> std::error::Error for EitherErr2<T1, T2> {}
52
53/// Value parse error for an enumeration.
54/// This usually indicates an unexpected value (one without an enum variant) was received while parsing.
55#[derive(Debug, Clone)]
56pub struct ValueEnumError<'a> {
57    pub(crate) got: String,
58    pub(crate) allowed: &'a [&'a str],
59}
60
61impl <'a> std::fmt::Display for ValueEnumError<'a> {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "Got {}, expected one of {:?}", self.got, self.allowed)
64    }
65}
66
67impl <'a> std::error::Error for ValueEnumError<'a> {}