servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Immutable strings, typed access, and application-owned validation.

use super::{Secret, SettingsError, SettingsErrorKind as Kind};
use std::{collections::BTreeMap, fmt, str::FromStr};

pub(super) struct Value {
    pub value: Option<String>,
    pub secret: bool,
}

/// An immutable snapshot with no further environment or filesystem reads.
/// Parse fields into an ordinary application struct, validate, then drop this
/// snapshot. All stored values are redacted from `Debug`, even ordinary settings.
pub struct Snapshot {
    pub(super) values: BTreeMap<String, Value>,
}

impl Snapshot {
    /// Parse a required ordinary setting; native parse errors are discarded.
    pub fn get<T: FromStr>(&self, key: &str) -> Result<T, SettingsError> {
        self.optional(key)?
            .ok_or_else(|| SettingsError::new(key, Kind::Missing))
    }
    /// Parse an optional ordinary setting. Empty strings are present values.
    pub fn optional<T: FromStr>(&self, key: &str) -> Result<Option<T>, SettingsError> {
        self.parse(key, false)
    }
    /// Parse a required secret into a redacted wrapper.
    pub fn secret<T: FromStr>(&self, key: &str) -> Result<Secret<T>, SettingsError> {
        self.optional_secret(key)?
            .ok_or_else(|| SettingsError::new(key, Kind::Missing))
    }
    /// Parse an optional secret into a redacted wrapper.
    pub fn optional_secret<T: FromStr>(
        &self,
        key: &str,
    ) -> Result<Option<Secret<T>>, SettingsError> {
        self.parse(key, true).map(|v| v.map(Secret))
    }
    /// Report a fixed validation failure for a declared key if `valid` is false.
    /// Supports field or cross-field constraints without retaining custom errors.
    pub fn validate(&self, key: &str, valid: bool) -> Result<(), SettingsError> {
        self.entry(key)?;
        if !valid {
            return Err(SettingsError::new(key, Kind::Validation));
        }
        Ok(())
    }
    fn entry(&self, key: &str) -> Result<&Value, SettingsError> {
        self.values
            .get(key)
            .ok_or_else(|| SettingsError::new(key, Kind::UnknownKey))
    }
    fn parse<T: FromStr>(&self, key: &str, secret: bool) -> Result<Option<T>, SettingsError> {
        let entry = self.entry(key)?;
        if entry.secret != secret {
            return Err(SettingsError::new(key, Kind::SecretAccess));
        }
        entry
            .value
            .as_ref()
            .map(|value| {
                value
                    .parse()
                    .map_err(|_| SettingsError::new(key, Kind::Parse))
            })
            .transpose()
    }
}
impl fmt::Debug for Snapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Snapshot")
            .field("keys", &self.values.keys())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
#[path = "snapshot_test.rs"]
mod snapshot_test;