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
//! Named errors. No variant carries a secret's value: where a value would be shown for a secret
//! key, the text is `<redacted>`.

use std::fmt;

use crate::schema::Bound;

/// Where a value (or an offending input) came from.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Origin {
    Default,
    /// A configuration file, by the name it was given.
    File(String),
    /// An environment variable, by name.
    Env(String),
    /// A command-line flag (or argument position).
    Cli(String),
    /// A secret source (`configurant-secrets`), as the schema declares it: `<source>:<reference>`. Never the value.
    Source(String),
    /// A lookup by key (`Config::get`, `explain`), not a source of values.
    Lookup,
    /// A change made by a front end through [`crate::edit::Session`].
    Edit,
}

impl fmt::Display for Origin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Origin::Default => f.write_str("the schema default"),
            Origin::File(n) => write!(f, "file `{n}`"),
            Origin::Env(n) => write!(f, "environment variable `{n}`"),
            Origin::Cli(n) => write!(f, "command-line `{n}`"),
            Origin::Source(declared) => match declared.split_once(':') {
                Some((name, reference)) => {
                    write!(f, "secret source `{name}` (reference `{reference}`)")
                }
                None => write!(f, "secret source `{declared}`"),
            },
            Origin::Lookup => f.write_str("this lookup"),
            Origin::Edit => f.write_str("this edit"),
        }
    }
}

/// One refused input.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
    #[error("unknown key `{key}` from {origin}{}", suggest(.suggestion))]
    UnknownKey {
        key: String,
        suggestion: Option<String>,
        origin: Origin,
    },
    #[error("`{key}` from {origin}: expected {expected}, found {found}")]
    WrongType {
        key: String,
        expected: &'static str,
        found: String,
        origin: Origin,
    },
    #[error("`{key}` from {origin}: {found} is outside {}", range(*.min, *.max))]
    OutOfRange {
        key: String,
        found: String,
        min: Option<Bound>,
        max: Option<Bound>,
        origin: Origin,
    },
    #[error("`{key}` from {origin}: {found} is not one of {}", .choices.join(", "))]
    NotAllowed {
        key: String,
        found: String,
        choices: Vec<String>,
        origin: Origin,
    },
    #[error("`{key}` is a secret and may not come from {origin}; use its environment variable")]
    SecretInForbiddenSource { key: String, origin: Origin },
    #[error("`{key}` is required and no origin set it")]
    MissingRequired { key: String },
    #[error("`{key}` has no value (no default and no origin set it)")]
    NotSet { key: String },
    #[error("flag `{flag}` needs a value")]
    MissingCliValue { flag: String },
    #[error("unexpected command-line argument at position {index}")]
    UnexpectedArgument { index: usize },
    #[error("configuration file `{name}`: {reason}")]
    FileParse { name: String, reason: String },
    #[error("`{key}` takes its value from module `{module}`, which is not registered")]
    ModuleMissing { key: String, module: String },
    #[error(
        "configuration file `{name}` is at config version {found}, this schema is at {current}; run `configurant migrate`"
    )]
    FileOutdated {
        name: String,
        found: u32,
        current: u32,
    },
    #[error(
        "configuration file `{name}` is at config version {found}, newer than this schema's {current}"
    )]
    FileNewer {
        name: String,
        found: u32,
        current: u32,
    },
    #[error(
        "configuration file `{name}` has `{mount}` at config version {found}, the fragment is at {current}; run `configurant migrate`"
    )]
    MountFileOutdated {
        name: String,
        mount: String,
        found: u32,
        current: u32,
    },
    #[error(
        "configuration file `{name}` has `{mount}` at config version {found}, newer than the fragment's {current}"
    )]
    MountFileNewer {
        name: String,
        mount: String,
        found: u32,
        current: u32,
    },
    #[error("map `{key}` from {origin} has an empty key")]
    EmptyMapKey { key: String, origin: Origin },
    #[error("`{key}` is an array of tables or a map, which only the configuration file can set")]
    FileOnly { key: String },
    #[error("the configuration does not fit the target type: {reason}")]
    Deserialize { reason: String },
    #[error("`{key}` from {origin} was rejected by module `{module}`: {reason}")]
    ModuleRejected {
        key: String,
        module: String,
        reason: String,
        origin: Origin,
    },
}

fn suggest(s: &Option<String>) -> String {
    s.as_ref()
        .map(|s| format!(" (did you mean `{s}`?)"))
        .unwrap_or_default()
}

fn range(min: Option<Bound>, max: Option<Bound>) -> String {
    match (min, max) {
        (Some(a), Some(b)) => format!("{a}..={b}"),
        (Some(a), None) => format!(">= {a}"),
        (None, Some(b)) => format!("<= {b}"),
        (None, None) => "the allowed range".into(),
    }
}

/// Every error found in one load (the loader does not stop at the first).
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub struct Errors(pub Vec<ConfigError>);

impl fmt::Display for Errors {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, e) in self.0.iter().enumerate() {
            if i > 0 {
                f.write_str("\n")?;
            }
            write!(f, "{e}")?;
        }
        Ok(())
    }
}