1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    Appellation: external <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::ErrorType;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strum::{Display, EnumCount, EnumIs, VariantNames};

#[derive(
    Clone,
    Copy,
    Debug,
    Default,
    Display,
    EnumCount,
    EnumIs,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    VariantNames,
)]
#[cfg_attr(
    feature = "serde",
    derive(Deserialize, Serialize,),
    serde(rename_all = "snake_case")
)]
#[strum(serialize_all = "snake_case")]
pub enum ExternalError<E = String> {
    Custom(E),
    #[default]
    Unknown,
}

impl<E> ExternalError<E> {
    pub fn new(error: E) -> Self {
        Self::Custom(error)
    }

    pub fn unknown() -> Self {
        Self::Unknown
    }
}

impl<E> std::error::Error for ExternalError<E> where E: std::fmt::Debug {}

impl<E> ErrorType for ExternalError<E>
where
    E: ToString,
{
    type Kind = ExternalError<E>;

    fn kind(&self) -> &Self::Kind {
        &self
    }

    fn name(&self) -> String {
        match self {
            Self::Custom(inner) => inner.to_string(),
            _ => self.to_string(),
        }
    }
}