1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# config-rs
> Application Configuration for Rust
config-rs is a layered configuration system for Rust applications (including [12-factor]).
[12-factor]: https://12factor.net/config
## Install
```toml
[dependencies]
config = { git = "https://github.com/mehcode/config-rs.git" }
```
## Usage
Configuration is collected in a series of layers, in order from lowest to highest priority.
1. Explicit Default — `config::set_default`
2. Source — File, Remote (ETCD, Consul, etc.)
3. Environment
4. Explicit Set — `config::set`
```rust
// Set explicit defaults. This is optional but can be used
// to ensure a key always has a value.
config::set_default("port", 80);
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");
// Environment variables would normally be set outside of the application
std::env::set_var("RUST_PORT", "80");
std::env::set_var("RUST_HOST", "::0");
std::env::set_var("RUST_DEBUG", "false");
assert_eq!(config::get_int("port"), Some(80));
assert_eq!(config::get_str("host"), Some("::0"));
assert_eq!(config::get_bool("debug"), Some(false));
// Set an explicit override of a key
confing::set("debug", true);
assert_eq!(config::get_bool("debug"), true);
```
|