noosphere_storage/
ucan.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use cid::Cid;
4use core::fmt::Debug;
5use libipld_core::{
6    codec::{Decode, Encode},
7    raw::RawCodec,
8};
9use ucan::store::{UcanStore as UcanStoreTrait, UcanStoreConditionalSend};
10
11use crate::block::BlockStore;
12
13pub struct UcanStore<S: BlockStore>(pub S);
14
15#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
16#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
17impl<S: BlockStore> UcanStoreTrait<RawCodec> for UcanStore<S> {
18    async fn read<T: Decode<RawCodec>>(&self, cid: &Cid) -> Result<Option<T>> {
19        self.0.get::<RawCodec, T>(cid).await
20    }
21
22    async fn write<T: Encode<RawCodec> + UcanStoreConditionalSend + Debug>(
23        &mut self,
24        token: T,
25    ) -> Result<Cid> {
26        self.0.put::<RawCodec, T>(token).await
27    }
28}
29
30impl<S: BlockStore + Clone> Clone for UcanStore<S> {
31    fn clone(&self) -> Self {
32        UcanStore(self.0.clone())
33    }
34}