use std::{io, process::ExitStatus};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GitListError {
#[error("`{program}` is not installed or not on PATH")]
ProgramNotFound {
program: String,
},
#[error("running `{command}`")]
Spawn {
command: String,
#[source]
source: io::Error,
},
#[error("`{command}` failed with {status}")]
Failed {
command: String,
status: ExitStatus,
stderr: String,
},
#[error("`{command}` did not report a usable Git directory")]
NoGitDir {
command: String,
},
}
impl GitListError {
#[must_use]
pub fn diagnostic(&self) -> String {
match self {
Self::Spawn { source, .. } => format!("{self}: {source}"),
Self::Failed { stderr, .. } if !stderr.is_empty() => format!("{self}: {stderr}"),
_ => self.to_string(),
}
}
#[must_use]
pub const fn category(&self) -> &'static str {
match self {
Self::ProgramNotFound { .. } => "program_not_found",
Self::Spawn { .. } => "spawn",
Self::Failed { .. } => "nonzero_exit",
Self::NoGitDir { .. } => "no_git_dir",
}
}
}
#[cfg(test)]
#[path = "git_failure_tests.rs"]
mod tests;