ConfigurAnt
use ;
Install with cargo add configurant, paste it into src/main.rs and cargo run. It prints nothing: it succeeds when the
assertion holds. The library reads nothing by itself: the value is 8080 because only the schema's default was given
to it. A flag, an environment variable or a file changes it only when you hand that layer to the Loader (below). The
persistant features are optional and off by default. This snippet is examples/quickstart.rs; CI runs it
(tests/quickstart_example.rs), so it cannot go stale.
In your own project
Put the schema in configurant.toml beside Cargo.toml:
[]
= "app"
= "1.0.0"
= "demo"
= 1
[[]]
= "server.port"
= "int" # string, int, float, bool, path, enum (with `choices`) or list
= 8000
= "APP_PORT"
= "--port" # written with the dashes; a value without `flag` has no command-line flag
= "listen port"
[[]]
= "api.token"
= "string"
= true # environment (or `source`) only: no default, file or flag
= "API_TOKEN"
= "API token"
Hand each layer to the loader (default < file < environment < command line; add use std::collections::BTreeMap;):
let schema = parse?;
let environment: = vars_os
.filter_map
.collect;
let mut loader = new
.env
.cli; // without the program name; only the schema's flags
if let Ok = read_to_string
let config = loader.build?;
let port: i64 = config.get?;
let token: String = config.get?; // an error if API_TOKEN is not set
println!; // every layer, and which one won
The configuration file app.toml nests by the dots in the key:
[]
= 9000
A complete program, with each error explained, is in the user guide.
(read_to_string(..).ok() above ignores an unreadable file; the guide's program reports it.)
Propose a schema from existing code
cargo install configurant-scan (a separate install). Start with a configurant.toml holding only the [project]
table, install the language server (rust-analyzer; pyright-langserver for Python; typescript-language-server,
with @types/node in the project, for TypeScript), then:
configurant-scan configurant.toml --root . --lang python # prints proposals
configurant-scan configurant.toml --root . --lang python --accept APP_PORT --write
If you name the server yourself with --server, its arguments are no longer added for you: add
--server-arg=--stdio for the Python and TypeScript servers, or the scan times out. On Windows, npm installs a .cmd
shim, so give --server its full path. A proposal is not marked required; check that yourself.
Reference
Each value in the schema takes: key, type, default, min, max, choices, description, prompt, hint, example, section, advanced, secret, env, source, flag, restart, provider, privileged, required. An unknown field is an error that lists these.
Schema-first configuration for Rust projects. Declare every value once in configurant.toml; the
library takes the schema defaults and whatever file text, environment map and argument list your code hands to the
Loader (it reads none of them itself), applies them in the order default < file < environment < command line,
validates with named errors, and
explains where each value came from. No generated code: the schema is a plain file that tools
and humans can edit, and the application reads values through the library. Secrets are enforced:
never printed, never read from a file or a flag; they come from the environment, or from a named source that configurant-secrets fetches (the environment still wins).
The commands below need the binary: cargo install configurant. The examples/ files they name are not in the published crate: clone the repository or use your own schema. configurant-tui, configurant-gui and configurant-scan are separate installs.
configurant check examples/configurant.toml
configurant describe examples/configurant.toml
configurant migrate examples/configurant.toml app.toml --from 1 # prints; --write saves app.toml.bak first
configurant json-schema examples/configurant.toml > configurant.schema.json # for editors (taplo, Even Better TOML); secrets left out
configurant project examples/configurant.toml --format wrangler # a host's settings block (env, wrangler, compose); secrets named, never printed
DEMO_PORT=9000 configurant explain examples/configurant.toml server.port -- --port 9100
// Opt-in typed view: `server.port` is field `port` of field `server`. Nothing is generated.
let settings: Settings = config.deserialize?; // Settings: your own #[derive(Deserialize)] struct
Key check (configurant keys examples/configurant.toml src, or configurant::keycheck from a build.rs): finds
Rust code that asks for a key the schema does not define. Known limits: it matches source text, so it sees only
.get::<T>("k"), .value("k"), .rows("k") and .explain("k") with a plain string literal; a key read through a
helper function (fn setting(c: &Config, k: &str) { c.get::<u16>(k) }, called as setting(c, "server.prot")) is
a computed name at the call inside the helper, so it is counted and not checked, and its callers are not followed;
a key built with format! or held in a variable is the same; another type's method named value, rows or
explain with a string argument is reported as if it were ours.
Status: 0.8.x (pre-1.0; a breaking change moves the second number). Schema, layering, validation, explain, serde struct binding, config-file migrations and the module
interface exist, and the PersistAnt module is behind the persistant feature; configurant-tui and configurant-gui edit a file with review and backup, configurant-scan proposes values. Design:
docs/DESIGN.md; why not figment/config/clap/confy: docs/PRIOR-ART.md.
Loading a file and the environment, what each error means, and exit codes: docs/GUIDE.md.
What stays compatible and how to upgrade: docs/UPGRADING.md.
Licence: MIT OR Apache-2.0.