1use std::fmt;
5use std::io;
6use std::path::PathBuf;
7
8#[derive(Debug)]
11#[non_exhaustive]
12pub enum HashUtilityError {
13 FileNotFound {
15 path: PathBuf,
17 },
18 DirectoryNotFound {
20 path: PathBuf,
22 },
23 PermissionDenied {
25 path: PathBuf,
27 operation: String,
29 },
30 IoError {
32 path: Option<PathBuf>,
34 operation: String,
36 source: io::Error,
38 },
39
40 UnsupportedAlgorithm {
42 algorithm: String,
44 },
45 AlgorithmUnavailable {
47 algorithm: String,
49 feature: &'static str,
51 },
52 InvalidDigest {
54 algorithm: String,
56 reason: String,
58 },
59 HashComputationFailed {
61 path: PathBuf,
63 algorithm: String,
65 reason: String,
67 },
68
69 Cancelled,
71
72 DatabaseNotFound {
74 path: PathBuf,
76 },
77 DatabaseParseError {
79 path: PathBuf,
81 line: usize,
83 reason: String,
85 },
86 DatabaseWriteError {
88 path: PathBuf,
90 reason: String,
92 },
93 EmptyDatabase {
95 path: PathBuf,
97 },
98
99 VerificationFailed {
101 reason: String,
103 },
104
105 InvalidArguments {
107 message: String,
109 },
110 MissingRequiredArgument {
112 argument: String,
114 },
115
116 BenchmarkFailed {
118 algorithm: String,
120 reason: String,
122 },
123}
124
125impl fmt::Display for HashUtilityError {
126 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127 match self {
128 HashUtilityError::FileNotFound { path } => {
130 writeln!(f, "File not found: {}", path.display())?;
131 write!(
132 f,
133 "Suggestion: Check that the file path is correct and the file exists"
134 )
135 }
136 HashUtilityError::DirectoryNotFound { path } => {
137 writeln!(f, "Directory not found: {}", path.display())?;
138 write!(
139 f,
140 "Suggestion: Check that the directory path is correct and the directory exists"
141 )
142 }
143 HashUtilityError::PermissionDenied { path, operation } => {
144 writeln!(
145 f,
146 "Permission denied while {} file: {}",
147 operation,
148 path.display()
149 )?;
150 write!(
151 f,
152 "Suggestion: Check file permissions or run with appropriate privileges"
153 )
154 }
155 HashUtilityError::IoError {
156 path,
157 operation,
158 source,
159 } => {
160 if let Some(p) = path {
161 writeln!(
162 f,
163 "I/O error while {} file {}: {}",
164 operation,
165 p.display(),
166 source
167 )?;
168 } else {
169 writeln!(f, "I/O error while {}: {}", operation, source)?;
170 }
171 write!(f, "Suggestion: Check file permissions and disk space")
172 }
173
174 HashUtilityError::UnsupportedAlgorithm { algorithm } => {
176 writeln!(f, "Unsupported hash algorithm: {}", algorithm)?;
177 write!(f, "Suggestion: Use --list to see available algorithms")
178 }
179 HashUtilityError::AlgorithmUnavailable { algorithm, feature } => {
180 writeln!(f, "Hash algorithm '{}' is not compiled in", algorithm)?;
181 write!(f, "Suggestion: Enable the '{}' Cargo feature", feature)
182 }
183 HashUtilityError::InvalidDigest { algorithm, reason } => {
184 write!(f, "Invalid {} digest: {}", algorithm, reason)
185 }
186 HashUtilityError::HashComputationFailed {
187 path,
188 algorithm,
189 reason,
190 } => {
191 writeln!(
192 f,
193 "Failed to compute {} hash for {}: {}",
194 algorithm,
195 path.display(),
196 reason
197 )?;
198 write!(
199 f,
200 "Suggestion: Check that the file is readable and not corrupted"
201 )
202 }
203 HashUtilityError::Cancelled => write!(f, "Operation cancelled"),
204
205 HashUtilityError::DatabaseNotFound { path } => {
207 writeln!(f, "Hash database file not found: {}", path.display())?;
208 write!(
209 f,
210 "Suggestion: Create a database first using the 'scan' command"
211 )
212 }
213 HashUtilityError::DatabaseParseError { path, line, reason } => {
214 writeln!(
215 f,
216 "Error parsing database {} at line {}: {}",
217 path.display(),
218 line,
219 reason
220 )?;
221 write!(
222 f,
223 "Suggestion: Check that the database file format is correct (hash filepath)"
224 )
225 }
226 HashUtilityError::DatabaseWriteError { path, reason } => {
227 writeln!(
228 f,
229 "Failed to write to database {}: {}",
230 path.display(),
231 reason
232 )?;
233 write!(f, "Suggestion: Check disk space and write permissions")
234 }
235 HashUtilityError::EmptyDatabase { path } => {
236 writeln!(f, "Database file is empty: {}", path.display())?;
237 write!(
238 f,
239 "Suggestion: Ensure the database contains at least one hash entry"
240 )
241 }
242
243 HashUtilityError::VerificationFailed { reason } => {
245 writeln!(f, "Verification failed: {}", reason)?;
246 write!(
247 f,
248 "Suggestion: Check that the database and directory paths are correct"
249 )
250 }
251
252 HashUtilityError::InvalidArguments { message } => {
254 writeln!(f, "Invalid arguments: {}", message)?;
255 write!(f, "Suggestion: Run with --help to see usage information")
256 }
257 HashUtilityError::MissingRequiredArgument { argument } => {
258 writeln!(f, "Missing required argument: {}", argument)?;
259 write!(f, "Suggestion: Run with --help to see required arguments")
260 }
261
262 HashUtilityError::BenchmarkFailed { algorithm, reason } => {
264 writeln!(f, "Benchmark failed for {}: {}", algorithm, reason)?;
265 write!(
266 f,
267 "Suggestion: Try running the benchmark again or with a smaller data size"
268 )
269 }
270 }
271 }
272}
273
274impl std::error::Error for HashUtilityError {
275 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
276 match self {
277 HashUtilityError::IoError { source, .. } => Some(source),
278 _ => None,
279 }
280 }
281}
282
283impl HashUtilityError {
285 pub fn from_io_error(err: io::Error, operation: &str, path: Option<PathBuf>) -> Self {
290 match err.kind() {
292 io::ErrorKind::NotFound => {
293 if let Some(p) = path {
294 if operation.contains("directory") || operation.contains("scan") {
295 HashUtilityError::DirectoryNotFound { path: p }
296 } else {
297 HashUtilityError::FileNotFound { path: p }
298 }
299 } else {
300 HashUtilityError::IoError {
301 path: None,
302 operation: operation.to_string(),
303 source: err,
304 }
305 }
306 }
307 io::ErrorKind::PermissionDenied => {
308 if let Some(p) = path {
309 HashUtilityError::PermissionDenied {
310 path: p,
311 operation: operation.to_string(),
312 }
313 } else {
314 HashUtilityError::IoError {
315 path: None,
316 operation: operation.to_string(),
317 source: err,
318 }
319 }
320 }
321 _ => HashUtilityError::IoError {
322 path,
323 operation: operation.to_string(),
324 source: err,
325 },
326 }
327 }
328}
329
330impl From<io::Error> for HashUtilityError {
332 fn from(err: io::Error) -> Self {
333 HashUtilityError::from_io_error(err, "unknown operation", None)
334 }
335}