use std::fmt;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum InstallError {
MissingManifest {
path: PathBuf,
},
MissingLock {
path: PathBuf,
},
InvalidManifest {
path: PathBuf,
detail: String,
},
Unsupported {
reasons: Vec<String>,
},
Vcs {
failure: VcsFailure,
operation: String,
url: String,
detail: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum VcsFailure {
GitMissing,
Auth,
MissingRef,
Unreachable,
Other,
}
impl VcsFailure {
pub fn hint(self) -> &'static str {
match self {
Self::GitMissing => {
"install git and make sure it is on PATH; git sources are installed with the \
system git"
}
Self::Auth => {
"the repository is private or the credentials are wrong; configure a git \
credential helper or ssh key"
}
Self::MissingRef => {
"the locked commit is not on the remote; the lock may point at an unpushed or \
force-pushed commit, so update the lock to re-resolve it"
}
Self::Unreachable => {
"the repository could not be reached; check the URL and your network access"
}
Self::Other => "see git's output above",
}
}
}
impl fmt::Display for InstallError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingManifest { path } => {
write!(f, "{} not found — not a Composer project", path.display())
}
Self::MissingLock { path } => write!(
f,
"{} not found — run `composer update` first to generate it",
path.display()
),
Self::InvalidManifest { path, detail } => {
write!(f, "parsing {}: {detail}", path.display())
}
Self::Unsupported { reasons } => {
write!(
f,
"this lockfile requires features this installer does not handle:"
)?;
for r in reasons {
write!(f, "\n - {r}")?;
}
Ok(())
}
Self::Vcs {
failure,
operation,
url,
detail,
} => {
write!(f, "git {operation} failed")?;
if !url.is_empty() {
write!(f, " for {url}")?;
}
if !detail.is_empty() {
write!(f, ": {detail}")?;
}
write!(f, "\nhint: {}", failure.hint())
}
}
}
}
impl std::error::Error for InstallError {}