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

#![allow(unknown_lints)]
#![feature(trace_macros)]

//! Configuration is gathered by building a `Source` and then merging that source into the
//! current state of the configuration.
//!
//! ```rust
//! // Add environment variables that begin with RUST_
//! config::merge(config::Environment::new("RUST")).unwrap();
//!
//! // Add 'Settings.toml'
//! config::merge(config::File::new("Settings", config::FileFormat::Toml)
//!     .required(false)).unwrap();
//!
//! // Add 'Settings.$(RUST_ENV).toml`
//! let name = format!("Settings.{}", config::get_str("env").unwrap_or("development".into()));
//! config::merge(config::File::new(&name, config::FileFormat::Toml)
//!     .required(false)).unwrap();
//! ```
//!
//! Note that in the above example the calls to `config::merge` could have
//! been re-ordered to influence the priority as each successive merge
//! is evaluated on top of the previous.
//!
//! Configuration values can be retrieved with a call to `config::get` and then
//! coerced into a type with `as_*`.
//!
//! ```rust
//! // Get 'debug' and coerce to a boolean
//! if let Some(value) = config::get("debug") {
//!     println!("{:?}", value.into_bool());
//! }
//!
//! // You can use a type suffix
//! println!("{:?}", config::get_bool("debug"));
//! println!("{:?}", config::get_str("debug"));
//! ```
//!
//! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for
//! more usage information.

#[macro_use]
extern crate nom;

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

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

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

mod value;
mod source;
mod file;
mod env;
mod path;
mod config;

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

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

pub use value::Value;

pub use config::Config;

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

// Get the global configuration instance
pub fn global() -> &'static mut RwLock<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().write().unwrap().merge(source)
}

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

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

pub fn get(key: &str) -> Option<Value> {
    // TODO(~): Should this panic! or return None with an error message?
    //          Make an issue if you think it should be an error message.
    global().read().unwrap().get(key)
}

pub fn get_str(key: &str) -> Option<String> {
    get(key).and_then(Value::into_str)
}

pub fn get_int(key: &str) -> Option<i64> {
    get(key).and_then(Value::into_int)
}

pub fn get_float(key: &str) -> Option<f64> {
    get(key).and_then(Value::into_float)
}

pub fn get_bool(key: &str) -> Option<bool> {
    get(key).and_then(Value::into_bool)
}

pub fn get_table(key: &str) -> Option<HashMap<String, Value>> {
    get(key).and_then(Value::into_table)
}

pub fn get_array(key: &str) -> Option<Vec<Value>> {
    get(key).and_then(Value::into_array)
}