pub struct Config(_);
Expand description
A configuration
Can be used as global (but don’t forget to initialize it somewhere!):
static CONFIG: ezconf::Config = ezconf::INIT;
Implementations§
source§impl Config
impl Config
sourcepub fn init<'a>(
&self,
sources: impl Iterator<Item = &'a Source<'a>>
) -> Result<bool, ()>
pub fn init<'a>(
&self,
sources: impl Iterator<Item = &'a Source<'a>>
) -> Result<bool, ()>
Initialize this configuration.
Can only be called once, further calls will return Err(())
.
sources
should be an iterator of possible config sources that are tried in
order. The first one to load successfully will be used. If none of them
load, an empty default config will be used and false
is returned.
Example
static CONFIG: ezconf::Config = ezconf::INIT;
fn main() {
CONFIG.init([ezconf::Source::File("config.toml")].iter()).unwrap();
}
sourcepub fn get<'a, T: Deserialize<'a> + Debug>(&self, path: &str) -> Option<T>
pub fn get<'a, T: Deserialize<'a> + Debug>(&self, path: &str) -> Option<T>
Retrieve a value from this config.
Returns the value or None
if it doesn’t exist.
Example
static CONFIG: ezconf::Config = ezconf::INIT;
fn main() {
CONFIG.init([ezconf::Source::File("tests/test.toml")].iter()).unwrap();
let v = CONFIG.get::<f32>("float.a").unwrap();
assert_eq!(v, 1.4142135);
}
sourcepub fn get_or<'a, T: Deserialize<'a> + Debug>(&self, path: &str, def: T) -> T
pub fn get_or<'a, T: Deserialize<'a> + Debug>(&self, path: &str, def: T) -> T
Retrieve a value from this config or return a default.
Returns the value or def
if it doesn’t exist.
Example
static CONFIG: ezconf::Config = ezconf::INIT;
fn main() {
CONFIG.init([ezconf::Source::File("tests/test.toml")].iter()).unwrap();
let v = CONFIG.get_or::<String>("string.foobar", "somestring".into());
assert_eq!(v, "somestring");
}