#[derive(Config)]
{
// Attributes available to this derive:
#[confroid]
}
Expand description
Derive confroid::FromEnv for a named struct.
Config maps struct fields to environment variables. Field names are
converted to SCREAMING_SNAKE_CASE, and nested segments are joined with
__.
§Example
#[derive(confroid::Config)]
#[confroid(prefix = "GUPPY")]
struct Config {
#[confroid(default = 8080)]
port: u16,
database: Database,
}
#[derive(confroid::Config)]
struct Database {
#[confroid(name = "URL")]
connection_string: String,
}
// Reads `GUPPY__PORT` and `GUPPY__DATABASE__URL`.
let config: Config = confroid::from_env()?;§Container attributes
Container attributes are placed on the struct below #[derive(Config)]:
prefix = "..."— prepend a segment to every environment variable represented by the struct. For example,#[confroid(prefix = "GUPPY")]makes a field namedportread fromGUPPY__PORT. Prefixes compose when a prefixed struct is nested inside another config.
§Field attributes
Field attributes customize naming, fallback values, parsing, and generated configuration documentation:
name = "..."— override the field’s environment-variable segment. On a nested field, the override applies to the entire nested prefix.default— useDefault::default()when the variable is absent.default = expression— use an explicit fallback when the variable is absent. String literals and compatible expressions are converted withInto::into. A present value that fails to parse still returns an error.example = expression— attach an example value forconfroid::env_exampleandconfroid::markdown_tablewhen thedocsfeature is enabled. It does not affect parsing.auto_vec— parse aVec<T>from one delimited variable rather than indexed variables such asNAMES__0andNAMES__1.auto_vec_delimiter = "..."— set the delimiter used byauto_vec; the default is,.humantime— parse astd::time::DurationorOption<std::time::Duration>using values such as30sor1h 15m. This requires confroid’shumantimefeature.
Attributes can be combined:
#[derive(confroid::Config)]
struct Config {
#[confroid(
name = "ALLOWED_HOSTS",
auto_vec,
auto_vec_delimiter = ";",
default
)]
hosts: Vec<String>,
}§Error reporting
All fields are evaluated before a failed read returns. Independent failures
are flattened into confroid::ConfroidError::Multiple in field declaration
order, allowing all missing and invalid variables to be fixed in one pass.
A single failure is returned as its leaf variant.
Parser failures are retained as boxed error sources rather than converted to
strings. Consequently, custom scalar parse errors must implement
std::error::Error + Send + Sync + 'static.
§Supported structs
The derive supports structs with named fields. Each field’s type must
implement confroid::FromEnv. Optional values, nested configs, vectors, and
hash maps use their FromEnv implementations to determine presence and
deserialize their children.