diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-29 01:03:19 -0800 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-29 01:03:19 -0800 |
commit | ae89babba1623793fe62d2d645eccfbff3f49708 (patch) | |
tree | 56c99eb8125aa5e5b81af041891b894e3f2e2535 /README.md | |
parent | b07a13c955d2de54093a382810209e3f9f7c3a1e (diff) |
:memo:
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 38 |
1 files changed, 31 insertions, 7 deletions
@@ -33,21 +33,45 @@ Configuration is gathered by building a `Source` and then merging that source in current state of the configuration. ```rust -// Add environment variables that begin with RUST_ -config::merge(config::Environment::new("RUST")); +fn main() { + // Add environment variables that begin with RUST_ + config::merge(config::Environment::new("RUST")); -// Add 'Settings.json' -config::merge(config::File::new("Settings", config::FileFormat::Json)); + // Add 'Settings.json' + config::merge(config::File::new("Settings", config::FileFormat::Json)); -// Add 'Settings.$(RUST_ENV).json` -let name = format!("Settings.{}", config::get_str("env").unwrap()); -config::merge(config::File::new(&name, config::FileFormat::Json)); + // Add 'Settings.$(RUST_ENV).json` + let name = format!("Settings.{}", config::get_str("env").unwrap()); + config::merge(config::File::new(&name, config::FileFormat::Json)); +} ``` Note that in the above example the calls to `config::merge` could have been re-ordered to influence the priority as each successive merge is evaluated on top of the previous. +Configuration values can be retrieved with a call to `config::get` and then +coerced into a type with `as_*`. + +```toml +# Settings.toml +debug = 1 +``` + +```rust +fn main() { + // Add 'Settings.toml' (from above) + config::merge(config::File::new("Settings", config::FileFormat::Toml)); + + // Get 'debug' and coerce to a boolean + assert_eq!(config::get("debug").unwrap().as_bool(), Some(true)); + + // You can use a type suffix on `config::get` to simplify + assert_eq!(config::get_bool("debug"), Some(true)); + assert_eq!(config::get_str("debug"), Some("true")); +} +``` + See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for more usage information. |