Skip to main content

heddle_object_model/object/
source.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Read-only object source traits for graph walkers.
3
4use super::{Blob, ContentHash, State, StateId, Tree};
5use crate::error::Result;
6
7/// Read-only object access needed by object graph walkers.
8pub trait ObjectSource {
9    fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
10    fn get_state(&self, id: &StateId) -> Result<Option<State>>;
11    fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
12
13    /// Uncompressed byte length without requiring content.
14    ///
15    /// The default falls back to [`Self::get_blob`]. Stores that can answer
16    /// from a header or index should override this so blame can reject an
17    /// oversized blob before materializing it.
18    fn decoded_blob_len(&self, hash: &ContentHash) -> Result<Option<u64>> {
19        Ok(self.get_blob(hash)?.map(|blob| blob.content().len() as u64))
20    }
21
22    /// Zero-copy variant of `get_blob`.
23    fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
24        Ok(self
25            .get_blob(hash)?
26            .map(|blob| bytes::Bytes::from(blob.into_content())))
27    }
28}
29
30#[cfg(feature = "async-source")]
31#[allow(async_fn_in_trait)]
32pub trait AsyncObjectSource {
33    async fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
34    async fn get_state(&self, id: &StateId) -> Result<Option<State>>;
35    async fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
36}