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;
22#[doc(hidden)]
23pub mod __sharding_core;
24pub mod access;
25pub mod api;
26pub mod bootstrap;
27#[doc(hidden)]
28pub mod dispatch;
29pub mod domain;
30pub mod dto;
31pub mod error;
32mod format;
33pub mod ids;
34pub mod log;
35pub mod perf;
36pub mod protocol;
37#[cfg(test)]
38pub mod test;
39
40pub(crate) mod config;
41pub(crate) mod infra;
42pub(crate) mod lifecycle;
43pub(crate) mod ops;
44pub(crate) mod storage;
45pub(crate) mod view;
46pub(crate) mod workflow;
47
48pub use {
49    ::canic_cdk as cdk,
50    ::canic_memory as memory,
51    ::canic_memory::{eager_init, eager_static, ic_memory, ic_memory_range},
52};
53
54pub(crate) use error::{InternalError, InternalErrorClass, InternalErrorOrigin};
55
56/// Internal re-exports required for macro expansion.
57/// Not part of the public API.
58#[doc(hidden)]
59pub mod __reexports {
60    pub use ::ctor;
61}
62
63///
64/// Consts
65///
66
67pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
68pub const VERSION: &str = env!("CARGO_PKG_VERSION");
69pub const CANIC_MEMORY_MIN: u8 = storage::stable::CANIC_MEMORY_MIN;
70pub const CANIC_MEMORY_MAX: u8 = storage::stable::CANIC_MEMORY_MAX;
71
72#[cfg(test)]
73#[macro_export]
74macro_rules! assert_err_variant {
75    ($err:expr, $pat:pat $(if $guard:expr)? $(,)?) => {{
76        match $err {
77            $pat $(if $guard)? => {}
78            other => panic!("unexpected error variant: {other:?}"),
79        }
80    }};
81}