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
use std::{
fmt::{self, Display},
io,
};
use toml;
macro_rules! format_err {
($kind:path, $msg:expr) => {
crate::error::Error::new(
$kind,
&$msg.to_string()
)
};
($kind:path, $fmt:expr, $($arg:tt)+) => {
format_err!($kind, &format!($fmt, $($arg)+))
};
}
macro_rules! fail {
($kind:path, $msg:expr) => {
return Err(format_err!($kind, $msg).into());
};
($kind:path, $fmt:expr, $($arg:tt)+) => {
fail!($kind, &format!($fmt, $($arg)+));
};
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
msg: String,
}
impl Error {
pub fn new<S: ToString>(kind: ErrorKind, description: &S) -> Self {
Self {
kind,
msg: description.to_string(),
}
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", &self.kind, &self.msg)
}
}
impl std::error::Error for Error {}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ErrorKind {
Io,
Parse,
Version,
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let description = match self {
ErrorKind::Io => "I/O operation failed",
ErrorKind::Parse => "parse error",
ErrorKind::Version => "bad version",
};
write!(f, "{}", description)
}
}
impl From<io::Error> for Error {
fn from(other: io::Error) -> Self {
format_err!(ErrorKind::Io, &other)
}
}
impl From<semver::SemVerError> for Error {
fn from(other: semver::SemVerError) -> Self {
format_err!(ErrorKind::Version, &other)
}
}
impl From<semver::ReqParseError> for Error {
fn from(other: semver::ReqParseError) -> Self {
format_err!(ErrorKind::Version, &other)
}
}
impl From<toml::de::Error> for Error {
fn from(other: toml::de::Error) -> Self {
format_err!(ErrorKind::Parse, &other)
}
}