Skip to main content

rusty_sweeper/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4/// Core library errors
5#[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("Command failed: {0}")]
39    Command(String),
40
41    #[error("Not found: {0}")]
42    NotFound(String),
43
44    #[error("{0}")]
45    Other(String),
46}
47
48impl SweeperError {
49    /// Get exit code for this error type
50    pub fn exit_code(&self) -> i32 {
51        match self {
52            SweeperError::Config(_) => 2,
53            SweeperError::Io { .. } => 1,
54            SweeperError::IoSimple(_) => 1,
55            SweeperError::PermissionDenied(_) => 3,
56            SweeperError::PathNotFound(_) => 1,
57            SweeperError::NotADirectory(_) => 1,
58            SweeperError::InvalidPath(_) => 1,
59            SweeperError::Json(_) => 1,
60            SweeperError::Nix(_) => 1,
61            SweeperError::Command(_) => 1,
62            SweeperError::NotFound(_) => 1,
63            SweeperError::Other(_) => 1,
64        }
65    }
66}
67
68/// Configuration-specific errors
69#[derive(Error, Debug)]
70pub enum ConfigError {
71    #[error("Failed to read config file '{path}': {source}")]
72    ReadError {
73        path: PathBuf,
74        #[source]
75        source: std::io::Error,
76    },
77
78    #[error("Failed to parse config file '{path}': {source}")]
79    ParseError {
80        path: PathBuf,
81        #[source]
82        source: toml::de::Error,
83    },
84
85    #[error("Invalid configuration: {0}")]
86    Invalid(String),
87}
88
89/// Result type alias for convenience
90pub type Result<T> = std::result::Result<T, SweeperError>;
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn error_display_messages() {
98        let err = ConfigError::Invalid("threshold must be 0-100".into());
99        assert!(err.to_string().contains("threshold"));
100    }
101
102    #[test]
103    fn error_conversion() {
104        let config_err = ConfigError::Invalid("test".into());
105        let sweeper_err: SweeperError = config_err.into();
106        assert!(matches!(sweeper_err, SweeperError::Config(_)));
107    }
108
109    #[test]
110    fn exit_code_config_error() {
111        let err = SweeperError::Config(ConfigError::Invalid("test".into()));
112        assert_eq!(err.exit_code(), 2);
113    }
114
115    #[test]
116    fn exit_code_permission_denied() {
117        let err = SweeperError::PermissionDenied(PathBuf::from("/secret"));
118        assert_eq!(err.exit_code(), 3);
119    }
120
121    #[test]
122    fn exit_code_path_not_found() {
123        let err = SweeperError::PathNotFound(PathBuf::from("/nonexistent"));
124        assert_eq!(err.exit_code(), 1);
125    }
126
127    #[test]
128    fn exit_code_not_a_directory() {
129        let err = SweeperError::NotADirectory(PathBuf::from("/file.txt"));
130        assert_eq!(err.exit_code(), 1);
131    }
132
133    #[test]
134    fn not_a_directory_error_message() {
135        let err = SweeperError::NotADirectory(PathBuf::from("/some/file"));
136        assert!(err.to_string().contains("Not a directory"));
137        assert!(err.to_string().contains("/some/file"));
138        assert!(err.to_string().contains("Hint"));
139    }
140
141    #[test]
142    fn other_error_message() {
143        let err = SweeperError::Other("something went wrong".into());
144        assert_eq!(err.to_string(), "something went wrong");
145        assert_eq!(err.exit_code(), 1);
146    }
147}