1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
6pub enum SweeperError {
7 #[error("Configuration error: {0}")]
8 Config(#[from] ConfigError),
9
10 #[error("IO error at path '{path}': {source}")]
11 Io {
12 path: PathBuf,
13 #[source]
14 source: std::io::Error,
15 },
16
17 #[error("IO error: {0}")]
18 IoSimple(#[from] std::io::Error),
19
20 #[error("Permission denied: {0}\n Hint: Try running with sudo or check file permissions")]
21 PermissionDenied(PathBuf),
22
23 #[error("Path not found: {0}\n Hint: Check if the path exists and is spelled correctly")]
24 PathNotFound(PathBuf),
25
26 #[error("Not a directory: {0}\n Hint: Provide a directory path, not a file")]
27 NotADirectory(PathBuf),
28
29 #[error("Invalid path: {0}")]
30 InvalidPath(String),
31
32 #[error("JSON serialization error: {0}")]
33 Json(#[from] serde_json::Error),
34
35 #[error("System call failed: {0}")]
36 Nix(#[from] nix::Error),
37
38 #[error("Notification failed: {0}")]
39 Notification(#[from] notify_rust::error::Error),
40
41 #[error("Monitor already running (PID: {0})\n Hint: Use 'rusty-sweeper monitor --stop' to stop it first")]
42 AlreadyRunning(u32),
43
44 #[error("Command failed: {0}")]
45 Command(String),
46
47 #[error("Not found: {0}")]
48 NotFound(String),
49
50 #[error("{0}")]
51 Other(String),
52}
53
54impl SweeperError {
55 pub fn exit_code(&self) -> i32 {
57 match self {
58 SweeperError::Config(_) => 2,
59 SweeperError::Io { .. } => 1,
60 SweeperError::IoSimple(_) => 1,
61 SweeperError::PermissionDenied(_) => 3,
62 SweeperError::PathNotFound(_) => 1,
63 SweeperError::NotADirectory(_) => 1,
64 SweeperError::InvalidPath(_) => 1,
65 SweeperError::Json(_) => 1,
66 SweeperError::Nix(_) => 1,
67 SweeperError::Notification(_) => 1,
68 SweeperError::AlreadyRunning(_) => 4,
69 SweeperError::Command(_) => 1,
70 SweeperError::NotFound(_) => 1,
71 SweeperError::Other(_) => 1,
72 }
73 }
74}
75
76#[derive(Error, Debug)]
78pub enum ConfigError {
79 #[error("Failed to read config file '{path}': {source}")]
80 ReadError {
81 path: PathBuf,
82 #[source]
83 source: std::io::Error,
84 },
85
86 #[error("Failed to parse config file '{path}': {source}")]
87 ParseError {
88 path: PathBuf,
89 #[source]
90 source: toml::de::Error,
91 },
92
93 #[error("Invalid configuration: {0}")]
94 Invalid(String),
95}
96
97pub type Result<T> = std::result::Result<T, SweeperError>;
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn error_display_messages() {
106 let err = ConfigError::Invalid("threshold must be 0-100".into());
107 assert!(err.to_string().contains("threshold"));
108 }
109
110 #[test]
111 fn error_conversion() {
112 let config_err = ConfigError::Invalid("test".into());
113 let sweeper_err: SweeperError = config_err.into();
114 assert!(matches!(sweeper_err, SweeperError::Config(_)));
115 }
116
117 #[test]
118 fn exit_code_config_error() {
119 let err = SweeperError::Config(ConfigError::Invalid("test".into()));
120 assert_eq!(err.exit_code(), 2);
121 }
122
123 #[test]
124 fn exit_code_permission_denied() {
125 let err = SweeperError::PermissionDenied(PathBuf::from("/secret"));
126 assert_eq!(err.exit_code(), 3);
127 }
128
129 #[test]
130 fn exit_code_path_not_found() {
131 let err = SweeperError::PathNotFound(PathBuf::from("/nonexistent"));
132 assert_eq!(err.exit_code(), 1);
133 }
134
135 #[test]
136 fn exit_code_not_a_directory() {
137 let err = SweeperError::NotADirectory(PathBuf::from("/file.txt"));
138 assert_eq!(err.exit_code(), 1);
139 }
140
141 #[test]
142 fn not_a_directory_error_message() {
143 let err = SweeperError::NotADirectory(PathBuf::from("/some/file"));
144 assert!(err.to_string().contains("Not a directory"));
145 assert!(err.to_string().contains("/some/file"));
146 assert!(err.to_string().contains("Hint"));
147 }
148
149 #[test]
150 fn other_error_message() {
151 let err = SweeperError::Other("something went wrong".into());
152 assert_eq!(err.to_string(), "something went wrong");
153 assert_eq!(err.exit_code(), 1);
154 }
155}