use thiserror::Error;
use super::RefLockError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RefError {
#[error("invalid ref: {detail}")]
Invalid {
detail: String,
},
#[error("unexpected line in {path}: {line}")]
PackedRefsUnexpectedLine {
path: String,
line: String,
},
#[error(transparent)]
Lock(#[from] RefLockError),
#[error(transparent)]
Store(crate::refs::store::RefStoreError),
#[error(transparent)]
Reftable(#[from] crate::reftable::ReftableError),
#[error("HEAD is detached")]
DetachedHead,
#[error("no commits yet on this branch")]
UnbornHead,
#[error("HEAD is in an unknown state")]
InvalidHead,
#[error("HEAD does not point at branch '{branch}'")]
HeadNotOnBranch {
branch: String,
},
#[error("HEAD points at a branch, not a detached commit")]
HeadNotDetached,
#[error("ref lock for '{refname}' was already released")]
LockReleased {
refname: String,
},
#[error("ref query requires an object database")]
QueryNeedsObjectDatabase,
#[error("object {oid} is not a commit")]
QueryTargetNotCommit {
oid: crate::objects::ObjectId,
},
#[error("unknown ref '{refname}'")]
ReflogUnknownRef {
refname: String,
},
#[error("expected ref@{{n}} form, got '{spec}'")]
ReflogInvalidSelector {
spec: String,
},
#[error("invalid reflog index in '{spec}'")]
ReflogInvalidIndex {
spec: String,
#[source]
source: std::num::ParseIntError,
},
#[error("invalid reflog expiry: {value:?}")]
InvalidReflogExpiry {
value: String,
},
#[error("reflog entry {refname}@{{{index}}} not found")]
ReflogIndexOutOfRange {
refname: String,
index: usize,
},
}
impl RefError {
#[must_use]
pub fn kind_name(&self) -> &'static str {
match self {
Self::Invalid { .. } => "invalid_ref",
Self::PackedRefsUnexpectedLine { .. } => "packed_refs_unexpected_line",
Self::Lock(..) => "ref_lock",
Self::Store(..) => "ref_store",
Self::Reftable(..) => "reftable",
Self::DetachedHead => "detached_head",
Self::UnbornHead => "unborn_head",
Self::InvalidHead => "invalid_head",
Self::HeadNotOnBranch { .. } => "head_not_on_branch",
Self::HeadNotDetached => "head_not_detached",
Self::LockReleased { .. } => "ref_lock_released",
Self::QueryNeedsObjectDatabase => "ref_query_needs_object_database",
Self::QueryTargetNotCommit { .. } => "ref_query_target_not_commit",
Self::ReflogUnknownRef { .. } => "reflog_unknown_ref",
Self::ReflogInvalidSelector { .. } => "reflog_invalid_selector",
Self::ReflogInvalidIndex { .. } => "reflog_invalid_index",
Self::ReflogIndexOutOfRange { .. } => "reflog_index_out_of_range",
Self::InvalidReflogExpiry { .. } => "invalid_reflog_expiry",
}
}
}