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 guard/auth/rule helpers 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// -----------------------------------------------------------------------------
21// Phase 0: path coherence re-exports (no behavior change)
22// -----------------------------------------------------------------------------
23
24pub mod access; // todo - potentially could be pub(crate) but custom errors would have to change
25pub mod api;
26#[doc(hidden)]
27pub mod dispatch;
28pub mod domain;
29pub mod dto;
30pub mod ids;
31pub mod log;
32pub mod perf;
33pub mod protocol;
34#[cfg(test)]
35pub mod test;
36
37pub(crate) mod config;
38pub(crate) mod infra;
39pub(crate) mod lifecycle;
40pub(crate) mod ops;
41pub(crate) mod storage;
42pub(crate) mod workflow;
43
44pub use {
45    ::canic_cdk as cdk,
46    ::canic_memory as memory,
47    ::canic_memory::{eager_init, eager_static, ic_memory, ic_memory_range},
48    ::canic_utils as utils,
49    dto::error::{Error as PublicError, ErrorCode},
50    thiserror::Error as ThisError,
51};
52
53/// Internal re-exports required for macro expansion.
54/// Not part of the public API.
55#[doc(hidden)]
56pub mod __reexports {
57    pub use ::ctor;
58}
59
60///
61/// Crate Version
62///
63
64pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
65pub const VERSION: &str = env!("CARGO_PKG_VERSION");
66
67///
68/// Error
69///
70/// Internal, structured error type.
71///
72/// This error:
73/// - is NOT Candid-exposed
74/// - is NOT stable across versions
75/// - may evolve freely
76///
77/// All canister endpoints must convert this into a public error envelope
78/// defined in dto/.
79///
80
81#[derive(Debug, ThisError)]
82pub(crate) enum Error {
83    #[error(transparent)]
84    Access(#[from] access::AccessError),
85
86    #[error(transparent)]
87    Config(#[from] config::ConfigError),
88
89    #[error(transparent)]
90    Domain(#[from] domain::DomainError),
91
92    #[error(transparent)]
93    Infra(#[from] infra::InfraError),
94
95    #[error(transparent)]
96    Ops(#[from] ops::OpsError),
97
98    #[error(transparent)]
99    Storage(#[from] storage::StorageError),
100
101    #[error(transparent)]
102    Workflow(#[from] workflow::WorkflowError),
103}
104
105// init and validate config
106// called from here as config is pub(crate)
107pub fn init_config(toml: &str) -> Result<(), String> {
108    config::Config::init_from_toml(toml)
109        .map(|_| ())
110        .map_err(|err| err.to_string())
111}