From 39f2e6c55ba2f270383a603eb5008ac64ba7ba08 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 24 Jan 2017 02:12:57 -0800 Subject: Simply docs --- README.md | 84 +++++++++++++++++++++++---------------------------------------- 1 file changed, 31 insertions(+), 53 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 1ad7cb5..5db1c30 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,6 @@ config = { git = "https://github.com/mehcode/config-rs.git" } ## Usage -### Setting Values - Configuration is collected in a series of layers, in order from lowest to highest priority. 1. Explicit Default — `config::set_default` @@ -23,64 +21,44 @@ Configuration is collected in a series of layers, in order from lowest to highes 3. Environment 4. Explicit Set — `config::set` -#### Defaults - -By default, `None` is returned when requesting a configuration value -that does not exist elsewhere. - -Defaults may be established in code. - ```rust +// Set explicit defaults. This is optional but can be used +// to ensure a key always has a value. config::set_default("port", 80); -``` -#### Environment - -```rust -// Keep your environment unique and predictable +assert_eq!(config::get_int("port"), 80); + +// Merge in a configuration file +// +// --- +// [development] +// host = "::1" +// factor = 5.321 +// +// [development.redis] +// port = 80 +// --- +config::merge(config::File::with_name("Settings").namespace("development")); + +assert_eq!(config::get_str("host"), Some("::1")); +assert_eq!(config::get_int("factor"), Some(5)); +assert_eq!(config::get_str("redis.port"), Some("80")); + +// Keep your environment unique and predictable by +// namespacing environment variable usage config::set_env_prefix("rust"); -// Enable environment -// config::bind_env("port"); -// config::bind_env_to("port", "port"); -config::bind_env_all(); - -// Environment variables are typically set outside of the application +// Environment variables would normally be set outside of the application std::env::set_var("RUST_PORT", "80"); -std::env::set_var("RUST_PORT2", "602"); - -config::get_int("port"); //= Some(80) -config::get_int("port2"); //= Some(602) -``` - -#### Source - -##### File +std::env::set_var("RUST_HOST", "::0"); +std::env::set_var("RUST_DEBUG", "false"); -Read `${CWD}/Settings.toml` and populate configuration. - - `prefix` is used to only pull keys nested under the named key - - `required(true)` (default) will cause an error to be returned if the file failed to be read/parsed/etc. +assert_eq!(config::get_int("port"), Some(80)); +assert_eq!(config::get_str("host"), Some("::0")); +assert_eq!(config::get_bool("debug"), Some(false)); -```rust -config::merge( - config::source::File::with_name("Settings") - .required(false) - .prefix("development") -).unwrap(); -``` +// Set an explicit override of a key +confing::set("debug", true); -### Getting Values - -Values will attempt to convert from their underlying type (from when they were set) when accessed. - - - `config::get::(key: &str) -> T` - - `config::get_str(key: &str) -> &str` - - `config::get_int(key: &str) -> i64` - - `config::get_float(key: &str) -> float` - - `config::get_bool(key: &str) -> bool` - -```rust -if config::get("debug").unwrap() { - println!("address: {:?}", config::get_int("port")); -} +assert_eq!(config::get_bool("debug"), true); ``` -- cgit v1.2.3