Files
sausage/userland/git/vendor/gix-odb/src/traits.rs
T
surma 4b27e9afd5 userland/git: git-compatible CLI on v2's gix code (T10)
Local workflow, smart-HTTP clone/fetch/push and public HTTPS clone/fetch match
native git in stdout and exit codes. Only WASI imports (wasi:http, exit-with-code).
2026-11-06 16:07:57 +00:00

63 lines
1.7 KiB
Rust

use crate::find;
use gix_error::Result;
/// A way to obtain object properties without fully decoding it.
pub trait Header {
/// Try to read the header of the object associated with `id` or return `None` if it could not be found.
fn try_header(&self, id: &gix_hash::oid) -> Result<Option<find::Header>>;
}
mod _impls {
use gix_error::Result;
use std::{ops::Deref, rc::Rc, sync::Arc};
use gix_hash::oid;
use crate::find::Header;
impl<T> crate::Header for &T
where
T: crate::Header,
{
fn try_header(&self, id: &oid) -> Result<Option<Header>> {
(*self).try_header(id)
}
}
impl<T> crate::Header for Rc<T>
where
T: crate::Header,
{
fn try_header(&self, id: &oid) -> Result<Option<Header>> {
self.deref().try_header(id)
}
}
impl<T> crate::Header for Arc<T>
where
T: crate::Header,
{
fn try_header(&self, id: &oid) -> Result<Option<Header>> {
self.deref().try_header(id)
}
}
}
mod ext {
use gix_error::{OptionExt, Result};
use crate::find;
/// An extension trait with convenience functions.
pub trait HeaderExt: super::Header {
/// Like [`try_header(…)`][super::Header::try_header()], but flattens the `Result<Option<_>>` into a single `Result` making a non-existing object an error.
fn header(&self, id: impl AsRef<gix_hash::oid>) -> Result<find::Header> {
let id = id.as_ref();
self.try_header(id)?
.ok_or_raise(|| gix_error::message!("An object with id {id} could not be found").not_found())
}
}
impl<T: super::Header> HeaderExt for T {}
}
pub use ext::HeaderExt;