Macro const_from_env

Source
macro_rules! const_from_env {
    ($(#[$meta:meta])* $name:ident: $t:ty = $env:expr, $default:expr) => { ... };
}
Expand description

Macro to create a const from an env var with compile-time parsing (Please read the docs carefully)

const_from_env!(LOCAL_PATH_MAX: usize = “LOCAL_PATH_MAX”, “X”);, where X(usize) is the default value if the env var is not set

Example usage:

use fdf::const_from_env;
const_from_env!(MYVAR: usize = "NYVAR", "6969");
assert_eq!(MYVAR, 6969); //6969 is the default value if the environment variable NYVAR is not set

/// This macro allows you to define a constant that can be set via an environment variable at compile time.` I realise people could have massive filesystems, i should probably write a rebuild script on value change.TODO! Macro to create a const from an env var with compile-time parsing

§Usage

use fdf::const_from_env;

// Creates a constant with documentation
const_from_env!(
    /// Maximum path length for local filesystem operations
    /// Default: 4096 (typical Linux PATH_MAX)
    LOCAL_PATH_MAX: usize = "`LOCAL_PATH_MAX`", "4096"
);

assert_eq!(LOCAL_PATH_MAX, 4096);

§Notes

  • The value is parsed at compile time
  • Environment variables must contain only numeric characters
  • Consider rebuilding if environment variable changes (TODO: add rebuild script)