Skip to main content

canic_core/
lib.rs

1//! Core Canic library used inside canisters.
2//!
3//! Most users should depend on the `canic` facade crate, which re-exports this crate
4//! under `canic::core` and exposes the common entrypoint macros:
5//! - `canic::build!` / `canic::build_root!` (in `build.rs`) to validate/embed `canic.toml`
6//! - `canic::start!` / `canic::start_root!` (in `lib.rs`) to wire lifecycle hooks and export endpoints
7//!
8//! ## Layering
9//!
10//! Canic is organized to keep endpoint code thin and policies centralized:
11//! - `access/` contains access expressions, predicates, and metrics for boundary enforcement.
12//! - `workflow/` implements orchestration and lifecycle workflows.
13//! - `policy/` owns deterministic decision rules.
14//! - `ops/` provides mechanical, reusable side-effecting operations.
15//! - `model/` owns storage (stable memory) and in-process registries/caches.
16//! - macro entrypoints live in the `canic` facade crate.
17//!
18//! The default flow is: endpoints → workflow → policy → ops → model.
19
20#[doc(hidden)]
21pub mod __control_plane_core;
22pub mod access;
23pub mod api;
24pub mod bootstrap;
25#[doc(hidden)]
26pub mod dispatch;
27pub mod domain;
28pub mod dto;
29pub mod error;
30mod format;
31pub mod ids;
32pub mod log;
33pub mod perf;
34pub mod protocol;
35#[cfg(test)]
36pub mod test;
37
38pub(crate) mod config;
39pub(crate) mod infra;
40pub(crate) mod lifecycle;
41pub(crate) mod ops;
42pub(crate) mod storage;
43pub(crate) mod view;
44pub(crate) mod workflow;
45
46pub use {
47    ::canic_cdk as cdk,
48    ::canic_memory as memory,
49    ::canic_memory::{eager_init, eager_static, ic_memory, ic_memory_range},
50};
51
52pub(crate) use error::{InternalError, InternalErrorClass, InternalErrorOrigin};
53
54/// Internal re-exports required for macro expansion.
55/// Not part of the public API.
56#[doc(hidden)]
57pub mod __reexports {
58    pub use ::ctor;
59}
60
61///
62/// Consts
63///
64
65pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
66pub const VERSION: &str = env!("CARGO_PKG_VERSION");
67pub const CANIC_MEMORY_MIN: u8 = storage::stable::CANIC_MEMORY_MIN;
68pub const CANIC_MEMORY_MAX: u8 = storage::stable::CANIC_MEMORY_MAX;
69// Canonical hardcoded 1 MiB chunk size for Canic wasm staging/install flows.
70// The management canister wasm chunk store rejects larger payloads.
71pub const CANIC_WASM_CHUNK_BYTES: usize = 1_048_576;
72
73#[macro_export]
74macro_rules! perf {
75    ($($label:tt)*) => {{
76        $crate::perf::PERF_LAST.with(|last| {
77            let now = $crate::perf::perf_counter();
78            let then = *last.borrow();
79            let delta = now.saturating_sub(then);
80
81            *last.borrow_mut() = now;
82
83            let label = format!($($label)*);
84            $crate::perf::record_checkpoint(module_path!(), &label, delta);
85        });
86    }};
87}
88
89#[cfg(test)]
90#[macro_export]
91macro_rules! assert_err_variant {
92    ($err:expr, $pat:pat $(if $guard:expr)? $(,)?) => {{
93        match $err {
94            $pat $(if $guard)? => {}
95            other => panic!("unexpected error variant: {other:?}"),
96        }
97    }};
98}