Skip to main content

canic_core/cdk/
mod.rs

1//! Module: cdk
2//!
3//! Responsibility: Canic's IC CDK facade and stable-structure helper macros.
4//! Does not own: application policy, endpoint auth, or stable schema design.
5//! Boundary: re-exports IC-facing SDKs behind the Canic core surface.
6
7pub use candid;
8pub use ic_cdk::{
9    api, call, eprintln, export_candid, futures, init, inspect_message, post_upgrade, println,
10    query, trap, update,
11};
12pub use ic_cdk_management_canister as mgmt;
13pub use ic_cdk_timers as timers;
14pub use icrc_ledger_types;
15
16pub mod serialize;
17pub mod spec;
18pub mod structures;
19pub mod types;
20pub mod utils;
21
22///
23/// impl_storable_bounded
24///
25/// Implement bounded stable storage encoding for one serde-backed type.
26///
27
28#[macro_export]
29macro_rules! impl_storable_bounded {
30    ($ident:ident, $max_size:expr, $is_fixed_size:expr) => {
31        impl $crate::cdk::structures::storable::Storable for $ident {
32            const BOUND: $crate::cdk::structures::storable::Bound =
33                $crate::cdk::structures::storable::Bound::Bounded {
34                    max_size: $max_size,
35                    is_fixed_size: $is_fixed_size,
36                };
37
38            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
39                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
40                    panic!("impl_storable_bounded: serialize failed: {err}");
41                });
42
43                ::std::borrow::Cow::Owned(bytes)
44            }
45
46            fn into_bytes(self) -> Vec<u8> {
47                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
48                    panic!("impl_storable_bounded: serialize failed: {err}");
49                })
50            }
51
52            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
53                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
54                    panic!("impl_storable_bounded: deserialize failed: {err}");
55                })
56            }
57        }
58    };
59}
60
61///
62/// impl_storable_unbounded
63///
64/// Implement unbounded stable storage encoding for one serde-backed type.
65///
66
67#[macro_export]
68macro_rules! impl_storable_unbounded {
69    ($ident:ident) => {
70        impl $crate::cdk::structures::storable::Storable for $ident {
71            const BOUND: $crate::cdk::structures::storable::Bound =
72                $crate::cdk::structures::storable::Bound::Unbounded;
73
74            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
75                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
76                    panic!("impl_storable_unbounded: serialize failed: {err}");
77                });
78
79                ::std::borrow::Cow::Owned(bytes)
80            }
81
82            fn into_bytes(self) -> Vec<u8> {
83                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
84                    panic!("impl_storable_unbounded: serialize failed: {err}");
85                })
86            }
87
88            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
89                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
90                    panic!("impl_storable_unbounded: deserialize failed: {err}");
91                })
92            }
93        }
94    };
95}