Function env_file_reader::read_files
source · [−]Expand description
Parses multiple environment files at the specified paths
and constructs a single HashMap with the merged environment
variables from the files.
This is a vectorized version of read_file, where a single
output is constructed from calling read_file for each file
provided as an argument to this function.
The order in which the paths
are defined is maintained, so
if an environment file with a higher index exposes the same
environment variable as a file with a lower index, the value of
the file with the higher index is exposed by the returned
HashMap.
Returns an error if reading one file was unsuccessful or if one
file is ill-formatted.
Read the crate’s documentation for information about the
format that env-file-reader
supports.
Example:
examples/.env
:
CLIENT_ID=YOUR_CLIENT_ID
CLIENT_SECRET=YOUR_CLIENT_SECRET
examples/.env.utf8
:
🦄=💖
💖=🦄
use env_file_reader::read_files;
fn main() -> std::io::Result<()> {
let env_variables = read_files(&[
"examples/.env",
"examples/.env.utf8",
])?;
assert_eq!(&env_variables["CLIENT_ID"], "YOUR_CLIENT_ID");
assert_eq!(&env_variables["CLIENT_SECRET"], "YOUR_CLIENT_SECRET");
assert_eq!(&env_variables["🦄"], "💖");
assert_eq!(&env_variables["💖"], "🦄");
Ok(())
}