1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[cfg(feature = "file-watcher")]
13 #[error("File watcher error: {0}")]
14 Watcher(String),
15
16 #[error("I/O error: {0}")]
18 Io(#[from] std::io::Error),
19
20 #[error("Utility error: {0}")]
22 Utility(String),
23}
24
25impl Error {
26 #[cfg(feature = "file-watcher")]
28 pub fn watcher<S: Into<String>>(msg: S) -> Self {
29 Self::Watcher(msg.into())
30 }
31
32 pub fn utility<S: Into<String>>(msg: S) -> Self {
34 Self::Utility(msg.into())
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_error_display() {
44 let err = Error::utility("test error");
45 assert_eq!(err.to_string(), "Utility error: test error");
46 }
47
48 #[test]
49 fn test_error_from_io() {
50 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
51 let err = Error::from(io_err);
52 assert!(matches!(err, Error::Io(_)));
53 }
54
55 #[cfg(feature = "file-watcher")]
56 #[test]
57 fn test_watcher_error() {
58 let err = Error::watcher("test watcher error");
59 assert!(matches!(err, Error::Watcher(_)));
60 assert_eq!(err.to_string(), "File watcher error: test watcher error");
61 }
62}