Struct Config

Source
pub struct Config { /* private fields */ }
Expand description

The top-level Config type that represents a configuration

Implementations§

Source§

impl Config

Source

pub fn new(sl: SettingsList) -> Config

Creates a new wrapper Config to hold a SettingsList

Source

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());
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Config

Source§

type Err = ConfigError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Config) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Config

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.