file_env_const
This crate provides macros for loading data at compile time from a file or environment variable, with fallback options.
This is useful in the case of building a binary with hard-coded strings which you may wish to inject as environment variables in a build environment (say in CI), or store locally in a file when building offline, with a fallback option in either case.
Examples
Loading a file, with environment fallback
use file_env;
// Read data from file first
const FILE_DATA: &'static str = file_env!;
let f = read_to_string.unwrap;
assert_eq!;
// Tries to read data from file, falls back to environment variable which is the package name
const FALL_BACK_TO_ENV: &'static str = file_env!;
assert_eq!;
// Tries to read data from file, falls back to environment variable which is the package name
const FALL_BACK_TO_DEFAULT: &'static str =
file_env!;
assert_eq!;
Loading an environment variable, with file fallback
use env_file;
// Read data from file first
const ENV_DATA: &'static str = env_file!;
assert_eq!;
// Tries to read data from file, falls back to environment variable which is the package name
const FALL_BACK_TO_FILE: &'static str = env_file!;
let f = read_to_string.unwrap;
assert_eq!;
// Tries to read data from file, falls back to environment variable which is the package name
const FALL_BACK_TO_DEFAULT: &'static str =
env_file!;
assert_eq!;