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