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
use crate::OneEntry;
use std::error;
use std::ffi::OsString;
use std::fmt;
use std::path::PathBuf;

pub type Result<T> = core::result::Result<T, FileMatcherError>;

#[derive(Debug)]
pub enum FileMatcherError {
    TooMany(OneEntry),
    NotExists(OneEntry),
    NotReadable(PathBuf),
    InvalidUnicode(OsString),
    IoError(std::io::Error),
    #[cfg(feature = "fs_extra")]
    FsExtraError(fs_extra::error::Error),
    #[cfg(feature = "regex")]
    RegexError(regex::Error),
}

impl fmt::Display for FileMatcherError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            FileMatcherError::TooMany(entry) => {
                write!(
                    f,
                    "Found more than one {:?} named {:?} in {:?}",
                    entry.entry_type(),
                    entry.entry_name(),
                    entry.directory()
                )
            }
            FileMatcherError::NotExists(entry) => {
                write!(
                    f,
                    "Could not find {:?} named {:?} in {:?}",
                    entry.entry_type(),
                    entry.entry_name(),
                    entry.directory()
                )
            }
            FileMatcherError::IoError(error) => {
                write!(f, "Failed to perform IO operation {:?}", error)
            }
            #[cfg(feature = "fs_extra")]
            FileMatcherError::FsExtraError(error) => {
                write!(f, "Failed to perform IO operation {:?}", error)
            }
            #[cfg(feature = "regex")]
            FileMatcherError::RegexError(error) => {
                write!(f, "Failed to create regex {:?}", error)
            }
            FileMatcherError::NotReadable(path) => {
                write!(f, "Failed to read {:?}", path)
            }
            FileMatcherError::InvalidUnicode(file_name) => {
                write!(f, "Failed to convert {:?} to Unicode", file_name)
            }
        }
    }
}

impl error::Error for FileMatcherError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            FileMatcherError::IoError(ref e) => Some(e),
            #[cfg(feature = "regex")]
            FileMatcherError::RegexError(ref e) => Some(e),
            _ => None,
        }
    }
}

impl From<regex::Error> for FileMatcherError {
    fn from(err: regex::Error) -> FileMatcherError {
        FileMatcherError::RegexError(err)
    }
}

impl From<std::io::Error> for FileMatcherError {
    fn from(err: std::io::Error) -> FileMatcherError {
        FileMatcherError::IoError(err)
    }
}

impl<T> From<FileMatcherError> for std::result::Result<T, FileMatcherError> {
    fn from(error: FileMatcherError) -> Self {
        Err(error)
    }
}

unsafe impl Sync for FileMatcherError {}
unsafe impl Send for FileMatcherError {}