1#[derive(
7 Clone,
8 Debug,
9 Eq,
10 Hash,
11 Ord,
12 PartialEq,
13 PartialOrd,
14 serde::Deserialize,
15 serde::Serialize,
16 strum::EnumDiscriminants,
17 strum::EnumIs,
18)]
19#[serde(rename_all = "kebab-case")]
20#[strum(serialize_all = "kebab-case")]
21#[strum_discriminants(
22 name(ErrorKind),
23 derive(
24 Hash,
25 Ord,
26 PartialOrd,
27 serde::Deserialize,
28 serde::Serialize,
29 strum::AsRefStr,
30 strum::Display,
31 strum::EnumIs,
32 strum::EnumString,
33 strum::VariantNames
34 )
35)]
36pub enum Error {
37 Config(String),
38 Serde(String),
39 Unknown(String),
40}
41
42impl Error {
43 pub fn new(kind: ErrorKind, message: impl ToString) -> Self {
44 let message = message.to_string();
45 match kind {
46 ErrorKind::Config => Self::Config(message),
47 ErrorKind::Serde => Self::Serde(message),
48 ErrorKind::Unknown => Self::Unknown(message),
49 }
50 }
51 pub fn kind(&self) -> ErrorKind {
53 match self {
54 Self::Config(_) => ErrorKind::Config,
55 Self::Serde(_) => ErrorKind::Serde,
56 Self::Unknown(_) => ErrorKind::Unknown,
57 }
58 }
59 pub fn message(&self) -> &str {
61 match self {
62 Self::Config(e) => e,
63 Self::Serde(e) => e,
64 Self::Unknown(e) => e,
65 }
66 }
67}
68
69impl core::fmt::Display for Error {
70 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
71 write!(
72 f,
73 "[{tag}] {content}",
74 tag = self.kind(),
75 content = self.message()
76 )
77 }
78}
79
80impl core::error::Error for Error {}
81
82unsafe impl Send for Error {}
83
84unsafe impl Sync for Error {}
85
86impl From<&str> for Error {
87 fn from(e: &str) -> Self {
88 Self::Unknown(e.to_string())
89 }
90}
91
92impl From<String> for Error {
93 fn from(e: String) -> Self {
94 Self::Unknown(e)
95 }
96}
97
98impl From<config::ConfigError> for Error {
99 fn from(e: config::ConfigError) -> Self {
100 Self::Config(e.to_string())
101 }
102}
103
104impl From<serde_json::Error> for Error {
105 fn from(e: serde_json::Error) -> Self {
106 Self::Serde(e.to_string())
107 }
108}