pub enum Source {
Path(PathBuf),
String {
format: FormatKind,
content: String,
},
Value(Result<Value, String>),
Table {
path: PathBuf,
format: Format,
sheet: Option<String>,
layout: TableLayout,
},
}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); - a 3-tuple
(format, path, layout)or 4-tuple(format, path, sheet, layout)becomesSource::Table— one file of a table format (csv, excel, ods) read with the givenTableLayout. The 3-tuple’s third element is always the layout (it suits csv — the file is the table); naming a spreadsheet sheet is always the 4-tuple.
So one Options.sources vec![…] can mix all of these 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.
Table
One file of a table format (csv, excel, ods) read with an
explicit TableLayout (from a (format, path, layout) /
(format, path, sheet, layout) tuple). A non-file path and a
non-table format are Error::Parse (the
path error hints that in-code text belongs in a string source). sheet (spreadsheets only;
naming one on csv is an error) reads exactly that sheet —
bypassing the ignore_commented_sheets / ignore_hidden_sheets
filters, erroring when it is missing — and merges its value
under the sheet name as key, so several sources can read
different sheets of one workbook without clobbering each other.
Trait Implementations§
Source§impl<P: Into<PathBuf>, L: Into<TableLayout>> From<(&str, P, L)> for Source
Like the (Format, path, layout) tuple, with the format named by its
id string. Panics on an unknown format id — this is config-time code.
impl<P: Into<PathBuf>, L: Into<TableLayout>> From<(&str, P, L)> for Source
Like the (Format, path, layout) tuple, with the format named by its
id string. Panics on an unknown format id — this is config-time code.
Source§impl<P, S, L> From<(&str, P, S, L)> for Source
Like the (Format, path, sheet, layout) tuple, with the format named
by its id string. Panics on an unknown format id — this is config-time
code.
impl<P, S, L> From<(&str, P, S, L)> for Source
Like the (Format, path, sheet, layout) tuple, with the format named
by its id string. Panics on an unknown format id — this is config-time
code.
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<P: Into<PathBuf>, L: Into<TableLayout>> From<(Format, P, L)> for Source
A (format, path, layout) 3-tuple is a table source: one file, read
with that TableLayout (a TableLayout, a
CustomLayout, or a layout-id &str —
"kv"/"kvf", "db"; an unknown id panics, this is config-time
code). The third element is always the layout — the 3-tuple suits
csv, where the file is the table. To name a spreadsheet sheet use the
4-tuple, which names the layout explicitly too.
impl<P: Into<PathBuf>, L: Into<TableLayout>> From<(Format, P, L)> for Source
A (format, path, layout) 3-tuple is a table source: one file, read
with that TableLayout (a TableLayout, a
CustomLayout, or a layout-id &str —
"kv"/"kvf", "db"; an unknown id panics, this is config-time
code). The third element is always the layout — the 3-tuple suits
csv, where the file is the table. To name a spreadsheet sheet use the
4-tuple, which names the layout explicitly too.
Source§impl<P, S, L> From<(Format, P, S, L)> for Source
A (format, path, sheet, layout) 4-tuple is a table source that reads
exactly one sheet of a workbook (skipping the sheet-ignore filters)
and merges it under the sheet name as key.
impl<P, S, L> From<(Format, P, S, L)> for Source
A (format, path, sheet, layout) 4-tuple is a table source that reads
exactly one sheet of a workbook (skipping the sheet-ignore filters)
and merges it under the sheet name as key.
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));