#env-var #static #config-env

env-flags

A convenience macro for declaring environment variables

2 releases

new 0.1.1 Mar 31, 2025
0.1.0 Oct 6, 2024

#146 in Configuration

Download history 479/week @ 2024-12-11 255/week @ 2024-12-18 334/week @ 2024-12-25 1621/week @ 2025-01-01 2383/week @ 2025-01-08 2410/week @ 2025-01-15 3150/week @ 2025-01-22 3709/week @ 2025-01-29 13331/week @ 2025-02-05 16790/week @ 2025-02-12 10524/week @ 2025-02-19 13138/week @ 2025-02-26 7266/week @ 2025-03-05 12547/week @ 2025-03-12 9251/week @ 2025-03-19 6103/week @ 2025-03-26

36,355 downloads per month

MIT license

31KB
665 lines

env_flags

github crates.io docs.rs build status

This library provides a convenient macro for declaring environment variables.

[dependencies]
env-flags = "0.1"

Compiler support: requires rustc 1.80+

Example

use env_flags::env_flags;

use std::time::Duration;

env_flags! {
    /// Required env var, panics if missing.
    AUTH_TOKEN: &str;
    /// Env var with a default value if not specified.
    pub(crate) PORT: u16 = 8080;
    /// An optional env var.
    pub OVERRIDE_HOSTNAME: Option<&str> = None;

    /// `Duration` by default is parsed as `f64` seconds.
    TIMEOUT: Duration = Duration::from_secs(5);
    /// Custom parsing function, takes a `String` and returns a `Result<Duration>`.
    TIMEOUT_MS: Duration = Duration::from_millis(30), |value| {
        value.parse().map(Duration::from_millis)
    };

    /// `bool` can be true, false, 1, or 0 (case insensitive)
    /// eg. export ENABLE_FEATURE="true"
    pub ENABLE_FEATURE: bool = true;

    /// `Vec<T>` by default is parsed as a comma-seprated string
    /// eg. export VALID_PORTS="80,443,9121"
    pub VALID_PORTS: Vec<u16> = vec![80, 443, 9121];
}

No runtime deps