summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: e757cf4ff7194ae096312698ef0171ada7dbd1a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::{
    fmt::{self, Write},
    path::PathBuf,
};

#[derive(thiserror::Error)]
pub enum Error {
    #[error("invalid regex {0}")]
    Regex(#[from] regex::Error),
    #[error(transparent)]
    File(#[from] std::io::Error),
    #[error("failed to move file: {0}")]
    TempfilePersist(#[from] tempfile::PersistError),
    #[error("file doesn't have parent path: {0}")]
    InvalidPath(PathBuf),
    #[error("failed processing files:\n{0}")]
    FailedProcessing(FailedJobs),
}

pub struct FailedJobs(Vec<(PathBuf, Error)>);

impl From<Vec<(PathBuf, Error)>> for FailedJobs {
    fn from(vec: Vec<(PathBuf, Error)>) -> Self {
        Self(vec)
    }
}

impl fmt::Display for FailedJobs {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("\tFailedJobs(\n")?;
        for (path, err) in &self.0 {
            f.write_str(&format!("\t{:?}: {}\n", path, err))?;
        }
        f.write_char(')')
    }
}

// pretty-print the error
impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;