junobuild_collections/
types.rs

1pub mod core {
2    /// Represents the key or identifier of a collection.
3    ///
4    /// This type, `CollectionKey`, is an alias for `String`, used to represent the key or identifier of a collection
5    /// within the context of various data structures and operations.
6    ///
7    /// `CollectionKey` is commonly employed as a unique identifier for collections in Rust code.
8    pub type CollectionKey = String;
9}
10
11pub mod rules {
12    use crate::types::core::CollectionKey;
13    use candid::CandidType;
14    use junobuild_shared::rate::types::RateConfig;
15    use junobuild_shared::serializers::deserialize_default_as_true;
16    use junobuild_shared::types::state::Timestamp;
17    use junobuild_shared::types::state::Version;
18    use serde::{Deserialize, Serialize};
19    use std::collections::HashMap;
20
21    pub type Rules = HashMap<CollectionKey, Rule>;
22
23    #[derive(CandidType, Serialize, Deserialize, Clone)]
24    pub struct Rule {
25        pub read: Permission,
26        pub write: Permission,
27        #[serde(default = "deserialize_default_as_true")]
28        pub mutable_permissions: Option<bool>,
29        pub memory: Option<Memory>,
30        pub max_size: Option<u128>,
31        pub max_capacity: Option<u32>,
32        pub max_changes_per_user: Option<u32>,
33        pub created_at: Timestamp,
34        pub updated_at: Timestamp,
35        pub version: Option<Version>,
36        pub rate_config: Option<RateConfig>,
37    }
38
39    #[derive(CandidType, Serialize, Deserialize, Default, Clone, PartialEq)]
40    pub enum Memory {
41        // Backwards compatibility. Version of the Satellite <= v0.0.11 had no memory information and we originally introduced the option with Heap as default.
42        // If we set Stable as default, the state won't resolve the information correctly given that we use memory.clone().unwrap_or_default() to select which type of memory to read in the Datastore.
43        // We can potentially migrate the collections but, keeping it that way is a pragmatic solution given that even if set as optional, a rule must be set when creating a new collection.
44        #[default]
45        Heap,
46        Stable,
47    }
48
49    #[derive(CandidType, Serialize, Deserialize, Clone, PartialEq)]
50    pub enum Permission {
51        // No rules applied
52        Public,
53        // The one - and only the one - that created the document can rule it
54        Private,
55        // The one that created the document and the controllers can rule the document
56        Managed,
57        // The controllers - and only these - can rule the document
58        Controllers,
59    }
60}
61
62pub mod interface {
63    use crate::types::core::CollectionKey;
64    use crate::types::rules::{Memory, Permission, Rule};
65    use candid::CandidType;
66    use junobuild_shared::rate::types::RateConfig;
67    use junobuild_shared::types::state::Version;
68    use serde::Deserialize;
69
70    #[derive(CandidType, Deserialize, Clone)]
71    pub struct SetRule {
72        pub read: Permission,
73        pub write: Permission,
74        pub mutable_permissions: Option<bool>,
75        pub memory: Option<Memory>,
76        pub max_size: Option<u128>,
77        pub max_capacity: Option<u32>,
78        pub max_changes_per_user: Option<u32>,
79        pub version: Option<Version>,
80        pub rate_config: Option<RateConfig>,
81    }
82
83    #[derive(Default, CandidType, Deserialize, Clone)]
84    pub struct DelRule {
85        pub version: Option<Version>,
86    }
87
88    #[derive(Default, CandidType, Deserialize, Clone)]
89    pub struct ListRulesParams {
90        pub matcher: Option<ListRulesMatcher>,
91    }
92
93    #[derive(Default, CandidType, Deserialize, Clone)]
94    pub struct ListRulesMatcher {
95        pub include_system: bool,
96    }
97
98    #[derive(Default, CandidType, Deserialize, Clone)]
99    pub struct ListRulesResults {
100        pub items: Vec<(CollectionKey, Rule)>,
101        pub items_length: usize,
102        pub matches_length: usize,
103    }
104}