configurant 0.9.0

Schema-first configuration: declare values in a TOML schema, layer schema defaults with the file, environment and arguments your code hands in, validate with named errors, explain where each value came from. No generated code; secrets never printed.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
use configurant::{Loader, Schema};
use std::collections::BTreeMap;

const SCHEMA: &str = r#"
[project]
name = "app"
version = "1.0.0"
summary = "demo"
schema_version = 1
env_prefix = "APP_"

[[value]]
key = "server.port"
type = "int"
default = 8080
min = 1
max = 65535
env = "APP_PORT"
flag = "--port"
description = "listen port"
"#;

/// `file` is the configuration file's name and text, if there is one.
fn load(
    file: Option<(&str, &str)>,
    env: BTreeMap<String, String>,
    args: Vec<String>,
) -> Result<i64, Box<dyn std::error::Error>> {
    let schema = Schema::parse(SCHEMA)?;
    let mut loader = Loader::new(&schema).env(env).cli(args);
    if let Some((name, text)) = file {
        loader = loader.file(name, text);
    }
    let config = loader.build()?; // every error found, one per line
    Ok(config.get("server.port")?)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // The library reads nothing by itself: you hand it the file, the environment and the arguments.
    let path = "app.toml";
    let text = match std::fs::read_to_string(path) {
        Ok(text) => Some(text),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
        Err(e) => return Err(e.into()),
    };
    // `vars` panics on a variable that is not Unicode; `vars_os` lets you skip those.
    let env = std::env::vars_os()
        .filter_map(|(k, v)| Some((k.into_string().ok()?, v.into_string().ok()?)))
        .collect();
    let args = std::env::args().skip(1).collect();
    let port = load(text.as_deref().map(|t| (path, t)), env, args)?;
    println!("listening on {port}");
    Ok(())
}