@@ -96,13 +96,21 @@ impl Environment {
96
96
Self :: default ( )
97
97
}
98
98
99
+ /// Optional prefix that will limit access to the environment to only keys that
100
+ /// begin with the defined prefix.
101
+ ///
102
+ /// A prefix with a separator of `_` is tested to be present on each key before its considered
103
+ /// to be part of the source environment.
104
+ ///
105
+ /// For example, the key `CONFIG_DEBUG` would become `DEBUG` with a prefix of `config`.
99
106
pub fn with_prefix ( s : & str ) -> Self {
100
107
Self {
101
108
prefix : Some ( s. into ( ) ) ,
102
109
..Self :: default ( )
103
110
}
104
111
}
105
112
113
+ /// See [Environment::with_prefix]
106
114
pub fn prefix ( mut self , s : & str ) -> Self {
107
115
self . prefix = Some ( s. into ( ) ) ;
108
116
self
@@ -119,11 +127,15 @@ impl Environment {
119
127
self
120
128
}
121
129
130
+ /// Optional character sequence that separates the prefix from the rest of the key
122
131
pub fn prefix_separator ( mut self , s : & str ) -> Self {
123
132
self . prefix_separator = Some ( s. into ( ) ) ;
124
133
self
125
134
}
126
135
136
+ /// Optional character sequence that separates each key segment in an environment key pattern.
137
+ /// Consider a nested configuration such as `redis.password`, a separator of `_` would allow
138
+ /// an environment key of `REDIS_PASSWORD` to match.
127
139
pub fn separator ( mut self , s : & str ) -> Self {
128
140
self . separator = Some ( s. into ( ) ) ;
129
141
self
@@ -151,6 +163,7 @@ impl Environment {
151
163
self
152
164
}
153
165
166
+ /// Ignore empty env values (treat as unset).
154
167
pub fn ignore_empty ( mut self , ignore : bool ) -> Self {
155
168
self . ignore_empty = ignore;
156
169
self
@@ -163,11 +176,46 @@ impl Environment {
163
176
self
164
177
}
165
178
179
+ // Preserve the prefix while parsing
166
180
pub fn keep_prefix ( mut self , keep : bool ) -> Self {
167
181
self . keep_prefix = keep;
168
182
self
169
183
}
170
184
185
+ /// Alternate source for the environment. This can be used when you want to test your own code
186
+ /// using this source, without the need to change the actual system environment variables.
187
+ ///
188
+ /// ## Example
189
+ ///
190
+ /// ```rust
191
+ /// # use config::{Environment, Config};
192
+ /// # use serde::Deserialize;
193
+ /// # use std::collections::HashMap;
194
+ /// # use std::convert::TryInto;
195
+ /// #
196
+ /// #[test]
197
+ /// fn test_config() -> Result<(), config::ConfigError> {
198
+ /// #[derive(Clone, Debug, Deserialize)]
199
+ /// struct MyConfig {
200
+ /// pub my_string: String,
201
+ /// }
202
+ ///
203
+ /// let source = Environment::default()
204
+ /// .source(Some({
205
+ /// let mut env = HashMap::new();
206
+ /// env.insert("MY_STRING".into(), "my-value".into());
207
+ /// env
208
+ /// }));
209
+ ///
210
+ /// let config: MyConfig = Config::builder()
211
+ /// .add_source(source)
212
+ /// .build()?
213
+ /// .try_into()?;
214
+ /// assert_eq!(config.my_string, "my-value");
215
+ ///
216
+ /// Ok(())
217
+ /// }
218
+ /// ```
171
219
pub fn source ( mut self , source : Option < Map < String , String > > ) -> Self {
172
220
self . source = source;
173
221
self
0 commit comments