1use crate::{
5 object::{Blob, ChangeId, ContentHash, State, Tree},
6 store::ObjectStore,
7};
8
9use super::Result;
10
11pub trait ObjectSource {
13 fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
14 fn get_state(&self, id: &ChangeId) -> Result<Option<State>>;
15 fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
16
17 fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
19 Ok(self
20 .get_blob(hash)?
21 .map(|blob| bytes::Bytes::from(blob.into_content())))
22 }
23}
24
25impl<S: ObjectStore + ?Sized> ObjectSource for S {
26 #[inline]
27 fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>> {
28 ObjectStore::get_tree(self, hash)
29 }
30
31 #[inline]
32 fn get_state(&self, id: &ChangeId) -> Result<Option<State>> {
33 ObjectStore::get_state(self, id)
34 }
35
36 #[inline]
37 fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>> {
38 ObjectStore::get_blob(self, hash)
39 }
40
41 #[inline]
42 fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
43 ObjectStore::get_blob_bytes(self, hash)
44 }
45}
46
47#[cfg(feature = "async-source")]
48#[allow(async_fn_in_trait)]
49pub trait AsyncObjectSource {
50 async fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
51 async fn get_state(&self, id: &ChangeId) -> Result<Option<State>>;
52 async fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
53}