static-toml 1.3.0

Effortlessly embed TOML files into your Rust code as static data with custom data structures.
Documentation
//! Example demonstrating how to embed a TOML file for configuration data in a
//! Rust binary. This approach allows for default values to be included
//! statically while also supporting dynamic deserialization of user-provided
//! data at runtime.

static_toml::static_toml! {
    #[derive(Debug, Clone, serde::Deserialize)]
    // Use `cow` to enable `Cow<'static, str>` instead of just `&'static str`
    #[static_toml(cow, root_mod = config)]
    const DEFAULT_CONFIG = include_toml!("examples/config.toml");
}

const INPUT_CONFIG: &str = r#"
[config]
string = "another value"
int = 69
array = [8, 0, 0, 8, 5]
"#;

fn main() {
    use config::Config;

    // Using the default config generated by the macro
    let default: Config = DEFAULT_CONFIG.clone();
    println!("default values for a config: {default:?}");

    // Deserializing user input for config, supporting dynamic strings
    let input: Config = toml::from_str(INPUT_CONFIG).unwrap();
    println!("user input for a config: {input:?}");
}