servlet 1.1.0

A small application runtime with an explicit operational contract
Documentation
//! Typed business settings and secret-aware diagnostics.

use super::*;
use crate::settings::Settings;

#[derive(Debug)]
struct Application {
    limit: usize,
    enabled: bool,
    password: Secret<String>,
}

#[test]
fn ordinary_struct_validation_and_redacted_secret_access() {
    let snapshot = Settings::new()
        .value("LIMIT", Some("32"))
        .unwrap()
        .value("ENABLED", Some("true"))
        .unwrap()
        .secret("PASSWORD", None)
        .unwrap()
        .value("OPTIONAL", None)
        .unwrap()
        .secret("OPTIONAL_SECRET", None)
        .unwrap()
        .capture_from([("PASSWORD", "sensitive-password")])
        .unwrap();
    let config = Application {
        limit: snapshot.get("LIMIT").unwrap(),
        enabled: snapshot.get("ENABLED").unwrap(),
        password: snapshot.secret("PASSWORD").unwrap(),
    };
    snapshot
        .validate("LIMIT", (1..=64).contains(&config.limit))
        .unwrap();
    assert!(config.enabled);
    assert_eq!(config.password.expose(), "sensitive-password");
    for text in [
        format!("{config:?}"),
        format!("{snapshot:?}"),
        config.password.to_string(),
    ] {
        assert!(!text.contains("sensitive-password"));
    }
    assert_eq!(snapshot.optional::<u32>("OPTIONAL").unwrap(), None);
    assert!(
        snapshot
            .optional_secret::<String>("OPTIONAL_SECRET")
            .unwrap()
            .is_none()
    );
    assert_eq!(
        snapshot.get::<u32>("OPTIONAL").unwrap_err().kind(),
        Kind::Missing
    );
    assert_eq!(
        snapshot
            .secret::<String>("OPTIONAL_SECRET")
            .unwrap_err()
            .kind(),
        Kind::Missing
    );
    assert_eq!(
        snapshot.get::<String>("PASSWORD").unwrap_err().kind(),
        Kind::SecretAccess
    );
    assert_eq!(
        snapshot.secret::<String>("LIMIT").unwrap_err().kind(),
        Kind::SecretAccess
    );
    assert_eq!(
        snapshot.get::<String>("UNKNOWN").unwrap_err().kind(),
        Kind::UnknownKey
    );
    assert_eq!(
        snapshot.validate("UNKNOWN", true).unwrap_err().kind(),
        Kind::UnknownKey
    );
    assert_eq!(
        snapshot.validate("LIMIT", false).unwrap_err().kind(),
        Kind::Validation
    );
}

#[derive(Debug)]
struct Echo;
impl FromStr for Echo {
    type Err = String;
    fn from_str(value: &str) -> Result<Self, String> {
        Err(format!("bad secret {value}"))
    }
}

#[test]
fn native_error_chains_and_empty_values_do_not_fall_back() {
    use std::error::Error;
    let snapshot = Settings::new()
        .value("ORDINARY", Some("99"))
        .unwrap()
        .secret("PASSWORD", Some("99"))
        .unwrap()
        .capture_from([("ORDINARY", ""), ("PASSWORD", "private")])
        .unwrap();
    assert_eq!(
        snapshot.optional::<String>("ORDINARY").unwrap(),
        Some(String::new())
    );
    assert_eq!(
        snapshot.get::<u32>("ORDINARY").unwrap_err().kind(),
        Kind::Parse
    );
    let error = snapshot.secret::<Echo>("PASSWORD").unwrap_err();
    assert_eq!(error.kind(), Kind::Parse);
    assert_eq!(error.key(), Some("PASSWORD"));
    assert!(error.source().is_none());
    assert!(!format!("{error} {error:?}").contains("private"));
}