Skip to main content

canic_core/
bootstrap.rs

1use crate::config::{Config, ConfigError, schema::ConfigModel};
2use std::sync::Arc;
3
4/// Parse, validate, and install the Canic configuration.
5///
6/// This function is intentionally:
7/// - deterministic
8/// - synchronous
9/// - side-effect limited to internal config state
10///
11/// It is safe to call from both build-time validation and
12/// canister init / post-upgrade bootstrap paths.
13pub fn init_config(toml: &str) -> Result<Arc<ConfigModel>, ConfigError> {
14    parse_and_install_config(toml)
15}
16
17/// Compact a validated Canic TOML source without changing value encodings.
18#[must_use]
19pub fn compact_config_source(toml: &str) -> String {
20    let mut compact = String::new();
21
22    for line in toml.lines() {
23        let trimmed = line.trim();
24
25        if trimmed.is_empty() || trimmed.starts_with('#') {
26            continue;
27        }
28
29        compact.push_str(trimmed);
30        compact.push('\n');
31    }
32
33    compact
34}
35
36fn parse_and_install_config(toml: &str) -> Result<Arc<ConfigModel>, ConfigError> {
37    Config::init_from_toml(toml)?;
38
39    Config::try_get().ok_or(ConfigError::NotInitialized)
40}