Skip to main content

weavatrix_scan/
error.rs

1use std::fmt;
2use std::path::PathBuf;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug)]
7pub enum Error {
8    Io {
9        path: PathBuf,
10        source: std::io::Error,
11    },
12    InvalidRoot(PathBuf),
13    ConcurrentModification(PathBuf),
14}
15
16impl Error {
17    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
18        Self::Io {
19            path: path.into(),
20            source,
21        }
22    }
23
24    pub(crate) fn concurrent_modification(path: impl Into<PathBuf>) -> Self {
25        Self::ConcurrentModification(path.into())
26    }
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()),
33            Self::InvalidRoot(path) => write!(formatter, "not a directory: {}", path.display()),
34            Self::ConcurrentModification(path) => {
35                write!(formatter, "file changed while scanning: {}", path.display())
36            }
37        }
38    }
39}
40
41impl std::error::Error for Error {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            Self::Io { source, .. } => Some(source),
45            Self::InvalidRoot(_) | Self::ConcurrentModification(_) => None,
46        }
47    }
48}