Forgeconf loads configuration files into strongly typed Rust structs. Derive Config, point it at one or more files, and get merged, validated values back with every error reported at once, including its key path and the file line and column it came from.
Forgeconf has no third-party runtime dependencies: the TOML, JSON and YAML parsers are in-house, and the derive macro needs only syn, quote and proc-macro2.
[dependencies]
forgeconf = "1"The minimum supported Rust version is 1.85.
use forgeconf::Config;
#[derive(Debug, Config)]
#[config(file = "config/app.toml")]
struct AppConfig {
#[config(default = 8080)]
port: u16,
database_url: String,
}
fn main() -> Result<(), forgeconf::ConfigError> {
let cfg = AppConfig::loader().load()?;
println!("listening on {}", cfg.port);
println!("db url: {}", cfg.database_url);
Ok(())
}AppConfig::loader() returns a Loader<AppConfig> preloaded with the declared files. Returning Result<(), ConfigError> from main prints the full error report:
2 configuration errors:
- port: expected u16, found "abc" (config/app.toml:1:8)
- database_url: missing value
- Layered sources. Files, then environment variables, then command-line flags, each overriding the one before. Variable names, flags and
--helpare derived from the struct. SeeLoader. - Derive attributes.
default,rename,alias,flatten,validate,sensitive,deprecated,deny_unknown_fieldsand more, on structs, fields and enums. See#[derive(Config)]. - Types. Strings, numbers,
bool(true/false/1/0/yes/no/on/off),Duration(30,250ms,1h30m), IP and socket addresses,PathBuf,Option,Vec, sets, maps, arrays, and your own structs and enums. ImplementFromNodefor anything else. - Validators. Any
fn(&T) -> Result<(), ConfigError>, plusnon_empty,len,rangeandone_offromforgeconf::validators. - Errors. One
ConfigErrorholds every failure, each with its kind, key path (servers[2].host) and origin. - Writing. With the
writefeature,forgeconf::template::<AppConfig>(FileFormat::Toml)returns a starter file: every key commented out with its doc comment, default and aRequired.marker. Add#[derive(ToNode)]andforgeconf::save(&cfg, "config/app.toml")writes a value back, skippingsensitivefields unless you opt in.forgeconf::setchanges or adds one value and leaves comments and layout alone, andforgeconf::migraterenames keys markedrenamed_from. - Tooling and coding agents. With the
inspectfeature a program can describe its own configuration:schemaandjson_schemalist every key,Loader::explainshows where each value came from, andConfigError::to_jsonreports errors with positions and fixes. The generated flags--config-schema,--config-explainand--config-checkexpose the same from the command line. Fields markedguardedare refused bysetandsaveunless the caller opts in.
| feature | default | description |
|---|---|---|
toml |
yes | TOML 1.0 parser, .toml files |
cli |
yes | Command-line flags with generated --help |
json |
no | JSON parser, .json files |
yaml |
no | YAML (common subset) parser, .yaml / .yml files |
dotenv |
no | Loader::dotenv(path) reads a .env file |
watch |
no | Loader::watch(interval) polls the files and reloads them |
write |
no | template, to_string, save, set and migrate write config files |
inspect |
no | schema, json_schema, explain, JSON errors and the --config-* flags |
secrets |
no | Loader::secret_refs() resolves env:NAME and file:PATH in sensitive fields |
rename-warnings |
no | Loading warns when a file still uses a key listed under renamed_from |
Loading a typical file plus environment variables takes a few microseconds. benchmarks/ compares speed, build time, binary size and crate count with config, figment, confique, serde and clap, and lists the release-profile settings that help.
Runnable examples live in forgeconf/examples.
Forgeconf is released under the MIT License.