pub struct Loader {
pub options: Options,
}Expand description
Loads and merges the config sources named by its Options.
Fields§
§options: OptionsImplementations§
Source§impl Loader
impl Loader
Sourcepub fn new(options: Options) -> Self
pub fn new(options: Options) -> Self
Everything — sources included — comes in through Options;
there is no separate builder step.
Sourcepub fn load<T: DeserializeOwned>(&self) -> Result<T>
pub fn load<T: DeserializeOwned>(&self) -> Result<T>
Load and merge all sources, deserializing into T.
Value implements Deserialize, so T = Value gives dynamic
access and any other serde type gives typed config.
Sourcepub fn trace(&self) -> Result<TracedValue>
pub fn trace(&self) -> Result<TracedValue>
Like Loader::load, but keeps per-leaf provenance — see
TracedValue. This powers the CLI --trace mode; source labels
are a debug/testing aid, not config data.
use c4::{CustomFormat, Loader, TracedValue};
// a custom-format string source keeps this example runnable
// under every feature combination (`Loader::default()` would
// read ./config instead)
let kv = CustomFormat::new("kv", ["kv"], |text, path, options| {
let rows = text
.lines()
.map(|line| line.split('=').map(str::to_owned).collect())
.collect();
c4::parse_table(rows, &c4::TableLayout::Kv, path, options)
});
let traced = Loader::new(c4::Options {
sources: vec![(kv, "port=8080").into()],
..c4::Options::default()
})
.trace()?;
let TracedValue::Object(root) = &traced else { unreachable!() };
let TracedValue::Leaf { value, source } = &root["port"] else { unreachable!() };
assert_eq!(value.as_u64(), Some(8080));
assert_eq!(source, &c4::SourceRef::String(0)); // "string:0" in tracesTrait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Loader
impl !UnwindSafe for Loader
impl Freeze for Loader
impl Send for Loader
impl Sync for Loader
impl Unpin for Loader
impl UnsafeUnpin for Loader
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