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
use std::convert::From;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
Msg(String),
IntegerOverflow(String),
InvalidArgument(String),
}
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
}
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match self.kind {
ErrorKind::IntegerOverflow(ref s) => s.as_str(),
ErrorKind::InvalidArgument(ref s) => s.as_str(),
ErrorKind::Msg(ref s) => s.as_str(),
}
}
fn cause(&self) -> Option<&dyn std::error::Error> {
None
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.kind, f)
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Self { kind }
}
}
impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::IntegerOverflow(ref s) => write!(f, "IntegerOverflow: {}", s),
ErrorKind::InvalidArgument(ref s) => write!(f, "InvalidArgument: {}", s),
ErrorKind::Msg(ref s) => write!(f, "Msg: {}", s),
}
}
}
impl From<String> for ErrorKind {
fn from(s: String) -> Self {
ErrorKind::Msg(s)
}
}
impl<'a> From<&'a str> for ErrorKind {
fn from(s: &'a str) -> Self {
ErrorKind::Msg(s.to_string())
}
}