void_core/store/
traits.rs1use crate::cid::VoidCid;
4use crate::Result;
5use void_crypto::EncryptedBlob;
6
7pub(crate) trait ObjectStore {
12 fn write_raw(&self, data: &[u8]) -> Result<VoidCid>;
14
15 fn read_raw(&self, cid: &VoidCid) -> Result<Vec<u8>>;
17
18 fn has(&self, cid: &VoidCid) -> Result<bool>;
20
21 fn delete(&self, cid: &VoidCid) -> Result<()>;
23}
24
25pub trait ObjectStoreExt {
30 fn put_blob<B: EncryptedBlob>(&self, blob: &B) -> Result<VoidCid>;
32
33 fn get_blob<B: EncryptedBlob>(&self, cid: &VoidCid) -> Result<B>;
38
39 fn get_blob_verified<B: EncryptedBlob>(&self, cid: &VoidCid) -> Result<B>;
45
46 fn exists(&self, cid: &VoidCid) -> Result<bool>;
48
49 fn remove(&self, cid: &VoidCid) -> Result<()>;
51}
52
53impl<T: ObjectStore + ?Sized> ObjectStoreExt for T {
55 fn put_blob<B: EncryptedBlob>(&self, blob: &B) -> Result<VoidCid> {
56 self.write_raw(blob.as_bytes())
57 }
58
59 fn get_blob<B: EncryptedBlob>(&self, cid: &VoidCid) -> Result<B> {
60 self.read_raw(cid).map(B::from_bytes)
61 }
62
63 fn get_blob_verified<B: EncryptedBlob>(&self, cid: &VoidCid) -> Result<B> {
64 let data = self.read_raw(cid)?;
65 let computed = VoidCid::create(&data);
66 if computed != *cid {
67 return Err(crate::VoidError::IntegrityError {
68 expected: cid.to_string(),
69 actual: computed.to_string(),
70 });
71 }
72 Ok(B::from_bytes(data))
73 }
74
75 fn exists(&self, cid: &VoidCid) -> Result<bool> {
76 self.has(cid)
77 }
78
79 fn remove(&self, cid: &VoidCid) -> Result<()> {
80 self.delete(cid)
81 }
82}