Skip to main content

Config

Struct Config 

Source
pub struct Config<S = DefaultStore> { /* private fields */ }
Expand description

Thread-safe, immutable configuration container.

§Design notes

  • Configuration is parsed once and then treated as read-only.
  • Internally, values are flattened into dot-separated keys: database.host, runners[0].name, etc.
  • The backing store is wrapped in an Arc to make cloning cheap and sharing across threads trivial.
use tomldir::Config;
let cfg = Config::from_file("config.toml").unwrap();

You can also use a custom store like:

use std::collections::HashMap;

use rustc_hash::FxBuildHasher;
use tomldir::{Config, Value};

let cfg = Config::<HashMap<String, Value, FxBuildHasher>>::from_file_with("config.toml");

Implementations§

Source§

impl Config

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>

Load configuration from a TOML file using the default store.

§Errors

Returns an error if the file cannot be read or contains invalid TOML.

Source

pub fn from_toml(content: &str) -> Result<Self>

Load configuration from a TOML string using the default store.

§Errors

Returns an error if the string contains invalid TOML.

Source§

impl<S> Config<S>
where S: Store,

Source

pub fn from_file_with<P: AsRef<Path>>(path: P) -> Result<Self>

Load configuration from a TOML file using a custom store.

§Errors

Returns an error if the file cannot be read or contains invalid TOML.

Source

pub fn from_toml_with(content: &str) -> Result<Self>

Load configuration from a TOML string using a custom store.

§Errors

Returns an error if the string contains invalid TOML.

Source

pub fn shared(&self) -> Self

Returns a new instance sharing the same underlying store.

Source

pub fn get(&self, key: &str) -> Option<&Value>

Retrieve a raw TOML value by flattened key.

Source

pub fn get_string(&self, key: &str) -> Option<&str>

Helper to get a String value.

Source

pub fn get_int(&self, key: &str) -> Option<i64>

Helper to get an integer value.

Source

pub fn get_float(&self, key: &str) -> Option<f64>

Helper to get a float value.

Source

pub fn get_bool(&self, key: &str) -> Option<bool>

Helper to get a boolean value.

Source

pub fn flatten(&self) -> impl Iterator<Item = (String, String)> + '_

Flatten all values into string form.

Strings preserve their raw content. Non-strings use TOML’s display representation.

Source

pub fn flatten_into<C>(&self) -> C
where C: FromIterator<(String, String)>,

Returns a flattened collection of the configuration, where all values are converted to strings.

The return type C must implement FromIterator<(String, String)>.

Trait Implementations§

Source§

impl<S> Clone for Config<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Config<S>

§

impl<S> RefUnwindSafe for Config<S>
where S: RefUnwindSafe,

§

impl<S> Send for Config<S>
where S: Sync + Send,

§

impl<S> Sync for Config<S>
where S: Sync + Send,

§

impl<S> Unpin for Config<S>

§

impl<S> UnwindSafe for Config<S>
where S: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.