Skip to main content

canic_core/ids/template/
mod.rs

1use crate::memory::impl_storable_bounded;
2use candid::CandidType;
3use serde::{Deserialize, Serialize};
4use std::{borrow::Borrow, borrow::Cow, fmt};
5
6///
7/// TemplateId
8///
9
10#[derive(
11    CandidType, Clone, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
12)]
13#[serde(transparent)]
14pub struct TemplateId(pub Cow<'static, str>);
15
16impl TemplateId {
17    #[must_use]
18    pub const fn new(s: &'static str) -> Self {
19        Self(Cow::Borrowed(s))
20    }
21
22    #[must_use]
23    pub const fn owned(s: String) -> Self {
24        Self(Cow::Owned(s))
25    }
26
27    #[must_use]
28    pub fn as_str(&self) -> &str {
29        &self.0
30    }
31}
32
33impl From<&'static str> for TemplateId {
34    fn from(value: &'static str) -> Self {
35        Self::new(value)
36    }
37}
38
39impl From<String> for TemplateId {
40    fn from(value: String) -> Self {
41        Self::owned(value)
42    }
43}
44
45impl From<&String> for TemplateId {
46    fn from(value: &String) -> Self {
47        Self::owned(value.clone())
48    }
49}
50
51impl Borrow<str> for TemplateId {
52    fn borrow(&self) -> &str {
53        self.as_str()
54    }
55}
56
57impl fmt::Display for TemplateId {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.write_str(self.as_str())
60    }
61}
62
63impl_storable_bounded!(TemplateId, 160, false);
64
65///
66/// TemplateVersion
67///
68
69#[derive(
70    CandidType, Clone, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
71)]
72#[serde(transparent)]
73pub struct TemplateVersion(pub Cow<'static, str>);
74
75impl TemplateVersion {
76    #[must_use]
77    pub const fn new(s: &'static str) -> Self {
78        Self(Cow::Borrowed(s))
79    }
80
81    #[must_use]
82    pub const fn owned(s: String) -> Self {
83        Self(Cow::Owned(s))
84    }
85
86    #[must_use]
87    pub fn as_str(&self) -> &str {
88        &self.0
89    }
90}
91
92impl From<&'static str> for TemplateVersion {
93    fn from(value: &'static str) -> Self {
94        Self::new(value)
95    }
96}
97
98impl From<String> for TemplateVersion {
99    fn from(value: String) -> Self {
100        Self::owned(value)
101    }
102}
103
104impl From<&String> for TemplateVersion {
105    fn from(value: &String) -> Self {
106        Self::owned(value.clone())
107    }
108}
109
110impl Borrow<str> for TemplateVersion {
111    fn borrow(&self) -> &str {
112        self.as_str()
113    }
114}
115
116impl fmt::Display for TemplateVersion {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        f.write_str(self.as_str())
119    }
120}
121
122impl_storable_bounded!(TemplateVersion, 64, false);
123
124///
125/// WasmStoreBinding
126///
127
128#[derive(
129    CandidType, Clone, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
130)]
131#[serde(transparent)]
132pub struct WasmStoreBinding(pub Cow<'static, str>);
133
134impl WasmStoreBinding {
135    #[must_use]
136    pub const fn new(s: &'static str) -> Self {
137        Self(Cow::Borrowed(s))
138    }
139
140    #[must_use]
141    pub const fn owned(s: String) -> Self {
142        Self(Cow::Owned(s))
143    }
144
145    #[must_use]
146    pub fn as_str(&self) -> &str {
147        &self.0
148    }
149}
150
151impl From<&'static str> for WasmStoreBinding {
152    fn from(value: &'static str) -> Self {
153        Self::new(value)
154    }
155}
156
157impl From<String> for WasmStoreBinding {
158    fn from(value: String) -> Self {
159        Self::owned(value)
160    }
161}
162
163impl From<&String> for WasmStoreBinding {
164    fn from(value: &String) -> Self {
165        Self::owned(value.clone())
166    }
167}
168
169impl Borrow<str> for WasmStoreBinding {
170    fn borrow(&self) -> &str {
171        self.as_str()
172    }
173}
174
175impl fmt::Display for WasmStoreBinding {
176    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177        f.write_str(self.as_str())
178    }
179}
180
181impl_storable_bounded!(WasmStoreBinding, 64, false);
182
183///
184/// TemplateReleaseKey
185///
186
187#[derive(
188    CandidType, Clone, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
189)]
190pub struct TemplateReleaseKey {
191    pub template_id: TemplateId,
192    pub version: TemplateVersion,
193}
194
195impl TemplateReleaseKey {
196    #[must_use]
197    pub const fn new(template_id: TemplateId, version: TemplateVersion) -> Self {
198        Self {
199            template_id,
200            version,
201        }
202    }
203}
204
205impl fmt::Display for TemplateReleaseKey {
206    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207        write!(f, "{}@{}", self.template_id, self.version)
208    }
209}
210
211impl_storable_bounded!(TemplateReleaseKey, 256, false);
212
213///
214/// TemplateChunkKey
215///
216
217#[derive(
218    CandidType, Clone, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
219)]
220pub struct TemplateChunkKey {
221    pub release: TemplateReleaseKey,
222    pub chunk_index: u32,
223}
224
225impl TemplateChunkKey {
226    #[must_use]
227    pub const fn new(release: TemplateReleaseKey, chunk_index: u32) -> Self {
228        Self {
229            release,
230            chunk_index,
231        }
232    }
233}
234
235impl fmt::Display for TemplateChunkKey {
236    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237        write!(f, "{}#{}", self.release, self.chunk_index)
238    }
239}
240
241impl_storable_bounded!(TemplateChunkKey, 320, false);
242
243///
244/// TemplateChunkingMode
245///
246
247#[derive(
248    CandidType, Clone, Copy, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq,
249)]
250pub enum TemplateChunkingMode {
251    Inline,
252    Chunked,
253}
254
255///
256/// WasmStoreGcMode
257///
258
259#[derive(
260    CandidType, Clone, Copy, Debug, Default, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq,
261)]
262pub enum WasmStoreGcMode {
263    #[default]
264    Normal,
265    Prepared,
266    InProgress,
267    Complete,
268}
269
270///
271/// WasmStoreGcStatus
272///
273
274#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
275pub struct WasmStoreGcStatus {
276    pub mode: WasmStoreGcMode,
277    pub changed_at: u64,
278    pub prepared_at: Option<u64>,
279    pub started_at: Option<u64>,
280    pub completed_at: Option<u64>,
281    pub runs_completed: u32,
282}
283
284///
285/// TemplateManifestState
286///
287
288#[derive(
289    CandidType, Clone, Copy, Debug, Eq, Ord, PartialOrd, Deserialize, Serialize, PartialEq,
290)]
291pub enum TemplateManifestState {
292    Staged,
293    Approved,
294    Blocked,
295    Deprecated,
296}