env_cfg
A rust derive macro library for loading configuration from environment variables.
Usage
use EnvConfig;
use Duration;
// Use no_prefix to disable the struct name prefix
// Use custom prefix instead of struct name
Derive Macro Attributes
Struct attributes:
#[env_cfg(no_prefix)]: Don't use struct name as prefix for field names#[env_cfg(prefix = "PREFIX")]: Use custom prefix instead of struct name
Field attributes:
- No attribute: Field name is prefixed with struct name and converted to UPPER_SNAKE_CASE for env var name
#[env_cfg(env = "VAR_NAME")]: Use custom environment variable name (overrides prefix)#[env_cfg(default = "value")]: Provide default value if env var not set#[env_cfg(skip)]: Skip this field (usesDefault::default())#[env_cfg(parse_with = "function_name")]: Use custom parser function (takesString, returnsT)#[env_cfg(nested)]: Treat field as nested EnvConfig struct (callsT::from_env())
Dependency
Add this to your Cargo.toml:
[]
= "0.2"
Features
trace
Enable the trace feature to emit tracing::trace! events when loading configuration:
[]
= { = "0.2.1", = ["trace"] }
When enabled, the generated from_env() method will emit a trace event with the loaded configuration struct. Note: This requires the struct to implement Debug.
Error variants
EnvConfigError::Missing(String): Environment variable is not set (Key)EnvConfigError::Parse(String, String): Failed to parse value (Key, Value)EnvConfigError::Nested(String, Box<EnvConfigError>): Error from a nested EnvConfig struct (Field type name, Underlying error)
License
Dual-licensed under Apache 2.0 and MIT terms.