summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 2192df14e79653228caa2b3783471b9970e1063c (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
#![feature(try_from)]
#![feature(drop_types_in_const)]

extern crate toml;

mod value;
mod source;
mod config;

use std::error::Error;
use std::convert::TryFrom;
use std::sync::{Once, ONCE_INIT};

pub use source::Source;
pub use source::File;

use value::Value;

pub use config::Config;

// Global configuration
static mut CONFIG: Option<Config> = None;
static CONFIG_INIT: Once = ONCE_INIT;

// Get the global configuration instance
fn global() -> Option<&'static mut Config> {
    unsafe {
        CONFIG_INIT.call_once(|| {
            CONFIG = Some(Default::default());
        });

        // TODO(@rust): One-line this if possible
        if let Some(ref mut c) = CONFIG {
            return Some(c);
        }

        None
    }
}

pub fn merge<T>(source: T) -> Result<(), Box<Error>>
    where T: Source
{
    global().unwrap().merge(source)
}

pub fn set_env_prefix(prefix: &str) {
    global().unwrap().set_env_prefix(prefix)
}

pub fn set_default<T>(key: &str, value: T)
    where T: Into<Value>
{
    global().unwrap().set_default(key, value)
}

pub fn set<T>(key: &str, value: T)
    where T: Into<Value>
{
    global().unwrap().set(key, value)
}

pub fn get<'a, T>(key: &str) -> Option<T>
    where T: TryFrom<&'a mut Value>,
          T: Default
{
    global().unwrap().get(key)
}

pub fn get_str<'a>(key: &str) -> Option<&'a str> {
    global().unwrap().get_str(key)
}

pub fn get_int(key: &str) -> Option<i64> {
    global().unwrap().get_int(key)
}

pub fn get_float(key: &str) -> Option<f64> {
    global().unwrap().get_float(key)
}

pub fn get_bool(key: &str) -> Option<bool> {
    global().unwrap().get_bool(key)
}