mkenv
mkenv is a lightweight Rust crate that helps you define the configuration your app needs.
Configurations are based on environment variables. The goal of this library is to make your app centralize the fetching of these environment variables. It greatly improves readability in your code.
Another benefit is that it allows you to fetch for all the environment variables at any time, e.g. on the program startup. This way, you may remove runtime errors when retrieving an invalid environment variable, because the error would be caught earlier. The library is designed to make the error message clear, specifying all the variables the program needs.
Example
use Duration;
use *;
// In debug mode
make_config!
}
// In release mode
make_config!
}
make_config!
}
let config = define;
// Optional: initialize the config, to early-panic if there is
// any missing/invalid environment variable.
config.init;
let timeout = config.request_timeout.get;
If the instruction with the init() call fails, an error similar to this is shown:
Error during configuration initialization:
Got 2 incorrect variables
- `DB_URL`: environment variable not found
- `REQUEST_TIMEOUT`: environment variable not found
Got 1 valid variable
- `USER`
Full required environment description:
- `DB_URL`: The DB URL
- `USER`
- `REQUEST_TIMEOUT`
You may find more complete examples here, or read the crate documentation.