blob: 612b97dcb52cd69da90cecf7fed8364ef1bb83c4 (
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
|
use std::env;
use std::error::Error;
use source;
use value::Value;
#[derive(Clone)]
pub struct Environment {
/// Optional prefix that would restrict environment consideration
/// to only variables which begin with that prefix.
prefix: Option<String>,
}
impl Environment {
pub fn new<'a, T>(prefix: T) -> Environment
where T: Into<Option<&'a str>>
{
Environment { prefix: prefix.into().map(String::from) }
}
}
impl source::SourceBuilder for Environment {
fn build(&self) -> Result<Box<source::Source>, Box<Error>> {
Ok(Box::new(self.clone()))
}
}
impl source::Source for Environment {
fn get(&self, key: &str) -> Option<Value> {
let mut env_key = String::new();
// Apply prefix
if let Some(ref prefix) = self.prefix {
env_key.push_str(prefix);
env_key.push('_');
}
env_key.push_str(&key.to_uppercase());
// Attempt to retreive environment variable and coerce into a Value
env::var(env_key.clone()).ok().map(Value::from)
}
}
|