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// Install a build-produced configuration model and its canonical TOML source.
24pub fn init_compiled_config(
25    config: ConfigModel,
26    source_toml: &str,
27) -> Result<Arc<ConfigModel>, ConfigError> {
28    Config::init_from_model(config, source_toml)
29}
30
31// Parse and validate the source TOML into a configuration model on host targets.
32#[cfg(any(not(target_arch = "wasm32"), test))]
33pub fn parse_config_model(toml: &str) -> Result<ConfigModel, ConfigError> {
34    Config::parse_toml(toml)
35}
36
37// Compact a validated Canic TOML source without changing value encodings.
38#[cfg(any(not(target_arch = "wasm32"), test))]
39#[must_use]
40pub fn compact_config_source(toml: &str) -> String {
41    let mut compact = String::new();
42
43    for line in toml.lines() {
44        let trimmed = line.trim();
45
46        if trimmed.is_empty() || trimmed.starts_with('#') {
47            continue;
48        }
49
50        compact.push_str(trimmed);
51        compact.push('\n');
52    }
53
54    compact
55}
56
57// Render the validated configuration model as Rust source for `include!` at runtime.
58#[cfg(any(not(target_arch = "wasm32"), test))]
59#[must_use]
60pub fn emit_config_model_source(config: &ConfigModel) -> String {
61    render::config_model(config)
62}