inline-config
Effortlessly embed config modules and access with any compatible types.
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 Into 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
indexmapenables 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 config;
// Declare a config module containing literal sources.
// With `export(static = MY_CONFIG)`, a static variable `MY_CONFIG` will be brought into scope.
Then, access the data inside using the path!() macro
use path;
// Multiple types may implement `From` trait, so type annotations are required.
let title: &str = MY_CONFIG.into;
assert_eq!;
let title: String = MY_CONFIG.into;
assert_eq!;
// A deeper path.
let owner: &str = MY_CONFIG.into;
assert_eq!;
// Any numerical types.
let timeout: u32 = MY_CONFIG.into;
assert_eq!;
let timeout: f32 = MY_CONFIG.into;
// A homogeneous array can be accessed as `Vec<T>`.
let ports: = MY_CONFIG.into;
assert_eq!;
Check out more examples for more details about usage.