Skip to main content

vcs_core/
error.rs

1//! The facade's error type: a thin wrapper that adds repo-detection failures on
2//! top of the underlying [`processkit::Error`] the per-tool clients return.
3
4use std::path::PathBuf;
5
6/// An error from a [`Repo`](crate::Repo) operation.
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum Error {
10    /// [`Repo::open`](crate::Repo::open) found no `.git`/`.jj` from the start dir
11    /// up to the filesystem root.
12    NotARepository(PathBuf),
13    /// A worktree/workspace lookup by path matched no attached worktree.
14    WorktreeNotFound(PathBuf),
15    /// A filesystem operation failed (e.g. removing a workspace directory).
16    Io(std::io::Error),
17    /// An underlying `vcs-git` / `vcs-jj` (i.e. `processkit`) error.
18    Vcs(processkit::Error),
19}
20
21impl std::fmt::Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Error::NotARepository(p) => {
25                write!(
26                    f,
27                    "no git or jj repository found at or above {}",
28                    p.display()
29                )
30            }
31            Error::WorktreeNotFound(p) => {
32                write!(f, "no worktree found at {}", p.display())
33            }
34            Error::Io(e) => write!(f, "{e}"),
35            Error::Vcs(e) => write!(f, "{e}"),
36        }
37    }
38}
39
40impl std::error::Error for Error {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        match self {
43            Error::Io(e) => Some(e),
44            Error::Vcs(e) => Some(e),
45            _ => None,
46        }
47    }
48}
49
50impl From<std::io::Error> for Error {
51    fn from(e: std::io::Error) -> Self {
52        Error::Io(e)
53    }
54}
55
56impl From<processkit::Error> for Error {
57    fn from(e: processkit::Error) -> Self {
58        Error::Vcs(e)
59    }
60}
61
62/// `Result` specialised to the facade [`Error`].
63pub type Result<T> = std::result::Result<T, Error>;