Skip to main content

junobuild_cdn/storage/state/
impls.rs

1use crate::storage::{ProposalAssetKey, ProposalContentChunkKey};
2use ic_stable_structures::storable::Bound;
3use ic_stable_structures::Storable;
4use junobuild_shared::serializers::{
5    deserialize_from_bytes, serialize_into_bytes, serialize_to_bytes,
6};
7use junobuild_shared::types::core::{Hash, Hashable};
8use sha2::{Digest, Sha256};
9use std::borrow::Cow;
10
11impl Storable for ProposalAssetKey {
12    fn to_bytes(&self) -> Cow<'_, [u8]> {
13        serialize_to_bytes(self)
14    }
15
16    fn into_bytes(self) -> Vec<u8> {
17        serialize_into_bytes(&self)
18    }
19
20    fn from_bytes(bytes: Cow<[u8]>) -> Self {
21        deserialize_from_bytes(bytes)
22    }
23
24    const BOUND: Bound = Bound::Unbounded;
25}
26
27impl Storable for ProposalContentChunkKey {
28    fn to_bytes(&self) -> Cow<'_, [u8]> {
29        serialize_to_bytes(self)
30    }
31
32    fn into_bytes(self) -> Vec<u8> {
33        serialize_into_bytes(&self)
34    }
35
36    fn from_bytes(bytes: Cow<[u8]>) -> Self {
37        deserialize_from_bytes(bytes)
38    }
39
40    const BOUND: Bound = Bound::Unbounded;
41}
42
43impl Hashable for ProposalAssetKey {
44    fn hash(&self) -> Hash {
45        let mut hasher = Sha256::new();
46
47        hasher.update(self.proposal_id.to_le_bytes());
48        hasher.update(self.collection.to_bytes());
49        hasher.update(self.full_path.to_bytes());
50
51        hasher.finalize().into()
52    }
53}