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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crossbeam_channel;
#[cfg(target_os = "linux")]
use mio_extras;
use std::error::Error as StdError;
use std::path::PathBuf;
use std::result::Result as StdResult;
use std::{self, fmt, io};
use crate::Config;
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
Generic(String),
Io(io::Error),
PathNotFound,
WatchNotFound,
InvalidConfig(Config),
}
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub paths: Vec<PathBuf>,
}
impl Error {
pub fn add_path(mut self, path: PathBuf) -> Self {
self.paths.push(path);
self
}
pub fn set_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.paths = paths;
self
}
pub fn new(kind: ErrorKind) -> Self {
Self {
kind,
paths: Vec::new(),
}
}
pub fn generic(msg: &str) -> Self {
Self::new(ErrorKind::Generic(msg.into()))
}
pub fn io(err: io::Error) -> Self {
Self::new(ErrorKind::Io(err))
}
pub fn path_not_found() -> Self {
Self::new(ErrorKind::PathNotFound)
}
pub fn watch_not_found() -> Self {
Self::new(ErrorKind::WatchNotFound)
}
pub fn invalid_config(config: &Config) -> Self {
Self::new(ErrorKind::InvalidConfig(config.clone()))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let error = match self.kind {
ErrorKind::PathNotFound => "No path was found.".into(),
ErrorKind::WatchNotFound => "No watch was found.".into(),
ErrorKind::InvalidConfig(ref config) => format!("Invalid configuration: {:?}", config),
ErrorKind::Generic(ref err) => err.clone(),
ErrorKind::Io(ref err) => err.description().into(),
};
if self.paths.is_empty() {
write!(f, "{}", error)
} else {
write!(f, "{} about {:?}", error, self.paths)
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match self.kind {
ErrorKind::PathNotFound => "Path(s) were not found",
ErrorKind::WatchNotFound => "No watch was found",
ErrorKind::InvalidConfig(_) => "Invalid configuration",
ErrorKind::Generic(_) => "Generic error",
ErrorKind::Io(_) => "I/O Error",
}
}
fn cause(&self) -> Option<&StdError> {
match self.kind {
ErrorKind::Io(ref cause) => Some(cause),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::io(err)
}
}
impl<T> From<crossbeam_channel::SendError<T>> for Error {
fn from(err: crossbeam_channel::SendError<T>) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
impl From<crossbeam_channel::RecvError> for Error {
fn from(err: crossbeam_channel::RecvError) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(err: std::sync::PoisonError<T>) -> Self {
Error::generic(&format!("internal mutex poisoned: {:?}", err))
}
}
#[cfg(target_os = "linux")]
impl<T> From<mio_extras::channel::SendError<T>> for Error {
fn from(err: mio_extras::channel::SendError<T>) -> Self {
Error::generic(&format!("internal channel error: {:?}", err))
}
}
#[test]
fn display_formatted_errors() {
let expected = "Some error";
assert_eq!(expected, format!("{}", Error::generic(expected)));
assert_eq!(
expected,
format!(
"{}",
Error::io(io::Error::new(io::ErrorKind::Other, expected))
)
);
}