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}
14
15impl Error {
16    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
17        Self::Io {
18            path: path.into(),
19            source,
20        }
21    }
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()),
28            Self::InvalidRoot(path) => write!(formatter, "not a directory: {}", path.display()),
29        }
30    }
31}
32
33impl std::error::Error for Error {
34    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35        match self {
36            Self::Io { source, .. } => Some(source),
37            Self::InvalidRoot(_) => None,
38        }
39    }
40}