dotenv-verbatim 0.2.0

A .env loader that takes the value verbatim: no expansion, no escapes, no inline comments; one malformed line is skipped, not the rest of the file
Documentation
//! A `.env` loader that takes the value verbatim: everything after the first `=`, with no
//! expansion, no escape processing and no inline-comment stripping.
//! A line that cannot be a key/value pair is skipped by number, never the rest of the file, and a
//! variable already present in the process environment wins. Rationale and evidence: README.

#![forbid(unsafe_code)]

mod parse;

pub use crate::parse::parse;
pub use crate::parse::Entry;
pub use crate::parse::Parsed;

use std::path::Path;

/// Read the file and set every variable it declares that the process does not have yet.
/// A missing or unreadable file is not an error: the deployed environment provides real vars.
/// A key repeated inside the file keeps its first value, which the later pass then sees as set.
pub fn load(path: &Path) {
    let content = match std::fs::read_to_string(path) {
        Ok(content) => content,
        Err(_) => return,
    };
    for entry in parse(&content).entries {
        set_if_absent(entry.key, entry.value);
    }
}

/// dotenv semantics: a variable already in the process environment wins over the file.
fn set_if_absent(key: &str, value: &str) {
    if std::env::var_os(key).is_none() {
        std::env::set_var(key, value);
    }
}