apollo-configuration 0.4.0

A typed, friendly configuration system for Apollo products.
Documentation
use apollo_configuration::{configuration, parse_yaml};
use std::num::NonZeroUsize;

#[test]
fn fancy_default_expr() {
    #[configuration]
    /// In memory cache configuration
    struct InMemoryCacheConfig {
        /// Number of entries in the Least Recently Used cache
        #[config(default = NonZeroUsize::new(500).unwrap())]
        pub limit: NonZeroUsize,
    }

    let default = InMemoryCacheConfig::default();
    assert_eq!(default.limit.get(), 500);
}

#[test]
fn default_defaults() {
    #[configuration]
    /// In memory cache configuration
    struct InMemoryCacheConfig {
        pub limit: usize,
    }

    let default = InMemoryCacheConfig::default();
    assert_eq!(default.limit, 0);
}

#[test]
fn custom_default() {
    #[configuration]
    /// In memory cache configuration
    struct InMemoryCacheConfig {
        #[config(default = 10)]
        pub limit: usize,
    }

    let default = InMemoryCacheConfig::default();
    assert_eq!(default.limit, 10);
}

#[test]
fn empty_yaml_string_parses_to_default() {
    #[derive(PartialEq)]
    #[configuration]
    struct Config {
        a: u8,
        c: bool,
    }

    let parsed = parse_yaml::<Config>("", &Default::default());
    assert_eq!(parsed.unwrap(), Config { a: 0, c: false });
}

#[test]
fn empty_yaml_string_with_required_fails() {
    #[configuration]
    struct Config {
        a: u8,
        #[config(required)]
        c: bool,
    }

    let parsed = parse_yaml::<Config>("", &Default::default())
        .expect_err("should fail with validation error");
    assert_eq!(parsed.to_string(), "schema validation error");
}

#[test]
fn comment_yaml_string_parses_to_default() {
    #[derive(PartialEq)]
    #[configuration]
    struct Config {
        a: u8,
        c: bool,
    }

    let parsed = parse_yaml::<Config>(
        r#"
# this is a comment

# there's also some whitespace...if you even care...
"#,
        &Default::default(),
    );
    assert_eq!(parsed.unwrap(), Config { a: 0, c: false });
}