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