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))
}
}
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>;