Skip to main content

vcs_watch/
error.rs

1//! The crate's error type: filesystem-watcher setup failures plus the underlying
2//! `vcs-core` re-query errors.
3
4/// An error from setting up or running a [`RepoWatcher`](crate::RepoWatcher).
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum Error {
8    /// The `notify` filesystem watcher failed to start or register a path.
9    Notify(notify::Error),
10    /// A `vcs-core` query (detection / `snapshot` / `local_branches`) failed —
11    /// chiefly while *building* the watcher (capturing the baseline state). A
12    /// re-query failure *during* watching is skipped and retried, not surfaced
13    /// here (see [`RepoWatcher`](crate::RepoWatcher)).
14    Vcs(vcs_core::Error),
15    /// A filesystem operation failed (e.g. resolving a worktree gitlink).
16    Io(std::io::Error),
17}
18
19impl std::fmt::Display for Error {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Error::Notify(e) => write!(f, "filesystem watch failed: {e}"),
23            Error::Vcs(e) => write!(f, "{e}"),
24            Error::Io(e) => write!(f, "{e}"),
25        }
26    }
27}
28
29impl std::error::Error for Error {
30    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31        match self {
32            Error::Notify(e) => Some(e),
33            Error::Vcs(e) => Some(e),
34            Error::Io(e) => Some(e),
35        }
36    }
37}
38
39impl From<notify::Error> for Error {
40    fn from(e: notify::Error) -> Self {
41        Error::Notify(e)
42    }
43}
44
45impl From<vcs_core::Error> for Error {
46    fn from(e: vcs_core::Error) -> Self {
47        Error::Vcs(e)
48    }
49}
50
51impl From<std::io::Error> for Error {
52    fn from(e: std::io::Error) -> Self {
53        Error::Io(e)
54    }
55}
56
57/// `Result` specialised to the watcher [`Error`].
58pub type Result<T> = std::result::Result<T, Error>;