junobuild_cdn/proposals/state/
stable.rs

1use crate::proposals::state::filter::filter_proposals_range;
2use crate::proposals::{ListProposalResults, Proposal, ProposalId, ProposalKey, ProposalsStable};
3use crate::strategies::CdnStableStrategy;
4
5pub fn get_proposal(
6    cdn_stable: &impl CdnStableStrategy,
7    proposal_id: &ProposalId,
8) -> Option<Proposal> {
9    cdn_stable.with_proposals(|proposals| get_proposal_impl(proposal_id, proposals))
10}
11
12fn get_proposal_impl(proposal_id: &ProposalId, proposals: &ProposalsStable) -> Option<Proposal> {
13    proposals.get(&proposal_key(proposal_id))
14}
15
16pub fn list_proposals(
17    cdn_stable: &impl CdnStableStrategy,
18    filters_key: &(u128, u128),
19) -> ListProposalResults {
20    cdn_stable.with_proposals(|proposals| list_proposals_impl(filters_key, proposals))
21}
22
23fn list_proposals_impl(
24    filters_key: &(u128, u128),
25    proposals: &ProposalsStable,
26) -> ListProposalResults {
27    let items: Vec<(ProposalKey, Proposal)> = proposals
28        .range(filter_proposals_range(filters_key))
29        .collect();
30
31    let items_length = items.len();
32
33    ListProposalResults {
34        items,
35        items_length,
36        matches_length: items_length,
37    }
38}
39
40pub fn count_proposals(cdn_stable: &impl CdnStableStrategy) -> usize {
41    cdn_stable.with_proposals(count_proposals_impl)
42}
43
44fn count_proposals_impl(proposals: &ProposalsStable) -> usize {
45    proposals.iter().count()
46}
47
48pub fn insert_proposal(
49    cdn_stable: &impl CdnStableStrategy,
50    proposal_id: &ProposalId,
51    proposal: &Proposal,
52) {
53    cdn_stable
54        .with_proposals_mut(|proposals| insert_proposal_impl(proposal_id, proposal, proposals))
55}
56
57fn insert_proposal_impl(
58    proposal_id: &ProposalId,
59    proposal: &Proposal,
60    proposals: &mut ProposalsStable,
61) {
62    proposals.insert(proposal_key(proposal_id), proposal.clone());
63}
64
65fn proposal_key(proposal_id: &ProposalId) -> ProposalKey {
66    ProposalKey {
67        proposal_id: *proposal_id,
68    }
69}