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
Arcto 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
impl Config
Source§impl<S> Config<S>where
S: Store,
impl<S> Config<S>where
S: Store,
Sourcepub fn from_file_with<P: AsRef<Path>>(path: P) -> Result<Self>
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.
Sourcepub fn from_toml_with(content: &str) -> Result<Self>
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.
Returns a new instance sharing the same underlying store.
Sourcepub fn get_string(&self, key: &str) -> Option<&str>
pub fn get_string(&self, key: &str) -> Option<&str>
Helper to get a String value.
Sourcepub fn flatten(&self) -> impl Iterator<Item = (String, String)> + '_
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.
Sourcepub fn flatten_into<C>(&self) -> C
pub fn flatten_into<C>(&self) -> C
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§
Auto Trait Implementations§
impl<S> Freeze for Config<S>
impl<S> RefUnwindSafe for Config<S>where
S: RefUnwindSafe,
impl<S> Send for Config<S>
impl<S> Sync for Config<S>
impl<S> Unpin for Config<S>
impl<S> UnwindSafe for Config<S>where
S: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more