Skip to main content

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.
18pub fn load(path: &Path) {
19    let content = match std::fs::read_to_string(path) {
20        Ok(content) => content,
21        Err(_) => return,
22    };
23    for entry in parse(&content).entries {
24        set_if_absent(entry.key, entry.value);
25    }
26}
27
28/// dotenv semantics: a variable already in the process environment wins over the file.
29fn set_if_absent(key: &str, value: &str) {
30    if std::env::var_os(key).is_none() {
31        std::env::set_var(key, value);
32    }
33}