pub enum Source {
Path(PathBuf),
String {
format: FormatKind,
content: String,
},
Value(Result<Value, String>),
}Expand description
One config source. Sources merge in order — later overrides earlier.
Construct sources by conversion, not by hand:
- a path-like value (
&str,String,&Path,PathBuf) becomesSource::Path— loaded as a folder or a single file, detected at load time; - a
(format, text)tuple becomesSource::String— the format is aFormat, a format-id string ("jsonc"), or aCustomFormat; - a 1-tuple
(value,)wraps anySerializetype as a typed override — the single-element tuple keeps it distinct from the conversions above (a blanketFrom<impl Serialize>would overlap them, since&str/String/tuples are allSerialize).
So one Options.sources vec![…] can mix all three kinds.
Variants§
Path(PathBuf)
A filesystem path — a folder whose files deep-merge, or a single
file. Which one is decided at load time (is_dir / is_file); a
path that is neither is Error::NotFound.
String
An in-code string; its format may be a built-in or a
CustomFormat (no formats registration
needed — the source names its parser directly).
Value(Result<Value, String>)
An in-code serde value (from a 1-tuple (value,) source) — already
converted to a Value, or the serialization error to report at
load time.
Trait Implementations§
Source§impl<F, S> From<(F, S)> for Source
A (format, text) tuple is an in-code string source. format is a
Format, a format-id &str, or a
CustomFormat; text is anything Into<String>.
impl<F, S> From<(F, S)> for Source
A (format, text) tuple is an in-code string source. format is a
Format, a format-id &str, or a
CustomFormat; text is anything Into<String>.
Source§impl<T: Serialize> From<(T,)> for Source
A 1-tuple (value,) is a typed serde override — the value is
serialized through the crate serializer (no format parser; works with
any #[derive(Serialize)] type). A serialization failure (e.g. a map
with non-string keys) surfaces at load time as
Error::Parse; a Null root (e.g. None)
contributes nothing.
impl<T: Serialize> From<(T,)> for Source
A 1-tuple (value,) is a typed serde override — the value is
serialized through the crate serializer (no format parser; works with
any #[derive(Serialize)] type). A serialization failure (e.g. a map
with non-string keys) surfaces at load time as
Error::Parse; a Null root (e.g. None)
contributes nothing.
The single-element tuple is what keeps this distinct: &str, String,
tuples and paths are all themselves Serialize, so a blanket
From<impl Serialize> would overlap the conversions above — but a
1-tuple (T,) is a type of its own. Mind the trailing comma:
#[derive(serde::Serialize)]
struct Overrides {
debug: bool,
}
let value: c4::Value = c4::Loader::new(c4::Options {
sources: vec![(Overrides { debug: true },).into()],
..c4::Options::default()
})
.load()?;
assert_eq!(value["debug"].as_bool(), Some(true));