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 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
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, derive a unit struct with Config and attach the config data
use Config;
// Declare a config type containing literal sources.
// While only using literal sources,
// a format needs to be specified.
// Including a file from disk is also possible,
// see `examples/include.rs`.
// When there are multiple sources,
// latter ones overwrite former ones.
;
Then, access the data inside using the path!() macro
use path;
// Use `Index`, `From` traits to access data.
// Different types may be accessible from a field.
let title: &str = MyConfig.into;
assert_eq!;
let title: String = MyConfig.into;
assert_eq!;
// A deeper path.
let owner: &str = MyConfig.into;
assert_eq!;
// Any numerical types.
let timeout: u32 = MyConfig.into;
assert_eq!;
let timeout: f32 = MyConfig.into;
// A homogeneous array can be accessed as `Vec<T>`.
let ports: = MyConfig.into;
assert_eq!;
Check out more examples for more details about usage.