ch-config 0.2.0

Typed configuration loading with validation and secret-file support
Documentation
use std::str::FromStr;

use super::{ConfigError, Result};

pub(crate) fn parsed<T>(
    variable: &'static str,
    value: Option<String>,
    expected: &'static str,
) -> Result<Option<T>>
where
    T: FromStr,
{
    value
        .map(|value| {
            value.parse::<T>().map_err(|_| ConfigError::VariableValue {
                variable,
                message: expected,
            })
        })
        .transpose()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parsed_values_never_include_the_rejected_input() {
        let error = parsed::<u16>(
            "SMTP_PORT",
            Some("not-a-port".to_owned()),
            "must be an integer TCP port",
        )
        .expect_err("fixture must be invalid");
        let rendered = format!("{error:?} {error}");
        assert!(!rendered.contains("not-a-port"));
        assert!(rendered.contains("SMTP_PORT"));
    }
}