confroid 0.0.1

The n+1-st config reader for your environment-based configs.
Documentation
//! Documentation-generation tests (require the `docs` feature).
#![cfg(feature = "docs")]
#![allow(dead_code)] // fields exist for their derived docs, not for reading

use confroid::{env_example, markdown_table};

#[derive(confroid::Config, Debug)]
struct HttpConfig {
    #[confroid(default = 8080)]
    /// The port to listen on.
    port: u16,
    #[confroid(example = "localhost")]
    /// The host to listen on.
    host: String,
}

const DEFAULT_NAME: &str = "World";

#[derive(confroid::Config, Debug)]
struct Config {
    /// The HTTP configuration.
    http: HttpConfig,
    #[confroid(default = "Hello")]
    /// The greeting to use.
    greeting: String,
    #[confroid(default = DEFAULT_NAME, example = "John")]
    /// The name of the user.
    name: String,
}

#[test]
fn env_example_matches_expected_layout() {
    let expected = "\
# Config configuration

# The HTTP configuration.

# The port to listen on. (default: 8080)
HTTP__PORT=8080
# The host to listen on. (example: localhost)
HTTP__HOST=

# The greeting to use. (default: Hello)
GREETING=Hello

# The name of the user. (default: World, example: John)
NAME=World
";
    assert_eq!(env_example::<Config>(), expected);
}

#[test]
fn markdown_table_lists_all_leaf_variables() {
    let expected = "\
| Variable | Description | Default | Example |
| --- | --- | --- | --- |
| `HTTP__PORT` | The port to listen on. | `8080` |  |
| `HTTP__HOST` | The host to listen on. |  | `localhost` |
| `GREETING` | The greeting to use. | `Hello` |  |
| `NAME` | The name of the user. | `World` | `John` |
";
    assert_eq!(markdown_table::<Config>(), expected);
}

#[test]
fn bare_default_renders_via_display() {
    #[derive(confroid::Config, Debug)]
    struct WithBare {
        #[confroid(default)]
        /// Retry count.
        retries: u16,
    }
    let out = env_example::<WithBare>();
    assert!(out.contains("# Retry count. (default: 0)"), "got:\n{out}");
    assert!(out.contains("RETRIES=0"), "got:\n{out}");
}