idar/
errors.rs

1use ansi_term::Color;
2use std::fmt;
3use std::io;
4use std::path::PathBuf;
5
6#[derive(Debug)]
7pub enum AppError {
8    IoError(io::Error),
9    SerdeError(serde_json::Error),
10    InvalidDirectory(PathBuf),
11    FileNotFound(String),
12    InvalidDeduplicationReport(String),
13}
14
15impl From<io::Error> for AppError {
16    fn from(err: io::Error) -> Self {
17        AppError::IoError(err)
18    }
19}
20
21impl From<serde_json::Error> for AppError {
22    fn from(err: serde_json::Error) -> Self {
23        AppError::SerdeError(err)
24    }
25}
26
27impl fmt::Display for AppError {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        let error_prefix = Color::Red.bold().paint("error:");
30
31        match self {
32            AppError::IoError(err) => write!(f, "{} I/O error: {}", error_prefix, err),
33            AppError::SerdeError(err) => {
34                write!(f, "{} Serialization error: {}", error_prefix, err)
35            }
36            AppError::InvalidDirectory(dir) => {
37                write!(f, "{} Directory `{:?}` does not exist", error_prefix, dir)
38            }
39            AppError::FileNotFound(dir) => {
40                write!(f, "{} File `{}` not found", error_prefix, dir)
41            }
42            AppError::InvalidDeduplicationReport(msg) => {
43                write!(f, "{} Invalid deduplication report: {}", error_prefix, msg)
44            }
45        }
46    }
47}