inline-config-macros

Crates.ioinline-config-macros
lib.rsinline-config-macros
version0.3.1
created_at2025-12-30 20:25:06.108462+00
updated_at2026-01-19 03:16:49.902073+00
descriptionProcedual macros for inline-config
homepagehttps://github.com/YishiMichael/inline-config
repositoryhttps://github.com/YishiMichael/inline-config
max_upload_size
id2013259
size44,243
YishiMichael (YishiMichael)

documentation

README

inline-config

Effortlessly embed config modules and access with any compatible types.

Version License Docs CI

A procedual macro config is provided to parse sources at compile time, generate corresponding data structures, from which we can access values via the Index trait and the From trait.

Features

  • JSON, YAML, TOML formats are supported.
  • Both inline literal configs and file inclusions are supported; overwriting is supported.
  • Compile-time source validation. Errors are clearly reported for easier debugging.
  • Infallible data access. Path existence and type compatibility are both checked at compile time.
  • Define custom data structures to access data.
  • The feature flag indexmap enables preserving orders of tables. Check this example for details.

Usage

Add inline-config to your dependencies

cargo add inline-config

In your source file, declare a module using config holding the config data

use inline_config::config;

// Declare a config module containing literal sources.
// With `export(static = MY_CONFIG)`,
// a static variable `MY_CONFIG` will be brought into scope.
#[config(export(static = MY_CONFIG))]
mod my_config {
    // When there are multiple sources,
    // latter ones overwrite former ones.
    toml!(
        r#"
        title = "TOML example"

        [server]
        owner = "Tom"
        timeout = 2000
        ports = [ 8000, 8001, 8002 ]
        "#
    );
    toml!(
        r#"
        [server]
        timeout = 5000
        "#
    );

    // Including a file from disk is also possible,
    // see `examples/include.rs`.
}

Then, access the data inside using the path!() macro

use inline_config::path;

// Use `Index`, `From` traits to access data.
// Different types may be accessible from a field.
let title: &str = MY_CONFIG[path!(title)].into();
assert_eq!("TOML example", title);
let title: String = MY_CONFIG[path!(title)].into();
assert_eq!("TOML example", title);

// A deeper path.
let owner: &str = MY_CONFIG[path!(server.owner)].into();
assert_eq!("Tom", owner);

// Any numerical types.
let timeout: u32 = MY_CONFIG[path!(server.timeout)].into();
assert_eq!(5000, timeout);
let timeout: f32 = MY_CONFIG[path!(server.timeout)].into();

// A homogeneous array can be accessed as `Vec<T>`.
let ports: Vec<u64> = MY_CONFIG[path!(server.ports)].into();
assert_eq!([8000, 8001, 8002].to_vec(), ports);

Check out more examples for more details about usage.

Commit count: 47

cargo fmt