inline-config
Effortlessly embed config as static data and access with any compatible data structures.
A procedual macro config! is provided to parse sources at compile time, generate static data structures, from which we can access values via the Get trait. The output types of accessed values can be almost "at will", as long as they are compatible.
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 static variable using config! holding the config data
use config;
config!
Then, access the data inside using the Get trait in combination with the path! macro
use ;
// Multiple types may be compatible. As a cost, type annotation is always required.
let title: &str = MY_CONFIG.get;
assert_eq!;
let title: String = MY_CONFIG.get;
assert_eq!;
// A deeper path.
let owner: &str = MY_CONFIG.get;
assert_eq!;
// Any numerical types.
let timeout: u32 = MY_CONFIG.get;
assert_eq!;
let timeout: f32 = MY_CONFIG.get;
// A homogeneous array can be accessed as `Vec<T>`.
let ports: = MY_CONFIG.get;
assert_eq!;
Check out more examples for more details about usage.