Skip to main content

resolve_field

Function resolve_field 

Source
pub fn resolve_field<T>(
    sources: &SourceChain<'_>,
    field: &'static str,
    key: &str,
    env_suffix: Option<&str>,
    allow_blank: bool,
    from_toml: impl Fn(&Value) -> Result<T, String>,
    from_env: impl Fn(&str) -> Result<T, String>,
) -> Result<Option<T>, EnvConfigError>
Expand description

Walks sources looking for a value for one field: within a source, its env var (if env_suffix is given) is checked before its TOML value; the first source to answer either wins. Returns Ok(None) when no source has the field at all, letting the caller apply a default.

By default (allow_blank is false), a source that answers with an empty-or-whitespace-only string, or an empty TOML array, is treated the same as that source not answering at all: resolve_field keeps walking the rest of the chain, trying the next source, and ultimately falls to the field’s default/default_fn only once every source has been asked. (An env var is always a string; a TOML value counts only when it is a string or an array — any other TOML shape, including an empty table, never counts as blank.) This fits nearly every field — a blank or empty override is essentially always a mistake or an unset placeholder, not a real value, and this holds whether a field has one source or several fallback tiers. Set allow_blank on the rare field where "" or [] is itself a meaningful, literal answer.

§Errors

Returns EnvConfigError::InvalidField when a present, non-blank value fails from_env/from_toml.