dir_assert/
error.rs

1use std::fmt;
2use std::path::PathBuf;
3
4#[derive(Debug, Clone)]
5pub enum Error {
6    /// Error with system IO, unrecoverable, ceasing traverse of current directory
7    Critical(String),
8    /// Extra expected item exists
9    ExtraExpected(PathBuf),
10    /// Extra actual item exists
11    ExtraActual(PathBuf),
12    /// Found filename and directory sharing same name and path
13    InvalidComparison { expected: PathBuf, actual: PathBuf },
14    /// Two files with same path have different contents
15    FileContentsMismatch {
16        expected: PathBuf,
17        actual: PathBuf,
18        line: usize,
19    },
20    /// Top level directories are missing (eg. actual folder wasn't actually created)
21    MissingPath(PathBuf),
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        match self {
27            Error::Critical(msg) => write!(f, "critical error has occurred, {}", msg),
28            Error::ExtraExpected(expected) => write!(
29                f,
30                "found expected file {:?} with no counterpart in actual",
31                expected
32            ),
33            Error::ExtraActual(actual) => write!(
34                f,
35                "found actual file {:?} with no counterpart in expected",
36                actual
37            ),
38            Error::InvalidComparison { expected, actual } => write!(
39                f,
40                "comparing directories and files will not work with {:?} and {:?}",
41                actual, expected
42            ),
43            Error::FileContentsMismatch {
44                actual,
45                expected,
46                line,
47            } => write!(
48                f,
49                "files {:?} and {:?} differ on line {}",
50                actual, expected, line
51            ),
52            Error::MissingPath(path) => write!(f, "path {:?} not found", path),
53        }
54    }
55}
56
57impl Error {
58    pub(crate) fn new_critical<S: Into<String>>(message: S) -> Self {
59        Error::Critical(message.into())
60    }
61
62    pub(crate) fn new_extra_expected<P: Into<PathBuf>>(path: P) -> Self {
63        Error::ExtraExpected(path.into())
64    }
65
66    pub(crate) fn new_extra_actual<P: Into<PathBuf>>(path: P) -> Self {
67        Error::ExtraActual(path.into())
68    }
69
70    pub(crate) fn new_invalid_comparison<PE: Into<PathBuf>, PA: Into<PathBuf>>(
71        expected: PE,
72        actual: PA,
73    ) -> Self {
74        Error::InvalidComparison {
75            expected: expected.into(),
76            actual: actual.into(),
77        }
78    }
79
80    pub(crate) fn new_file_contents_mismatch<PE: Into<PathBuf>, PA: Into<PathBuf>>(
81        expected: PE,
82        actual: PA,
83        line: usize,
84    ) -> Self {
85        Error::FileContentsMismatch {
86            expected: expected.into(),
87            actual: actual.into(),
88            line,
89        }
90    }
91
92    pub(crate) fn new_missing_path<P: Into<PathBuf>>(path: P) -> Self {
93        Error::MissingPath(path.into())
94    }
95}