pub struct Config { /* private fields */ }
Expand description
The top-level Config
type that represents a configuration
Implementations§
Source§impl Config
impl Config
Sourcepub fn new(sl: SettingsList) -> Config
pub fn new(sl: SettingsList) -> Config
Creates a new wrapper Config
to hold a SettingsList
Sourcepub fn lookup(&self, path: &str) -> Option<&Value>
pub fn lookup(&self, path: &str) -> Option<&Value>
Looks up a value in a configuration. A path is a dot-separated list of settings
describing the path of the desired value in the configuration.
Returns None
if the path is invalid. A path is invalid if it is not syntactically
well-formed, if it attempts to index an array or list beyond the limit, or if it
includes an unknown setting.
§Examples
Suppose we have loaded a configuration that consists of:
my_string = "hello";
a_list = ([1, 2, 3], true, { x = 4; }, "good bye");
Then, the path to retrieve "hello"
is my_string
.
The path to retrieve true
inside a_list
would be a_list.[1]
.
The path to retrieve the setting x
inside a_list
would be alist.[2].x
.
Here’s a small demonstration:
use config::reader::from_str;
let my_conf = from_str("my_string = \"hello\"; a_list = ([1, 2, 3], true, { x = 4; }, \"good_bye\");").unwrap();
let my_str_value = my_conf.lookup("my_string");
assert!(my_str_value.is_some());
let my_boolean_value = my_conf.lookup("a_list.[1]");
assert!(my_boolean_value.is_some());
let my_x_setting = my_conf.lookup("a_list.[2].x");
assert!(my_x_setting.is_some());
Sourcepub fn lookup_boolean(&self, path: &str) -> Option<bool>
pub fn lookup_boolean(&self, path: &str) -> Option<bool>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, bool
.
Sourcepub fn lookup_integer32(&self, path: &str) -> Option<i32>
pub fn lookup_integer32(&self, path: &str) -> Option<i32>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, i32
.
Sourcepub fn lookup_integer64(&self, path: &str) -> Option<i64>
pub fn lookup_integer64(&self, path: &str) -> Option<i64>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, i64
.
Sourcepub fn lookup_floating32(&self, path: &str) -> Option<f32>
pub fn lookup_floating32(&self, path: &str) -> Option<f32>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, f32
.
Sourcepub fn lookup_floating64(&self, path: &str) -> Option<f64>
pub fn lookup_floating64(&self, path: &str) -> Option<f64>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, f64
.
Sourcepub fn lookup_str(&self, path: &str) -> Option<&str>
pub fn lookup_str(&self, path: &str) -> Option<&str>
A convenient wrapper around lookup()
that unwraps the underlying primitive
type of a generic Value
.
Returns None
in the same way lookup()
does; or if the underlying Value
type does not match with the requested type - in this case, String
.
Sourcepub fn lookup_boolean_or(&self, path: &str, default: bool) -> bool
pub fn lookup_boolean_or(&self, path: &str, default: bool) -> bool
A convenient wrapper around lookup_boolean()
that unwraps the underlying primitive
type of a boolean Value
.
If either of lookup_boolean()
or lookup
return None
,
then the user-provided default value is returned.
Sourcepub fn lookup_integer32_or(&self, path: &str, default: i32) -> i32
pub fn lookup_integer32_or(&self, path: &str, default: i32) -> i32
A convenient wrapper around lookup_integer32()
that unwraps the underlying primitive
type of an integer32 Value
.
If either of lookup_integer32()
or lookup
return None
,
then the user-provided default value is returned.
Sourcepub fn lookup_integer64_or(&self, path: &str, default: i64) -> i64
pub fn lookup_integer64_or(&self, path: &str, default: i64) -> i64
A convenient wrapper around lookup_integer64()
that unwraps the underlying primitive
type of an integer64 Value
.
If either of lookup_integer64()
or lookup
return None
,
then the user-provided default value is returned.
Sourcepub fn lookup_floating32_or(&self, path: &str, default: f32) -> f32
pub fn lookup_floating32_or(&self, path: &str, default: f32) -> f32
A convenient wrapper around lookup_floating32()
that unwraps the underlying primitive
type of an floating32 Value
.
If either of lookup_floating32()
or lookup
return None
,
then the user-provided default value is returned.
Sourcepub fn lookup_floating64_or(&self, path: &str, default: f64) -> f64
pub fn lookup_floating64_or(&self, path: &str, default: f64) -> f64
A convenient wrapper around lookup_floating64()
that unwraps the underlying primitive
type of an floating64 Value
. If either of lookup_floating64()
or lookup
return None
,
then the user-provided default value is returned.
Sourcepub fn lookup_str_or<'a>(&'a self, path: &str, default: &'a str) -> &'a str
pub fn lookup_str_or<'a>(&'a self, path: &str, default: &'a str) -> &'a str
A convenient wrapper around lookup_str()
that unwraps the underlying primitive
type of a string Value
.
If either of lookup_str()
or lookup
return None
,
then the user-provided default value is returned.