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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
use core::fmt;
use serde::Deserialize;
pub type MLE<T> = Result<T, MLErr>;
#[derive(Debug, Deserialize)]
pub struct MLErr {
etype: EType,
message: String,
}
#[derive(Debug, Deserialize)]
pub enum EType {
ArgumentError,
ArgumentCountError,
ConfigError,
DBError,
ModError,
LibToml,
LibSql,
LibReq,
LibChrono,
LibJson,
LibIndicatif,
IoError,
Other,
}
impl std::error::Error for MLErr {
fn description(&self) -> &str {
&self.message
}
}
impl fmt::Display for MLErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.etype {
EType::ArgumentError => {
write!(f, "User input not accepted: {}", self.message)
}
EType::ArgumentCountError => {
write!(f, "Too many/too few arguments")
}
EType::ConfigError => write!(f, "CONFIG"),
EType::DBError => write!(f, "Database: {}", self.message),
EType::ModError => write!(f, "Mod: {}", self.message),
EType::LibToml => write!(f, "TOML"),
EType::LibSql => write!(f, "SQL: {}", self.message),
EType::LibReq => write!(f, "REQWEST"),
EType::LibChrono => write!(f, "Chrono error: {}", self.message),
EType::LibJson => write!(f, "JSON: {}", self.message),
EType::LibIndicatif => write!(f, "Indicativ: {}", self.message),
EType::IoError => write!(f, "IO"),
EType::Other => write!(f, "OTHER"),
}
}
}
impl From<reqwest::Error> for MLErr {
fn from(error: reqwest::Error) -> Self {
Self {
etype: EType::LibReq,
message: error.to_string(),
}
}
}
impl From<toml::de::Error> for MLErr {
fn from(error: toml::de::Error) -> Self {
Self {
etype: EType::LibToml,
message: error.to_string(),
}
}
}
impl From<rusqlite::Error> for MLErr {
fn from(error: rusqlite::Error) -> Self {
Self {
etype: EType::LibSql,
message: error.to_string(),
}
}
}
impl From<toml::ser::Error> for MLErr {
fn from(error: toml::ser::Error) -> Self {
Self {
etype: EType::LibToml,
message: error.to_string(),
}
}
}
impl From<chrono::ParseError> for MLErr {
fn from(error: chrono::ParseError) -> Self {
Self {
etype: EType::LibChrono,
message: error.to_string(),
}
}
}
impl From<std::io::Error> for MLErr {
fn from(error: std::io::Error) -> Self {
Self {
etype: EType::IoError,
message: error.to_string(),
}
}
}
impl From<serde_json::error::Error> for MLErr {
fn from(value: serde_json::error::Error) -> Self {
Self {
etype: EType::LibJson,
message: value.to_string(),
}
}
}
impl MLErr {
#[must_use] pub fn new(etype: EType, message: &str) -> Self {
Self {
etype,
message: String::from(message),
}
}
}
|