Skip to main content

Module env

Module env 

Source
Expand description

Dotenv / env-file parser (env feature).

Format: env

§Behaviour

  • Splits the UTF-8 input into lines; blank lines and # comments are ignored, and an optional leading export is stripped.
  • Each remaining KEY=VALUE line becomes a string entry. Values may be double-quoted (with \n, \r, \t, \", \\ escapes), single-quoted (taken literally), or unquoted (used verbatim). The result is always a Value::Map of Value::Strings.
  • When the source carries a separator option, keys are split on that separator and nested into sub-maps (e.g. BAR__BAZ=val with separator=__ becomes {bar: {baz: "val"}}).
  • Each key carries its line/column Location; for single-line input the line/column are omitted. The root map has no line.
  • Non-UTF-8 input fails with Error::InvalidUtf8; there are no syntax errors otherwise. is_format_supported returns Some(true) when any non-comment line contains =, else Some(false).

§Example

use tanzim_parse::{Parse, env::Env};
use tanzim_source::SourceBuilder;

let source = SourceBuilder::new()
    .with_source("file")
    .with_resource(".env")
    .build()
    .unwrap();
let value = Env::new()
    .parse(&source, b"SERVER_HOST=\"127.0.0.1\"\n")
    .unwrap();
assert_eq!(
    value.value().as_map().unwrap().get("server_host").unwrap().value().as_string().unwrap(),
    "127.0.0.1"
);

Structs§

Env
Parser for the env format: dotenv / env-file KEY=VALUE lines into a string map.

Functions§

unparse
Serialize a Value map into dotenv / env-file KEY=VALUE lines.