Skip to main content

canic_core/bootstrap/
mod.rs

1use crate::config::{Config, ConfigError, schema::ConfigModel};
2use std::sync::Arc;
3
4#[cfg(any(not(target_arch = "wasm32"), test))]
5mod render;
6
7#[doc(hidden)]
8pub mod compiled {
9    pub use crate::{
10        cdk::{candid::Principal, types::Cycles},
11        config::schema::{
12            AppConfig, AppInitMode, AuthConfig, CanisterConfig, CanisterKind, CanisterPool,
13            ConfigModel, DelegatedAuthCanisterConfig, DelegatedTokenConfig,
14            DelegationProofCacheConfig, DelegationProofCacheProfile, LogConfig, PoolImport,
15            RandomnessConfig, RandomnessSource, RoleAttestationConfig, ScalePool, ScalePoolPolicy,
16            ScalingConfig, ShardPool, ShardPoolPolicy, ShardingConfig, Standards,
17            StandardsCanisterConfig, SubnetConfig, TopupPolicy, Whitelist,
18        },
19        ids::{CanisterRole, SubnetRole},
20    };
21}
22
23/// EmbeddedRootBootstrapEntry
24///
25
26#[derive(Clone, Copy, Debug, Eq, PartialEq)]
27pub struct EmbeddedRootBootstrapEntry {
28    pub role: &'static str,
29    pub wasm_module: &'static [u8],
30    pub artifact_path: &'static str,
31    pub embedded_artifact_path: &'static str,
32    pub artifact_kind: &'static str,
33    pub artifact_size_bytes: u64,
34    pub artifact_sha256_hex: &'static str,
35    pub decompressed_size_bytes: Option<u64>,
36    pub decompressed_sha256_hex: Option<&'static str>,
37}
38
39// Install a build-produced configuration model and its canonical TOML source.
40pub fn init_compiled_config(
41    config: ConfigModel,
42    source_toml: &str,
43) -> Result<Arc<ConfigModel>, ConfigError> {
44    Config::init_from_model(config, source_toml)
45}
46
47// Parse and validate the source TOML into a configuration model on host targets.
48#[cfg(any(not(target_arch = "wasm32"), test))]
49pub fn parse_config_model(toml: &str) -> Result<ConfigModel, ConfigError> {
50    Config::parse_toml(toml)
51}
52
53// Compact a validated Canic TOML source without changing value encodings.
54#[cfg(any(not(target_arch = "wasm32"), test))]
55#[must_use]
56pub fn compact_config_source(toml: &str) -> String {
57    let mut compact = String::new();
58
59    for line in toml.lines() {
60        let trimmed = line.trim();
61
62        if trimmed.is_empty() || trimmed.starts_with('#') {
63            continue;
64        }
65
66        compact.push_str(trimmed);
67        compact.push('\n');
68    }
69
70    compact
71}
72
73// Render the validated configuration model as Rust source for `include!` at runtime.
74#[cfg(any(not(target_arch = "wasm32"), test))]
75#[must_use]
76pub fn emit_config_model_source(config: &ConfigModel) -> String {
77    render::config_model(config)
78}