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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 JSRPC “Kryptonite”
//! > **Crate to build config from environment, command line and files**
//! # Motivation
//! Non-runtime data generally comes to a project from
//! command line, environment and configuration files.\
//! Sometimes it comes from each of the sources simultaneously,
//! so all of them must be handled.\
//! None of the popular crates (including [clap](https://docs.rs/clap/latest/clap/) and [config](https://docs.rs/config/latest/config/))
//! can't handle all 3 together, so this crate has been created to solve this problem.
//!
//! # Basis
//! The Core of the crate is an attribute-macro [config](attr.config.html). \
//! Annotate structure with this macro and a field of it with the `source` attribute,
//! so the field will be searched in one of the provided sources. The sources can be provided by using the following nested `source` attributes: \
//! 1. `clap`: command line argument
//! 2. `env`: environment variable
//! 3. `config`: configuration file key
//! 4. `default`: default value
//!
//! **Example**
//! ```
//! use config_manager::config;
//!
//! #[config]
//! struct ApplicationConfig {
//! #[source(clap(long, short = 'p'), env = "APP_MODEL_PATH", config)]
//! model_path: String,
//! #[source(env, config, default = 0)]
//! prediction_delay: u64,
//! }
//! ```
//! In the example above, to set the value of the `model_path` field, a user may provide:
//! - command line argument `--model_path`
//! - environment variable named `model_path`
//! - configuration file containing field `model_path`
//!
//! If the value is found in multiple provided sources, the value will be assigned according to the provided order
//! (the order for the `model_path` field is `clap -> env -> config` and `env -> config -> default` for the `prediction_delay`). \
//! If none of them (including the default value) is found, the program returns error `MissingArgument`.
//!
//! **Note:** the default value is always assigned last.
//!
//! # Attributes documentation
//! For further understanding of project syntax and features, it is recommended to visit [Cookbook](__cookbook).
//!
//! # Complex example
//! ```no_run
//! use std::collections::HashMap;
//!
//! use config_manager::{config, ConfigInit};
//!
//! const SUFFIX: &str = "_env";
//! /// This doc will be included to CLI long_about.
//! #[derive(Debug)]
//! #[config(
//! clap(version, author, long_about),
//! env_prefix = "demo",
//! file(
//! format = "toml",
//! clap(long = "config", short = 'c', help = "path to configuration file"),
//! env = "demo_config",
//! default = "./config.toml"
//! )
//! )]
//! struct MethodConfig {
//! /// This doc will be included to CLI help.
//! #[source(clap(long, short, help))]
//! a: i32,
//! #[source(
//! env(init_from = &format!("b{}", SUFFIX)),
//! default = "abc"
//! )]
//! b: String,
//! #[source(config = "bpm")]
//! c: i32,
//! #[source(default = HashMap::new())]
//! d: HashMap<i32, String>,
//! }
//!
//! fn main() {
//! dbg!(MethodConfig::parse().unwrap());
//! }
//! ```
//! Run in [the repository](https://github.com/3xMike/config-manager)
//! ```console
//! cargo run --package examples --bin demo -- --config="examples/config.toml" --a=5
//! ```
//! Result must be:
//! ```console
//! [examples/src/demo.rs:34] &*CFG = MethodConfig {
//! a: 5,
//! b: "qwerty",
//! c: 165,
//! d: {},
//! }
//! ```
use HashMap;
use HashSet;
use fmt;
pub use config;
pub use Flatten;
/// Runtime initializing error.
/// Config trait that constructs an instance of itself from
/// environment, command line and configuration files. \
///
/// Don't implement the trait manually,
/// invoking `#[config]` is the only correct way to derive this trait.