Configulator
A simple configuration manager for Rust applications with derive macro support.
Features
- Supports configuration from multiple sources with clear precedence:
- Default values (lowest)
- Config files (any serde format via
serde_loader) - Environment variables
- CLI flags (highest)
#[derive(Config)]macro for declarative configuration structs- Any serde-compatible file format - YAML, TOML, JSON, with a one-liner
- Pluggable file format support - bring your own parser via [
FileLoader] - Nested struct support
Vec<T>list fields- Custom types - anything implementing
FromStr+ [Default] - Optional validation via the [
Validate] trait - Boolean CLI flags (
--debugsets true,--debug falsesets false)
Supported Types
- All primitive scalars (
i8–i64,u8–u64,f32,f64,bool,String) PathBufand any otherFromStr + Defaulttype- Custom enums (with
FromStrimplementation) Vec<T>for list values- Nested structs (must also derive
Config)
Usage
Add to your Cargo.toml:
[]
= "0.1"
[!NOTE] Because the configuration options are expressed as different cases (i.e.
http.hostin a config file would beHTTP__HOSTin environment variables), this library cannot be used for configurations that contain the same field name in different cases.
Derive Attributes
This library uses the #[configulator(...)] attribute with the following keys:
name- The config key name (defaults to the field name)default- Default value as a string literaldescription- Help text shown in CLI--helpoutput
// Field appears in config files, env vars, and CLI flags as "my-name".
my_name: String,
// Field has a description shown in CLI --help
my_name: String,
// Field has a default value of 1
my_name: u32,
Example
use ;
Configuration Sources
Config Files
Configulator is format-agnostic, pass any serde-compatible deserializer via
serde_loader, or implement the FileLoader trait for full control.
YAML, TOML, JSON, and any other serde format work out of the box.
Provide a list of paths to search. The first file found is used.
// YAML
.with_file
// TOML
.with_file
// JSON
.with_file
The CLI also accepts --config / -c to specify a config file path at runtime
(requires calling .with_file() first).
Environment Variables
Environment variables are formed as PREFIX + SEPARATOR + FIELD_NAME (uppercased, dashes become underscores).
.with_environment_variables
For example, a field named max-connections under a database parent with prefix MYAPP and separator __ would be MYAPP__DATABASE__MAX_CONNECTIONS.
CLI Flags
Nested fields use the separator to form flag names (e.g. --database.host).
.with_cli_flags
Boolean fields work as flags (--debug sets to true, --debug false sets to false). List fields can be repeated (--ports 80 --ports 443).
You can also provide a custom clap::Command to set the app name, version, or add your own flags:
.with_cli_command
.with_cli_flags
Validation
Implement the Validate trait and call .load() to validate after loading. Use .load_without_validation() to skip validation.
Feature Flags
Configulator uses feature flags to keep dependencies minimal. All features are enabled by default.
| Feature | Description | Dependencies |
|---|---|---|
file |
Config file loading (FileOptions, serde_loader, --config flag) |
serde |
cli |
CLI flag parsing via clap | clap |
env |
Environment variable loading | - |
To opt out of features you don't need:
[]
= { = "0.1", = false, = ["env"] }