menv
This crate provides a macro for asserting the presence of a list of environment
variables and accessing them as types which implement FromStr
.
Generate the following:
- A function which asserts the presence and well-formedness of a list of env vars
- A function which returns a bool
representing whether any of the required vars are set
- A function which returns a String
representing the collected help messages for the list of vars
- A list of functions, one for each environment variable required
Here we fill an env
module with required environment variables,
print help and exit if none of them are set, runs the asserts for them
if some are set, and proceeds.
Other parts of this program are now free, since the asserts were run at the start of main,
to access env::server_port()
and env::db_path()
as if they are infallible.
```
mod env {
use menv::requireenvs;
requireenvs! {
(assertenvvars, anyset, genhelp);
server_port, "FERRISCRAFT_USERS_PORT", u16,
"FERRISCRAFT_USERS_PORT should be set to the desired server port";
db_path, "FERRISCRAFT_USERS_DB", String,
"FERRISCRAFT_USERS_DB should be set to the path to the users database";
}
} fn main() { if env::anyset() { env::assertenvvars(); } else { println!("# Environment Variables Help\n{}", env::genhelp()); return } } ```