#env-var #variables #environment #env

build const_env_impl

Configure const and static items by environment variables

4 releases

0.1.3 Nov 25, 2021
0.1.2 Oct 30, 2019
0.1.1 Oct 30, 2019
0.1.0 Oct 30, 2019

#705 in Build Utils

Download history 1471/week @ 2024-03-14 1330/week @ 2024-03-21 1222/week @ 2024-03-28 1645/week @ 2024-04-04 1853/week @ 2024-04-11 1263/week @ 2024-04-18 1660/week @ 2024-04-25 1434/week @ 2024-05-02 1501/week @ 2024-05-09 1271/week @ 2024-05-16 735/week @ 2024-05-23 946/week @ 2024-05-30 694/week @ 2024-06-06 810/week @ 2024-06-13 1254/week @ 2024-06-20 917/week @ 2024-06-27

3,853 downloads per month
Used in 50 crates (via const_env)

CC0 license

13KB
137 lines

const_env

Motivation

Your goal: You want to define various constants for your code at compile time based on environment variables.

Your problem: Its only possible to do this for &'static str constants in today's Rust.

const MY_STR: &'static str = env!("SOME_ENV");

You cannot similarly initialize other types such as constant u32s, bools, or other primitive types. See issues and workarounds. Eventually you will be able to do so once support for running parse and unwrap in const fn lands, but for now this crate offers an easy workaround that you can use today.

Crate Features

Feature name Enabled by default? Requires nightly? Description
tracked No yes Use the unstable proc_macro_tracked_env feature to inform the build system about the used environment variables.

Usage

Add the dependency. If your crate uses nightly, enable the tracked feature for better build dependency tracking.

# If using a stable compiler:
[dependencies]
const_env = "0.1"

# If using a nightly compiler:
[dependencies]
const_env = { version = "0.1", features = ["tracked"] }

At the top of your file import the from_env! macro.

use const_env::from_env;

Use the macro to override the value of constants based on environment variables at build time.

#[from_env]
const FOO: u32 = 123;

// This test will PASS if invoked as `FOO=456 cargo test`
#[test]
fn test() {
    assert_eq!(456, FOO);
}

By default, the macro looks for an environment variable with the same name as the constant that it is attached to.

// Use `FOO=true cargo build` to configure the value.
#[from_env]
const FOO: bool = false;

But you can specify a name explicitly too.

// Use `BAR=true cargo build` to configure the value.
#[from_env("BAR")]
const FOO: bool = false;

The expression that you assign in your source acts as a default in case the environment variable does not exist.

// If env var FOO is not set then the FOO constant will have the default value of '🦀'.
#[from_env]
const FOO: char = '🦀';

Both const and static declarations are supported.

// Both of these may be set by `FOO=abc BAZ=def cargo build`.
#[from_env]
const FOO: &'static str = "hello";
#[from_env("BAZ")]
static BAR: &'static [u8] = b"world";

Supported Types

Strings!

#[from_env]
const FOO: &'static str = "hello";

// example: `FOO=abc cargo build`
// results in:
const FOO: &'static str = "abc";

Byte strings!

#[from_env]
const FOO: &'static [u8] = b"hello";

// example: `FOO=world cargo build`
// results in:
const FOO: &'static [u8] = b"world";

Bytes!

#[from_env]
const FOO: u8 = b'';

// example: `FOO=🦀 cargo build`
// results in:
const FOO: u8 = b'🦀';

Characters!

#[from_env]
const FOO: char = '';

// example: `FOO=🦀 cargo build`
// results in:
const FOO: car = '🦀';

Integers of all shapes and sizes!

#[from_env]
const FOO: u32 = 123;
#[from_env]
const BAR: i64 = 456;
#[from_env]
const BAZ: usize = 0;

// example: `FOO=321 BAR=-456 BAZ=1usize cargo build`
// results in:
const FOO: u32 = 321;
const BAR: i64 = -456;
const BAZ: usize = 1usize;

Floats of all shapes and sizes!

#[from_env]
const FOO: f32 = 123.0;
#[from_env]
const BAR: f64 = 456.0;
#[from_env]
const BAZ: f32 = 0.0;

// example: `FOO=321.0 BAR=-456.0 BAZ=1f32 cargo build`
// results in:
const FOO: f32 = 321.0;
const BAR: f64 = -456.0;
const BAZ: f32 = 1f32;

Booleans!

#[from_env]
const FOO: bool = false;

// example: `FOO=true cargo build`
// results in:
const FOO: bool = true;

Known Limitations

  • Only top-level const and static declarations are supported.

Alternatives

  • Writing a build.rs script which looks at the env vars and generates code based on them. This is conceptually similar to how this crate works, except that this crate uses a procedural macro instead of a build script.
  • Wait for const fn to be finished. Ultimated this crate does the equivalent of static FOO: u32 = std::option_env!("FOO").map(|val| val.parse::<u32>().unwrap_or(0)).unwrap_or(0);, so once you can simply write that code this crate will become superfluous.

Dependencies

~1.5MB
~35K SLoC