conflaguration
Typed configuration from environment variables, files, and fluent builders.
Quick start
use ;
let config: Config = init?;
Derive attributes
Struct-level #[settings(...)]
| Attribute | Effect |
|---|---|
prefix = "APP" |
Prepend APP_ to all env var keys |
resolve_with = "my_fn" |
Default custom parser for fields without typed defaults |
Field-level #[setting(...)]
| Attribute | Effect |
|---|---|
default |
Use T::default() as fallback when env var is missing |
default = value |
Typed fallback when env var is missing |
default_str = "str" |
String fallback, parsed at resolution time |
envs = "KEY" |
Override the auto-generated env var name |
envs = ["K1", "K2"] |
Cascade — first set key wins |
override |
Use exact key names, ignoring prefix |
resolve_with = "fn" |
Custom fn(&str) -> Result<T, E> parser |
nested |
Delegate to inner struct's Settings impl |
skip |
Use Default::default(), ignore env |
sensitive |
Mask value in ConfigDisplay output |
override_prefix |
Accumulate parent prefix for nested structs |
override_prefix = "X" |
Use explicit prefix for nested struct |
Conflicting combinations are rejected at compile time:
default+default_strskip+ any other attributenested+default/default_str/resolve_with/envs/override/sensitiveoverride_prefixwithoutnested
Custom parsing with resolve_with
Bypass FromEnvStr and parse raw env var strings with your own function:
Apply to all fields at the struct level:
Builder
Layer sources with explicit ordering — later sources override earlier ones:
let config: Config = builder
.defaults
.file
.env
.apply
.validate
.build?;
File loading
Requires a format feature: toml, json, or yaml.
let config: Config = from_file?;
let config: Config = from_file_then_env?;
Format is detected by lowercase file extension (.toml, .json, .yaml, .yml).
Uppercase or mixed-case extensions are rejected.
Validation
Derive Validate for automatic cascading into nested fields, or implement manually:
Display
Derive ConfigDisplay to render config with env var keys and sensitive masking:
// Output:
// port = 8080 (APP_PORT)
// token = *** (APP_TOKEN)
Features
| Feature | Effect |
|---|---|
derive |
Enable #[derive(Settings, Validate, ConfigDisplay)] |
toml |
TOML file parsing |
json |
JSON file parsing |
yaml |
YAML file parsing |