Registry Mirror
Skip to content

Latest commit

 

History

174 Commits

Folders and files

Repository files navigation

Forgeconf

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.

Install

[dependencies]
forgeconf = "1"

The minimum supported Rust version is 1.85.

Quick start

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

What it does

  • Layered sources. Files, then environment variables, then command-line flags, each overriding the one before. Variable names, flags and --help are derived from the struct. See Loader.
  • Derive attributes. default, rename, alias, flatten, validate, sensitive, deprecated, deny_unknown_fields and 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. Implement FromNode for anything else.
  • Validators. Any fn(&T) -> Result<(), ConfigError>, plus non_empty, len, range and one_of from forgeconf::validators.
  • Errors. One ConfigError holds every failure, each with its kind, key path (servers[2].host) and origin.
  • Writing. With the write feature, forgeconf::template::<AppConfig>(FileFormat::Toml) returns a starter file: every key commented out with its doc comment, default and a Required. marker. Add #[derive(ToNode)] and forgeconf::save(&cfg, "config/app.toml") writes a value back, skipping sensitive fields unless you opt in. forgeconf::set changes or adds one value and leaves comments and layout alone, and forgeconf::migrate renames keys marked renamed_from.
  • Tooling and coding agents. With the inspect feature a program can describe its own configuration: schema and json_schema list every key, Loader::explain shows where each value came from, and ConfigError::to_json reports errors with positions and fixes. The generated flags --config-schema, --config-explain and --config-check expose the same from the command line. Fields marked guarded are refused by set and save unless the caller opts in.

Features

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

Performance

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.

Examples

Runnable examples live in forgeconf/examples.

License

Forgeconf is released under the MIT License.

About

Smol configuration rust crate

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

5 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages