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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! `fix-getters` global level `Error`.

use std::fmt::{self, Display};
use std::{io, path::PathBuf};

/// `fix-getters` global level `Error`.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    CheckEntry(rules::dir_entry::CheckError),
    CreateDir(PathBuf, io::Error),
    ReadDir(PathBuf, io::Error),
    ReadEntry(PathBuf, io::Error),
    ReadFile(PathBuf, io::Error),
    WriteFile(PathBuf, io::Error),
    ParseFile(ParseFileError),
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Error::*;

        match self {
            CheckEntry(err) => err.fmt(f),
            CreateDir(path, err) => write!(f, "Unable to create dir {:?} {}", path, err),
            ReadDir(path, err) => write!(f, "Unable to read dir {:?}: {}", path, err),
            ReadEntry(path, err) => write!(f, "Unable to read dir entry {:?}: {}", path, err),
            ReadFile(path, err) => write!(f, "Unable to read file {:?}: {}", path, err),
            WriteFile(path, err) => write!(f, "Unable to write file {:?}: {}", path, err),
            ParseFile(err) => err.fmt(f),
        }
    }
}

impl std::error::Error for Error {}

impl From<rules::dir_entry::CheckError> for Error {
    fn from(err: rules::dir_entry::CheckError) -> Self {
        Error::CheckEntry(err)
    }
}

/// Rust code parser error wrapper.
#[derive(Debug)]
pub struct ParseFileError {
    error: syn::Error,
    filepath: PathBuf,
    source_code: String,
}

impl ParseFileError {
    pub fn new(error: syn::Error, filepath: PathBuf, source_code: String) -> Self {
        ParseFileError {
            error,
            filepath,
            source_code,
        }
    }
}

impl Display for ParseFileError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "failed to parse file {:?}: {:?}\n\t{}",
            self.filepath, self.error, self.source_code
        )
    }
}

impl std::error::Error for ParseFileError {}

impl From<ParseFileError> for super::Error {
    fn from(err: ParseFileError) -> Self {
        Error::ParseFile(err)
    }
}