file_matcher/
error.rs

1use crate::OneEntry;
2use std::error;
3use std::ffi::OsString;
4use std::fmt;
5use std::path::PathBuf;
6
7pub type Result<T> = core::result::Result<T, FileMatcherError>;
8
9#[derive(Debug)]
10pub enum FileMatcherError {
11    TooMany(OneEntry),
12    NotExists(OneEntry),
13    NotReadable(PathBuf),
14    InvalidUnicode(OsString),
15    IoError(std::io::Error),
16    #[cfg(feature = "fs_extra")]
17    FsExtraError(fs_extra::error::Error),
18    #[cfg(feature = "regex")]
19    RegexError(regex::Error),
20}
21
22impl fmt::Display for FileMatcherError {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self {
25            FileMatcherError::TooMany(entry) => {
26                write!(
27                    f,
28                    "Found more than one {:?} named {:?} in {:?}",
29                    entry.entry_type(),
30                    entry.entry_name(),
31                    entry.directory()
32                )
33            }
34            FileMatcherError::NotExists(entry) => {
35                write!(
36                    f,
37                    "Could not find {:?} named {:?} in {:?}",
38                    entry.entry_type(),
39                    entry.entry_name(),
40                    entry.directory()
41                )
42            }
43            FileMatcherError::IoError(error) => {
44                write!(f, "Failed to perform IO operation {:?}", error)
45            }
46            #[cfg(feature = "fs_extra")]
47            FileMatcherError::FsExtraError(error) => {
48                write!(f, "Failed to perform IO operation {:?}", error)
49            }
50            #[cfg(feature = "regex")]
51            FileMatcherError::RegexError(error) => {
52                write!(f, "Failed to create regex {:?}", error)
53            }
54            FileMatcherError::NotReadable(path) => {
55                write!(f, "Failed to read {:?}", path)
56            }
57            FileMatcherError::InvalidUnicode(file_name) => {
58                write!(f, "Failed to convert {:?} to Unicode", file_name)
59            }
60        }
61    }
62}
63
64impl error::Error for FileMatcherError {
65    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
66        match *self {
67            FileMatcherError::IoError(ref e) => Some(e),
68            #[cfg(feature = "regex")]
69            FileMatcherError::RegexError(ref e) => Some(e),
70            _ => None,
71        }
72    }
73}
74
75impl From<regex::Error> for FileMatcherError {
76    fn from(err: regex::Error) -> FileMatcherError {
77        FileMatcherError::RegexError(err)
78    }
79}
80
81impl From<std::io::Error> for FileMatcherError {
82    fn from(err: std::io::Error) -> FileMatcherError {
83        FileMatcherError::IoError(err)
84    }
85}
86
87impl<T> From<FileMatcherError> for std::result::Result<T, FileMatcherError> {
88    fn from(error: FileMatcherError) -> Self {
89        Err(error)
90    }
91}
92
93unsafe impl Sync for FileMatcherError {}
94unsafe impl Send for FileMatcherError {}