Crates.io | cfg-rs |
lib.rs | cfg-rs |
version | 0.4.1 |
source | src |
created_at | 2021-08-21 08:53:13.259483 |
updated_at | 2024-10-02 15:29:59.429557 |
description | A rust configuration loader |
homepage | https://github.com/leptonyu/cfg-rs |
repository | https://github.com/leptonyu/cfg-rs |
max_upload_size | |
id | 440255 |
size | 158,908 |
${config.key}
, see placeholder.configuration.get::<u8>("random.u8")
will get random u8
value.See the examples for general usage information.
use cfg_rs::*;
let configuration = Configuration::with_predefined().unwrap();
// use configuration.
See init for details.
use cfg_rs::*;
init_cargo_env!();
let configuration = Configuration::with_predefined_builder()
.set_cargo_env(init_cargo_env())
.init()
.unwrap();
// use configuration.
See init for details.
use cfg_rs::*;
init_cargo_env!();
let mut configuration = Configuration::new()
// Layer 0: Register cargo env config source.
.register_source(init_cargo_env()).unwrap()
// Layer 1: Register customized config.
.register_kv("customized_config")
.set("hello", "world")
.finish()
.unwrap();
// Layer 2: Register random value config.
#[cfg(feature = "rand")]
{
configuration = configuration.register_random().unwrap();
}
// Layer 3: Register all env variables `CFG_*`.
configuration = configuration.register_prefix_env("CFG").unwrap()
// Layer 4: Register yaml file(Need feature yaml).
.register_file("/conf/app.yaml", true).unwrap();
#[cfg(feature = "toml")]
{
let toml = inline_source!("../app.toml").unwrap();
configuration = configuration.register_source(toml).unwrap();
}
// use configuration.
See register_kv, register_file, register_random, register_prefix_env for details.
Config order is determined by the order of registering sources, register earlier have higher priority. ↩