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::{deserialize_from_bytes, serialize_to_bytes};
5use junobuild_shared::types::core::{Hash, Hashable};
6use sha2::{Digest, Sha256};
7use std::borrow::Cow;
8
9impl Storable for ProposalAssetKey {
10    fn to_bytes(&self) -> Cow<[u8]> {
11        serialize_to_bytes(self)
12    }
13
14    fn from_bytes(bytes: Cow<[u8]>) -> Self {
15        deserialize_from_bytes(bytes)
16    }
17
18    const BOUND: Bound = Bound::Unbounded;
19}
20
21impl Storable for ProposalContentChunkKey {
22    fn to_bytes(&self) -> Cow<[u8]> {
23        serialize_to_bytes(self)
24    }
25
26    fn from_bytes(bytes: Cow<[u8]>) -> Self {
27        deserialize_from_bytes(bytes)
28    }
29
30    const BOUND: Bound = Bound::Unbounded;
31}
32
33impl Hashable for ProposalAssetKey {
34    fn hash(&self) -> Hash {
35        let mut hasher = Sha256::new();
36
37        hasher.update(self.proposal_id.to_le_bytes());
38        hasher.update(self.collection.to_bytes());
39        hasher.update(self.full_path.to_bytes());
40
41        hasher.finalize().into()
42    }
43}