servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Fixed diagnostics that never retain values, paths, or native parser errors.

use std::fmt;

/// A stable application-settings failure category.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SettingsErrorKind {
    /// Names must be uppercase ASCII identifiers of at most 96 bytes.
    InvalidKey,
    /// A key was declared more than once.
    DuplicateKey,
    /// An undeclared key was accessed or configured.
    UnknownKey,
    /// A required value is absent from every source.
    Missing,
    /// The winning value is not UTF-8.
    Encoding,
    /// A key, value, path, or aggregate storage bound was exceeded.
    Limit,
    /// A supplied environment contains a repeated selected key.
    DuplicateEnvironment,
    /// The selected value cannot be parsed as the requested Rust type.
    Parse,
    /// An application validation predicate was false.
    Validation,
    /// Ordinary access to a secret, or secret access to an ordinary key.
    SecretAccess,
    /// A selected secret file cannot be opened or read as a regular file.
    SecretFile,
}

/// Redacted diagnostics. Keys are public metadata; values and file paths are not.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SettingsError {
    key: Option<String>,
    kind: SettingsErrorKind,
}

impl SettingsError {
    pub(super) fn new(key: &str, kind: SettingsErrorKind) -> Self {
        Self {
            key: super::valid_key(key).then(|| key.to_owned()),
            kind,
        }
    }
    /// The declared key, if its spelling is a valid bounded identifier.
    pub fn key(&self) -> Option<&str> {
        self.key.as_deref()
    }
    /// Fixed failure category, without an underlying error chain.
    pub fn kind(&self) -> SettingsErrorKind {
        self.kind
    }
}

impl fmt::Display for SettingsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "application settings {:?}", self.kind)?;
        if let Some(key) = &self.key {
            write!(f, " for {key}")?;
        }
        Ok(())
    }
}
impl std::error::Error for SettingsError {}