#env-var #environment #env #static

env-flags

A convenience macro for declaring environment variables

1 unstable release

0.1.0 Oct 6, 2024

#278 in Configuration

Download history 480/week @ 2024-10-27 205/week @ 2024-11-03 448/week @ 2024-11-10 472/week @ 2024-11-17 389/week @ 2024-11-24 427/week @ 2024-12-01 632/week @ 2024-12-08 315/week @ 2024-12-15 277/week @ 2024-12-22 805/week @ 2024-12-29 2418/week @ 2025-01-05 2707/week @ 2025-01-12 2565/week @ 2025-01-19 3048/week @ 2025-01-26 9549/week @ 2025-02-02 11968/week @ 2025-02-09

27,388 downloads per month

MIT license

29KB
640 lines

env_flags

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