Skip to main content

Source

Enum Source 

Source
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) becomes Source::Path — loaded as a folder or a single file, detected at load time;
  • a (format, text) tuple becomes Source::String — the format is a Format, a format-id string ("jsonc"), or a CustomFormat;
  • a 1-tuple (value,) wraps any Serialize type as a typed override — the single-element tuple keeps it distinct from the conversions above (a blanket From<impl Serialize> would overlap them, since &str/String/tuples are all Serialize);
  • a 3-tuple (format, path, layout) or 4-tuple (format, path, sheet, layout) becomes Source::Table — one file of a table format (csv, excel, ods) read with the given TableLayout. 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).

Fields

§format: FormatKind
§content: String
§

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.

Fields

§path: PathBuf
§format: Format

Trait Implementations§

Source§

impl Clone for Source

Source§

fn clone(&self) -> Source

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Path> for Source

Source§

fn from(path: &Path) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Source

Source§

fn from(path: &str) -> Self

Converts to this type from the input type.
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.

Source§

fn from((id, path, layout): (&str, P, L)) -> Self

Converts to this type from the input type.
Source§

impl<P, S, L> From<(&str, P, S, L)> for Source
where P: Into<PathBuf>, S: Into<String>, L: Into<TableLayout>,

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§

fn from((id, path, sheet, layout): (&str, P, S, L)) -> Self

Converts to this type from the input type.
Source§

impl<F, S> From<(F, S)> for Source
where F: Into<FormatKind>, S: Into<String>,

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§

fn from((format, content): (F, S)) -> Self

Converts to this type from the input type.
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.

Source§

fn from((format, path, layout): (Format, P, L)) -> Self

Converts to this type from the input type.
Source§

impl<P, S, L> From<(Format, P, S, L)> for Source
where P: Into<PathBuf>, S: Into<String>, L: Into<TableLayout>,

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§

fn from((format, path, sheet, layout): (Format, P, S, L)) -> Self

Converts to this type from the input type.
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.

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));
Source§

fn from((value): (T,)) -> Self

Converts to this type from the input type.
Source§

impl From<PathBuf> for Source

Source§

fn from(path: PathBuf) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Source

Source§

fn from(path: String) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Source

Source§

fn eq(&self, other: &Source) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Source

Auto Trait Implementations§

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.