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    /// Zero-copy variant of `get_blob`.
15    fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
16        Ok(self
17            .get_blob(hash)?
18            .map(|blob| bytes::Bytes::from(blob.into_content())))
19    }
20}
21
22#[cfg(feature = "async-source")]
23#[allow(async_fn_in_trait)]
24pub trait AsyncObjectSource {
25    async fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
26    async fn get_state(&self, id: &StateId) -> Result<Option<State>>;
27    async fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
28}