mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
Documentation
//! The closed registry of stored settings.
//!
//! A key absent from this file does not exist in the product. Every ceiling,
//! every default and every accepted value comes from here, so no other module
//! ever carries a literal of its own.

// What a setting can accept and what a value looks like once it passed.
mod types;
// The table itself, and the closed word lists the table points at.
mod registry;
// Reading a raw text as the value of one key, and refusing it well.
mod reading;

pub use reading::{unknown_key, validate};
pub use types::{Domain, Fallback, KeySpec, Value};

/// Every published setting.
#[must_use]
pub fn all() -> &'static [KeySpec] {
    registry::REGISTRY
}

/// The setting of that exact name, when it exists.
#[must_use]
pub fn spec(name: &str) -> Option<&'static KeySpec> {
    registry::REGISTRY.iter().find(|entry| entry.name == name)
}

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

    #[test]
    fn every_published_name_appears_exactly_once() {
        let mut seen = std::collections::BTreeSet::new();
        for entry in all() {
            assert!(seen.insert(entry.name), "duplicate setting {}", entry.name);
        }
        assert_eq!(seen.len(), all().len());
    }

    #[test]
    fn every_builtin_default_passes_its_own_domain() {
        for entry in all() {
            entry
                .default_value()
                .unwrap_or_else(|_| panic!("default of {} is outside its domain", entry.name));
        }
    }

    #[test]
    fn a_value_outside_the_domain_is_refused_with_the_accepted_values() {
        let failure = validate("output.format", "yaml").expect_err("must refuse");
        assert_eq!(failure.code(), "config.value_outside_domain");
        assert!(failure.hint().is_some_and(|hint| hint.contains("ndjson")));
    }

    #[test]
    fn a_positive_setting_refuses_zero_but_a_non_negative_one_accepts_it() {
        assert!(validate("limits.retry_base_ms", "0").is_err());
        assert_eq!(
            validate("limits.retries", "0").expect("zero attempts is legal"),
            Value::Integer(0)
        );
    }

    #[test]
    fn a_truth_value_is_read_from_the_obvious_spellings() {
        for raw in ["true", "yes", "1", "TRUE"] {
            assert_eq!(
                validate("sql.read_only", raw).expect("truth"),
                Value::Boolean(true)
            );
        }
        for raw in ["false", "no", "0"] {
            assert_eq!(
                validate("sql.read_only", raw).expect("truth"),
                Value::Boolean(false)
            );
        }
        assert!(validate("sql.read_only", "maybe").is_err());
    }

    #[test]
    fn a_mistyped_name_is_answered_with_the_nearest_setting() {
        let failure = validate("output.formt", "json").expect_err("must refuse");
        assert_eq!(failure.code(), "config.unknown_key");
        assert!(failure
            .hint()
            .is_some_and(|hint| hint.contains("output.format")));
    }

    #[test]
    fn a_setting_decided_elsewhere_carries_no_builtin_value() {
        let entry = spec("conn.timezone").expect("published");
        assert_eq!(entry.default_value().expect("no domain work"), None);
    }

    #[test]
    fn free_text_refuses_the_empty_value() {
        assert!(validate("sql.delimiter", "").is_err());
        assert_eq!(
            validate("sql.delimiter", ";").expect("legal"),
            Value::Text(";".to_owned())
        );
    }
}