dotenv_verbatim/lib.rs
1//! A `.env` loader that takes the value verbatim: everything after the first `=`, with no
2//! expansion, no escape processing and no inline-comment stripping.
3//! A line that cannot be a key/value pair is skipped by number, never the rest of the file, and a
4//! variable already present in the process environment wins. Rationale and evidence: README.
5
6#![forbid(unsafe_code)]
7
8mod parse;
9
10pub use crate::parse::parse;
11pub use crate::parse::Entry;
12pub use crate::parse::Parsed;
13
14use std::path::Path;
15
16/// Read the file and set every variable it declares that the process does not have yet.
17/// A missing or unreadable file is not an error: the deployed environment provides real vars.
18/// A key repeated inside the file keeps its first value, which the later pass then sees as set.
19pub fn load(path: &Path) {
20 let content = match std::fs::read_to_string(path) {
21 Ok(content) => content,
22 Err(_) => return,
23 };
24 for entry in parse(&content).entries {
25 set_if_absent(entry.key, entry.value);
26 }
27}
28
29/// dotenv semantics: a variable already in the process environment wins over the file.
30fn set_if_absent(key: &str, value: &str) {
31 if std::env::var_os(key).is_none() {
32 std::env::set_var(key, value);
33 }
34}