1use crate::Config;
4use std::error::Error as StdError;
5use std::path::PathBuf;
6use std::result::Result as StdResult;
7use std::{self, fmt, io};
8
9pub type Result<T> = StdResult<T, Error>;
11
12#[derive(Debug)]
14pub enum ErrorKind {
15 Generic(String),
20
21 Io(io::Error),
23
24 PathNotFound,
26
27 WatchNotFound,
29
30 InvalidConfig(Config),
32
33 MaxFilesWatch,
35}
36
37#[derive(Debug)]
45pub struct Error {
46 pub kind: ErrorKind,
48
49 pub paths: Vec<PathBuf>,
51}
52
53impl Error {
54 #[must_use]
56 pub fn add_path(mut self, path: PathBuf) -> Self {
57 self.paths.push(path);
58 self
59 }
60
61 #[must_use]
63 pub fn set_paths(mut self, paths: Vec<PathBuf>) -> Self {
64 self.paths = paths;
65 self
66 }
67
68 #[must_use]
70 pub fn new(kind: ErrorKind) -> Self {
71 Self {
72 kind,
73 paths: Vec::new(),
74 }
75 }
76
77 #[must_use]
79 pub fn generic(msg: &str) -> Self {
80 Self::new(ErrorKind::Generic(msg.into()))
81 }
82
83 #[must_use]
85 pub fn io(err: io::Error) -> Self {
86 Self::new(ErrorKind::Io(err))
87 }
88
89 #[must_use]
91 pub fn io_watch(err: io::Error) -> Self {
92 if err.kind() == io::ErrorKind::NotFound {
93 Self::path_not_found()
94 } else {
95 Self::io(err)
96 }
97 }
98
99 #[must_use]
101 pub fn path_not_found() -> Self {
102 Self::new(ErrorKind::PathNotFound)
103 }
104
105 #[must_use]
107 pub fn watch_not_found() -> Self {
108 Self::new(ErrorKind::WatchNotFound)
109 }
110
111 #[must_use]
115 pub fn invalid_config(config: &Config) -> Self {
116 Self::new(ErrorKind::InvalidConfig(config.without_ignored()))
117 }
118}
119
120impl fmt::Display for Error {
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122 let error = match self.kind {
123 ErrorKind::PathNotFound => "No path was found.".into(),
124 ErrorKind::WatchNotFound => "No watch was found.".into(),
125 ErrorKind::InvalidConfig(ref config) => format!("Invalid configuration: {config:?}"),
126 ErrorKind::Generic(ref err) => err.clone(),
127 ErrorKind::Io(ref err) => err.to_string(),
128 ErrorKind::MaxFilesWatch => "OS file watch limit reached.".into(),
129 };
130
131 if self.paths.is_empty() {
132 write!(f, "{error}")
133 } else {
134 write!(f, "{} about {:?}", error, self.paths)
135 }
136 }
137}
138
139impl StdError for Error {
140 fn cause(&self) -> Option<&dyn StdError> {
141 match self.kind {
142 ErrorKind::Io(ref cause) => Some(cause),
143 _ => None,
144 }
145 }
146}
147
148impl From<io::Error> for Error {
149 fn from(err: io::Error) -> Self {
150 Error::io(err)
151 }
152}
153
154impl<T> From<std::sync::mpsc::SendError<T>> for Error {
155 fn from(err: std::sync::mpsc::SendError<T>) -> Self {
156 Error::generic(&format!("internal channel disconnect: {err:?}"))
157 }
158}
159
160impl From<std::sync::mpsc::RecvError> for Error {
161 fn from(err: std::sync::mpsc::RecvError) -> Self {
162 Error::generic(&format!("internal channel disconnect: {err:?}"))
163 }
164}
165
166impl<T> From<std::sync::PoisonError<T>> for Error {
167 fn from(err: std::sync::PoisonError<T>) -> Self {
168 Error::generic(&format!("internal mutex poisoned: {err:?}"))
169 }
170}
171
172#[test]
173fn display_formatted_errors() {
174 let expected = "Some error";
175
176 assert_eq!(expected, format!("{}", Error::generic(expected)));
177
178 assert_eq!(
179 expected,
180 format!("{}", Error::io(io::Error::other(expected)))
181 );
182}
183
184#[test]
185fn invalid_config_does_not_keep_ignore_filter() {
186 use std::sync::Arc;
187
188 let captured = Arc::new(());
189 let config = Config::default().with_ignored({
190 let captured = Arc::clone(&captured);
191 move |_, _| Arc::strong_count(&captured) > 0
192 });
193 let error = Error::invalid_config(&config);
194 drop(config);
195
196 assert_eq!(Arc::strong_count(&captured), 1);
197 assert!(matches!(error.kind, ErrorKind::InvalidConfig(_)));
198}