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 StaleSnapshot {
16 expected: u64,
18 actual: u64,
20 },
21}
22
23impl Error {
24 pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
25 Self::Io {
26 path: path.into(),
27 source,
28 }
29 }
30
31 pub(crate) fn concurrent_modification(path: impl Into<PathBuf>) -> Self {
32 Self::ConcurrentModification(path.into())
33 }
34
35 #[must_use]
37 pub const fn stale_snapshot(expected: u64, actual: u64) -> Self {
38 Self::StaleSnapshot { expected, actual }
39 }
40}
41
42impl fmt::Display for Error {
43 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()),
46 Self::InvalidRoot(path) => write!(formatter, "not a directory: {}", path.display()),
47 Self::ConcurrentModification(path) => {
48 write!(formatter, "file changed while scanning: {}", path.display())
49 }
50 Self::StaleSnapshot { expected, actual } => write!(
51 formatter,
52 "scan snapshot generation {expected} is stale; current generation is {actual}"
53 ),
54 }
55 }
56}
57
58impl std::error::Error for Error {
59 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60 match self {
61 Self::Io { source, .. } => Some(source),
62 Self::InvalidRoot(_) | Self::ConcurrentModification(_) | Self::StaleSnapshot { .. } => {
63 None
64 }
65 }
66 }
67}