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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::fmt;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub enum Error {
    /// Error with system IO, unrecoverable, ceasing traverse of current directory
    Critical(String),
    /// Extra expected item exists
    ExtraExpected(PathBuf),
    /// Extra actual item exists
    ExtraActual(PathBuf),
    /// Found filename and directory sharing same name and path
    InvalidComparison { expected: PathBuf, actual: PathBuf },
    /// Two files with same path have different contents
    FileContentsMismatch {
        expected: PathBuf,
        actual: PathBuf,
        line: usize,
    },
    /// Top level directories are missing (eg. actual folder wasn't actually created)
    MissingPath(PathBuf),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Critical(msg) => write!(f, "critical error has occurred, {}", msg),
            Error::ExtraExpected(expected) => write!(
                f,
                "found expected file {:?} with no counterpart in actual",
                expected
            ),
            Error::ExtraActual(actual) => write!(
                f,
                "found actual file {:?} with no counterpart in expected",
                actual
            ),
            Error::InvalidComparison { expected, actual } => write!(
                f,
                "comparing directories and files will not work with {:?} and {:?}",
                actual, expected
            ),
            Error::FileContentsMismatch {
                actual,
                expected,
                line,
            } => write!(
                f,
                "files {:?} and {:?} differ on line {}",
                actual, expected, line
            ),
            Error::MissingPath(path) => write!(f, "path {:?} not found", path),
        }
    }
}

impl Error {
    pub fn new_critical<S: Into<String>>(message: S) -> Self {
        Error::Critical(message.into())
    }

    pub fn new_extra_expected<P: Into<PathBuf>>(path: P) -> Self {
        Error::ExtraExpected(path.into())
    }

    pub fn new_extra_actual<P: Into<PathBuf>>(path: P) -> Self {
        Error::ExtraActual(path.into())
    }

    pub fn new_invalid_comparison<PE: Into<PathBuf>, PA: Into<PathBuf>>(
        expected: PE,
        actual: PA,
    ) -> Self {
        Error::InvalidComparison {
            expected: expected.into(),
            actual: actual.into(),
        }
    }

    pub fn new_file_contents_mismatch<PE: Into<PathBuf>, PA: Into<PathBuf>>(
        expected: PE,
        actual: PA,
        line: usize,
    ) -> Self {
        Error::FileContentsMismatch {
            expected: expected.into(),
            actual: actual.into(),
            line,
        }
    }

    pub fn new_missing_path<P: Into<PathBuf>>(path: P) -> Self {
        Error::MissingPath(path.into())
    }
}