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
use std::{error::Error,
fmt::{Display, Result as FmtResult},
result::Result as OGResult};
pub type CommonResult<T> = OGResult<T, Box<dyn Error + Send + Sync>>;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CommonError {
err_type: CommonErrorType,
msg: Option<String>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum CommonErrorType {
ExitLoop,
General,
InvalidArguments,
InvalidResult,
InvalidState,
StackOverflow,
StackUnderflow,
ParsingError,
IOError,
ValueOutOfRange,
InvalidValue,
}
impl Default for CommonErrorType {
fn default() -> Self { CommonErrorType::General }
}
impl Error for CommonError {}
impl Display for CommonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> FmtResult { write!(f, "{:?}", self) }
}
impl CommonError {
#[allow(clippy::all)]
pub fn new<T>(err_type: CommonErrorType, msg: &str) -> CommonResult<T> {
Self::from_err(CommonError {
err_type,
msg: Some(msg.to_string()),
})
}
pub fn new_err_with_only_type<T>(err_type: CommonErrorType) -> CommonResult<T> {
CommonError::from_err_type_and_msg(err_type, None)
}
pub fn new_err_with_only_msg<T>(msg: &str) -> CommonResult<T> {
CommonError::from_err_type_and_msg(CommonErrorType::General, Some(msg.to_string()))
}
fn from_err_type_and_msg<T>(err_type: CommonErrorType, msg: Option<String>) -> CommonResult<T> {
Self::from_err(CommonError { err_type, msg })
}
fn from_err<T>(err: CommonError) -> CommonResult<T> { Err(Box::new(err)) }
}