summaryrefslogtreecommitdiffstats
path: root/README.md
blob: b7d4c6410e3667558c14bc69a22528daf0d73fa5 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

### Setting Values

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`

#### Defaults

By default, `None` is returned when requesting a configuration value
that does not exist elsewhere.

Defaults may be established in code.

```rust
config::set_default("port", 80);
```

#### Environment

```rust
// Keep your environment unique and predictable
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
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

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.

```rust
config::merge(
    config::source::File::with_name("Settings")
        .required(false)
        .prefix("development")
).unwrap();
```

## Getting Values

Values will attempt to convert from their underlying type (from when they were set) when accessed.

 - `config::get::<T>(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"));
}
```