Skip to content

Commit f5959ed

Browse files
committed
refactor: use specific error type for rev_parse_single()
1 parent 5ca7945 commit f5959ed

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

git-repository/src/repository/revision.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{bstr::BStr, revision, Id};
22

33
/// Methods for resolving revisions by spec or working with the commit graph.
44
impl crate::Repository {
5-
/// Parse a revision specification and turn it into the full id to the object it describes, similar to `git rev-parse`.
5+
/// Parse a revision specification and turn it into the object(s) it describes, similar to `git rev-parse`.
66
pub fn rev_parse<'a>(&self, spec: impl Into<&'a BStr>) -> Result<revision::Spec<'_>, revision::spec::parse::Error> {
77
revision::Spec::from_bstr(
88
spec,
@@ -14,15 +14,15 @@ impl crate::Repository {
1414
)
1515
}
1616

17-
/// Parse a revision specification and return a Result containing the single included object
18-
/// represented by this instance, or `Err` if it is a range of any kind.
17+
/// Parse a revision specification and return single object id as represented by this instance.
1918
pub fn rev_parse_single<'repo, 'a>(
2019
&'repo self,
2120
spec: impl Into<&'a BStr>,
22-
) -> Result<Id<'repo>, revision::spec::parse::Error> {
21+
) -> Result<Id<'repo>, revision::spec::parse::single::Error> {
22+
let spec = spec.into();
2323
self.rev_parse(spec)?
2424
.single()
25-
.ok_or(revision::spec::parse::Error::SingleNotFound)
25+
.ok_or(revision::spec::parse::single::Error::RangedRev { spec: spec.into() })
2626
}
2727

2828
/// Create the baseline for a revision walk by initializing it with the `tips` to start iterating on.

git-repository/src/revision/spec/parse/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ use crate::{bstr::BStr, revision::Spec, Repository};
88
mod types;
99
pub use types::{Error, ObjectKindHint, Options, RefsHint};
1010

11+
///
12+
pub mod single {
13+
use crate::bstr::BString;
14+
15+
/// The error returned by [`crate::Repository::rev_parse_single()`].
16+
#[derive(Debug, thiserror::Error)]
17+
#[allow(missing_docs)]
18+
pub enum Error {
19+
#[error(transparent)]
20+
Parse(#[from] super::Error),
21+
#[error("revspec {spec:?} did not resolve to a single object")]
22+
RangedRev { spec: BString },
23+
}
24+
}
25+
1126
///
1227
pub mod error;
1328

0 commit comments

Comments
 (0)