use std::path::PathBuf;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
NotARepository(PathBuf),
WorktreeNotFound(PathBuf),
Io(std::io::Error),
Vcs(processkit::Error),
}
impl Error {
pub fn is_merge_conflict(&self) -> bool {
matches!(self, Error::Vcs(e) if vcs_cli_support::is_merge_conflict(e))
}
pub fn is_nothing_to_commit(&self) -> bool {
matches!(self, Error::Vcs(e) if vcs_cli_support::is_nothing_to_commit(e))
}
pub fn is_transient_fetch_error(&self) -> bool {
matches!(self, Error::Vcs(e) if vcs_cli_support::is_transient_fetch_error(e))
}
pub fn is_transient(&self) -> bool {
matches!(self, Error::Vcs(e) if e.is_transient())
}
pub fn is_not_found(&self) -> bool {
matches!(self, Error::Vcs(e) if e.is_not_found())
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::NotARepository(p) => {
write!(
f,
"no git or jj repository found at or above {}",
p.display()
)
}
Error::WorktreeNotFound(p) => {
write!(f, "no worktree found at {}", p.display())
}
Error::Io(e) => write!(f, "{e}"),
Error::Vcs(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Vcs(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<processkit::Error> for Error {
fn from(e: processkit::Error) -> Self {
Error::Vcs(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_transient_delegates_to_processkit_and_excludes_facade_variants() {
let interrupted = Error::Vcs(processkit::Error::Spawn {
program: "git".into(),
source: std::io::Error::from(std::io::ErrorKind::Interrupted),
});
assert!(interrupted.is_transient());
let missing = Error::Vcs(processkit::Error::Spawn {
program: "git".into(),
source: std::io::Error::from(std::io::ErrorKind::NotFound),
});
assert!(!missing.is_transient());
assert!(!Error::Io(std::io::Error::from(std::io::ErrorKind::Interrupted)).is_transient());
assert!(!Error::NotARepository("/x".into()).is_transient());
}
#[test]
fn is_not_found_only_for_a_missing_binary() {
let not_found = Error::Vcs(processkit::Error::NotFound {
program: "jj".into(),
searched: None,
});
assert!(not_found.is_not_found());
let exit = Error::Vcs(processkit::Error::Exit {
program: "git".into(),
code: 1,
stdout: String::new(),
stderr: "fatal: not a git repository".into(),
});
assert!(!exit.is_not_found());
assert!(!Error::NotARepository("/x".into()).is_not_found());
}
}