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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![feature(drop_types_in_const)]
#![allow(unknown_lints)]
#![feature(trace_macros)]
#[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::borrow::Cow;
pub use source::{Source, SourceBuilder};
pub use file::{File, FileFormat};
pub use env::Environment;
pub use value::Value;
pub use config::Config;
static mut CONFIG: Option<RwLock<Config>> = None;
static CONFIG_INIT: Once = ONCE_INIT;
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<'a>(key: &str) -> Option<&'a Value> {
let r = global().read().unwrap();
let c = &*r;
unsafe {
let c: &'static Config = std::mem::transmute(c);
c.get(key)
}
}
pub fn get_str<'a>(key: &str) -> Option<Cow<'a, str>> {
let r = global().read().unwrap();
unsafe {
let c: &'static Config = std::mem::transmute(&*r);
c.get_str(key)
}
}
pub fn get_int(key: &str) -> Option<i64> {
let r = global().read().unwrap();
unsafe {
let c: &'static Config = std::mem::transmute(&*r);
c.get_int(key)
}
}
pub fn get_float(key: &str) -> Option<f64> {
let r = global().read().unwrap();
unsafe {
let c: &'static Config = std::mem::transmute(&*r);
c.get_float(key)
}
}
pub fn get_bool(key: &str) -> Option<bool> {
let r = global().read().unwrap();
unsafe {
let c: &'static Config = std::mem::transmute(&*r);
c.get_bool(key)
}
}