envparse

Crates.ioenvparse
lib.rsenvparse
version
sourcesrc
created_at2024-12-01 00:11:23.209453
updated_at2024-12-01 00:11:23.209453
descriptionParse compile-time environment variables into `const`s
homepagehttps://github.com/thomcc/envparse
repositoryhttps://github.com/thomcc/envparse
max_upload_size
id1467071
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
crates-io (github:sigp:crates-io)

documentation

README

envparse

Quick and dirty crate for parsing values out of an environment var provided at compile time. See also: docs.

Usage

Here's an example

const MAX_LEN: usize = envparse::parse_env!("MYCRATE_MAX_THING_LEN" as usize else 64);
struct Thing {
    len: [u8; MAX_LEN],
}

You can bound by ranges too. This one will fail because the MUST_BE_USER_PROVIDED var isn't provided.

const MAX_LEN_LOG2: u32 = envparse::parse_env!("MYCRATE_MAX_LEN_LOG2" as u32 in 0..32);
const MAX_LEN: usize = 1 << MAX_LEN_LOG2;
struct Thing {
    len: [u8; MAX_LEN],
}

You can also try

const MAX_LEN_LOG2: u32 = match envparse::parse_env!(try "OPTIONAL_MAX_LEN_LOG2" as u32 in 0..32) {
    Some(v) => v,
    None => 5,
}
const MAX_LEN: usize = 1 << MAX_LEN_LOG2;
struct Thing {
    len: [u8; MAX_LEN],
}

Commit count: 12

cargo fmt