Expand description
Declarative, attribute-driven per-environment configuration: EnvConfig,
ConfigSource, and the #[derive(EnvConfig)] macro’s runtime support.
Declarative, attribute-driven per-environment configuration.
An EnvConfig struct describes, per field, where to find its value —
a TOML key, an opt-in environment-variable suffix, a literal or computed
default, and (when the defaults don’t fit) a custom conversion function
for either raw form. #[derive(EnvConfig)] generates the wiring; see
docs/environments.md for the attribute grammar and
crate::environments::Environments::resolve for the common way to
build a SourceChain and assemble a struct from it.
§Why toml::Value, and why it’s exposed directly
Every source except an environment variable represents its values as
toml::Value/toml::Table — not a cli_engine-owned wrapper type.
Deliberate, not incidental:
- The file layer (
environments.toml) genuinely is TOML; parsing it already produces atoml::Table. - Compiled-in and in-memory sources (
crate::environments::EnvTable,ValueSource) use that same representation so they merge with the file layer key-by-key with no translation between “code-supplied” and “file-supplied” shapes, and so a field’s default conversion (default_from_toml) can lean ontoml::Value’s ownserde::Deserializeimpl — a TOML array becomes aVec<String>natively, a TOML table becomes a nested struct, with no per-field stringly-typed detour. - An environment variable is the one genuine exception: it’s always a
plain
String(an OS-level constraint, not a design choice), soConfigSource::env_varreturnsOption<String>, neverOption<toml::Value>, and a field’sfrom_envconversion is always a separate function from itsfrom_tomlconversion.
Using toml::Value/toml::Table directly, instead of hiding them behind
a cli_engine-owned newtype, means they’re part of this crate’s public
API — and a consumer whose toml::Value came from its own direct
dependency, rather than this re-export, would need that dependency on
the same major version as this crate’s, or the two crates’ toml::Value
types are different, incompatible types despite sharing a name.
#[derive(EnvConfig)]-generated code
always goes through crate::env_config::toml rather than a bare
toml:: path for exactly this reason, so the common case (no custom
from_toml/to_toml) needs no direct toml dependency in the consumer
at all. A consumer writing a custom from_toml/to_toml function or
calling ValueSource::with directly should do the same —
cli_engine::env_config::toml::Value, not its own toml dependency’s
toml::Value — to get the same guarantee.
Re-exports§
pub use toml;
Structs§
- EnvSource
- One environment’s source: its name and its merged TOML table (compiled-in
table overlaid by the
environments.tomlfile table). Purely a TOML lookup; env-var overrides are handled by a separateEnvVarSourcepushed alongside it — seecrate::environments::Environments::resolve. - EnvVar
Source - A source that only ever answers environment-variable lookups, under a fixed prefix — the app id for the common case.
- Source
Chain - An ordered chain of
ConfigSources. Assembly walks the chain in order; within one source its env var is checked before its TOML value; the first source to answer either wins for that field. Build one withSourceChain::newandSourceChain::push, then pass it toEnvConfig::assemble. - Value
Source - A source backed by values a consumer already has in hand — for example a provider’s own constructor arguments, used as a last-resort fallback tier.
Enums§
- EnvConfig
Error - An error assembling an
EnvConfigstruct.
Traits§
- Config
Source - Something a field’s assembly instructions can be checked against: “do you
have a TOML-shaped value for this key” and “do you have a string value for
an env var with this suffix.”
EnvConfig::assemblewalks a wholeSourceChainof these, in priority order, so more than one kind of fallback source can contribute to the same struct. - EnvConfig
- A struct that can be assembled from a
SourceChain— implemented by#[derive(EnvConfig)].
Functions§
- default_
from_ env - Default string-to-
Tconversion used when a field has nofrom_envattribute:Tmust implementstd::str::FromStr. - default_
from_ toml - Default TOML-to-
Tconversion used when a field has nofrom_tomlattribute:Tmust beserde::de::DeserializeOwned. - resolve_
field - Walks
sourceslooking for a value for one field: within a source, its env var (ifenv_suffixis given) is checked before its TOML value; the first source to answer either wins. ReturnsOk(None)when no source has the field at all, letting the caller apply a default.