git-storage: fallible functions for finding objects

When an object does not exist the `find_*` functions will return
None. It will be a common case that users of the API will follow the
pattern of `find_blob(store, oid)?.ok_or(Error::NotFound)`.

Add fallible functions corresponding to each `find_` function
returning a `git2::Error` if the object is not found.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
Fintan Halpenny
2022-09-27 12:32:20 +01:00
parent 5898c0ffde
commit ee7a9029ce
+64
View File
@@ -31,3 +31,67 @@ pub use read::Read;
pub mod write;
pub use write::Write;
/// Find the [`Object`] corresponding to the given `oid`.
///
/// Will fail if the object does not exist.
pub fn object<S>(storage: &S, oid: Oid) -> Result<Object, S::FindObj>
where
S: Read<FindObj = git2::Error>,
{
storage
.find_object(oid)?
.ok_or_else(|| not_found("object", oid))
}
/// Find the [`Blob`] corresponding to the given `oid`.
///
/// Will fail if the blob does not exist.
pub fn blob<S>(storage: &S, oid: Oid) -> Result<Blob, S::FindBlob>
where
S: Read<FindBlob = git2::Error>,
{
storage
.find_blob(oid)?
.ok_or_else(|| not_found("blob", oid))
}
/// Find the [`Commit`] corresponding to the given `oid`.
///
/// Will fail if the commit does not exist.
pub fn commit<S>(storage: &S, oid: Oid) -> Result<Commit, S::FindCommit>
where
S: Read<FindCommit = git2::Error>,
{
storage
.find_commit(oid)?
.ok_or_else(|| not_found("commit", oid))
}
/// Find the [`Tag`] corresponding to the given `oid`.
///
/// Will fail if the tag does not exist.
pub fn tag<S>(storage: &S, oid: Oid) -> Result<Tag, S::FindTag>
where
S: Read<FindTag = git2::Error>,
{
storage.find_tag(oid)?.ok_or_else(|| not_found("tag", oid))
}
/// Find the [`Tree`] corresponding to the given `oid`.
///
/// Will fail if the tree does not exist.
pub fn tree<S>(storage: &S, oid: Oid) -> Result<Tree, S::FindTree>
where
S: Read<FindTree = git2::Error>,
{
storage
.find_tree(oid)?
.ok_or_else(|| not_found("tree", oid))
}
fn not_found(kind: &str, oid: Oid) -> git2::Error {
use git2::{ErrorClass::*, ErrorCode::*};
git2::Error::new(NotFound, Object, format!("could not find {kind} {oid}"))
}