envparse 0.1.0

Parse compile-time environment variables into `const`s
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented2 out of 6 items with examples
  • Size
  • Source code size: 95.69 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.66 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • thomcc/envparse
    3 0 6
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • thomcc

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],
}