configurant 0.8.13

Schema-first configuration: declare values in a TOML schema, layer defaults, file, environment and command line, 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
//! The user guide's program (`examples/guide.rs`) is compiled and driven here, so the guide cannot go stale.
#![allow(
    dead_code,
    reason = "include! pulls in examples/guide.rs, whose main is not called here"
)]

include!("../examples/guide.rs");

#[test]
fn the_layers_override_in_the_order_the_guide_states() {
    let env = || BTreeMap::from([("APP_PORT".to_string(), "9001".to_string())]);
    let file = Some(("app.toml", "[server]\nport = 9000\n"));
    let args = vec!["--port".to_string(), "9002".to_string()];
    assert_eq!(load(None, BTreeMap::new(), vec![]).unwrap(), 8080);
    assert_eq!(load(file, BTreeMap::new(), vec![]).unwrap(), 9000);
    assert_eq!(load(file, env(), vec![]).unwrap(), 9001);
    assert_eq!(load(file, env(), args).unwrap(), 9002);
}

#[test]
fn the_error_the_guide_shows_is_the_error_given() {
    let file = Some(("app.toml", "[server]\nprot = 9000\n"));
    let said = load(file, BTreeMap::new(), vec![]).unwrap_err().to_string();
    assert_eq!(
        said,
        "unknown key `server.prot` from file `app.toml` (did you mean `server.port`?)"
    );
}

#[test]
fn the_guide_snippet_is_the_example() {
    let guide = include_str!("../docs/GUIDE.md").replace("\r\n", "\n");
    let start = guide.find("```rust\n").expect("a rust block") + 8;
    let end = start + guide[start..].find("```").expect("block closes");
    let example = include_str!("../examples/guide.rs").replace("\r\n", "\n");
    let body = example.split_once('\n').expect("SPDX line").1.trim_end();
    assert_eq!(
        guide[start..end].trim_end(),
        body,
        "docs/GUIDE.md drifted from examples/guide.rs"
    );
}