summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 99a6da3a775fd43375cf0d7500f2613a4d656421 (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
#![feature(drop_types_in_const)]
#![allow(unknown_lints)]

#[cfg(feature = "toml")]
extern crate toml;

#[cfg(feature = "json")]
extern crate serde_json;

mod value;
mod source;
mod file;
mod config;

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

pub use source::{Source, SourceBuilder};
pub use file::{File, FileFormat};

pub 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() -> &'static mut Config {
    unsafe {
        CONFIG_INIT.call_once(|| {
            CONFIG = Some(Default::default());
        });

        CONFIG.as_mut().unwrap()
    }
}

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

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

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

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

pub fn get(key: &str) -> Option<Value> {
    global().get(key)
}

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

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

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

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