fix_getters_utils/
error.rs

1//! `fix-getters` global level `Error`.
2
3use std::fmt::{self, Display};
4use std::{io, path::PathBuf};
5
6/// `fix-getters` global level `Error`.
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum Error {
10    CheckEntry(rules::dir_entry::CheckError),
11    CreateDir(PathBuf, io::Error),
12    ReadDir(PathBuf, io::Error),
13    ReadEntry(PathBuf, io::Error),
14    ReadFile(PathBuf, io::Error),
15    WriteFile(PathBuf, io::Error),
16    ParseFile(ParseFileError),
17}
18
19impl Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        use self::Error::*;
22
23        match self {
24            CheckEntry(err) => err.fmt(f),
25            CreateDir(path, err) => write!(f, "Unable to create dir {:?} {}", path, err),
26            ReadDir(path, err) => write!(f, "Unable to read dir {:?}: {}", path, err),
27            ReadEntry(path, err) => write!(f, "Unable to read dir entry {:?}: {}", path, err),
28            ReadFile(path, err) => write!(f, "Unable to read file {:?}: {}", path, err),
29            WriteFile(path, err) => write!(f, "Unable to write file {:?}: {}", path, err),
30            ParseFile(err) => err.fmt(f),
31        }
32    }
33}
34
35impl std::error::Error for Error {}
36
37impl From<rules::dir_entry::CheckError> for Error {
38    fn from(err: rules::dir_entry::CheckError) -> Self {
39        Error::CheckEntry(err)
40    }
41}
42
43/// Rust code parser error wrapper.
44#[derive(Debug)]
45pub struct ParseFileError {
46    error: syn::Error,
47    filepath: PathBuf,
48    source_code: String,
49}
50
51impl ParseFileError {
52    pub fn new(error: syn::Error, filepath: PathBuf, source_code: String) -> Self {
53        ParseFileError {
54            error,
55            filepath,
56            source_code,
57        }
58    }
59}
60
61impl Display for ParseFileError {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        write!(
64            f,
65            "failed to parse file {:?}: {:?}\n\t{}",
66            self.filepath, self.error, self.source_code
67        )
68    }
69}
70
71impl std::error::Error for ParseFileError {}
72
73impl From<ParseFileError> for super::Error {
74    fn from(err: ParseFileError) -> Self {
75        Error::ParseFile(err)
76    }
77}