Parsing and lookup
A Document holds one source string and the items a dialect reads from it.
It never changes the source: printing a document returns the original bytes.
Parse functions
Section titled “Parse functions”| Function | Returns | On errors |
|---|---|---|
parse(source) |
(Document, Vec<Diagnostic>) |
Keeps the bad text as an Invalid or Ignored item and reports it |
parse_strict(source) |
Result<Document, Diagnostic> |
Returns the first error |
parse_owned(String) |
(Document<'static>, Vec<Diagnostic>) |
Like parse, but the document owns the source |
parse_values(source) |
Result<BTreeMap<String, String>, Diagnostic> |
Returns the first error |
parse_values_compact(source) |
Result<CompactValues, Diagnostic> |
Returns the first error; lower retained memory |
parse_entries_compact(source) |
Result<CompactEntries, Diagnostic> |
Returns the first error; lower retained memory, preserves bare keys |
Each has a _with variant that takes a Dialect:
parse_with, parse_strict_with, parse_owned_with, parse_values_with,
parse_values_compact_with and parse_entries_compact_with.
Without one, the Portable dialect is used.
parse never fails. It recovers at the smallest safe boundary, so one broken
line does not hide the rest of the file:
use adotenv::{DiagnosticCode, ItemKind};
let source = "BAD KEY=value\nGOOD=kept\n";let (doc, diagnostics) = adotenv::parse(source);
assert_eq!(doc.get("GOOD"), Some("kept"));assert_eq!(doc.items().next().unwrap().kind(), ItemKind::Invalid);assert_eq!(diagnostics[0].code(), DiagnosticCode::InvalidKey);assert_eq!(doc.source(), source);parse_strict fails only on error diagnostics. Warnings and info diagnostics
do not fail it, and it does not return them. The codes are listed in
Diagnostic codes.
parse_values is the fast path when only final values matter. It keeps no
items or spans, applies the dialect’s duplicate and bare-key rules, and returns
the first error. The map, sorted by key, holds the same keys and values as
effective_entries below. It uses much less memory than a document.
parse_values_compact returns CompactValues instead of a map. It stores keys
and values in one buffer and one fixed-size entry per key, so retained memory
is lower while lookup stays O(log n) and iteration stays sorted. Use it when
a map is not required.
parse_owned moves the source into the document. Document::into_owned
copies the source only when the document still borrows it. Document::reparse
reads the same source with another dialect.
Limits
Section titled “Limits”ParseOptions adds opt-in bounds for untrusted input: source bytes, physical
lines, parsed items and collected diagnostics. The default has no byte, line or
item limit and keeps the established 10,000-diagnostic budget.
use adotenv::{parse_strict_with_options, Dialect, LimitKind, ParseOptions};
let options = ParseOptions::default().with_max_input_bytes(4);let error = parse_strict_with_options("A=long-value\n", Dialect::PORTABLE, options) .unwrap_err();let limit = error.limit().unwrap();assert_eq!(limit.kind(), LimitKind::InputBytes);assert_eq!(limit.limit(), 4);assert_eq!(limit.observed(), 13);Use the _with_options variants of the parse functions. A recovering parse
returns the source and a dotenv.limit-exceeded diagnostic; input byte, line
and item limits also add an invalid item for the remainder. parse_strict,
parse_values, parse_values_compact and parse_entries_compact return that
diagnostic as an error. Diagnostic::limit()
carries the structured kind, configured maximum and observed size or count.
ParseOptions item limits count statements for the values-only and compact
parsers.
The loader accepts the same limits through LoadOptions::with_limits and also
uses max_input_bytes to bound the read itself. A load returns
LoadError::Limit instead of reading an oversized file. See
Security and limits.
Items and entries
Section titled “Items and entries”Document::items() yields every item in source order. Items cover the source
without gaps, apart from a leading byte-order mark:
ItemKind |
Item |
|---|---|
Blank |
An empty or whitespace-only line |
Comment |
A whole-line comment |
Entry |
A declaration: KEY=value or a bare KEY |
Invalid |
A statement with an error, kept as written |
Ignored |
A statement the dialect skips, with a warning |
Document::entries() yields the declarations, including duplicates and
declarations that do not take part in lookup. Item and Entry are borrowed
views. Their id() is a zero-based index into items() or entries() of this
document, and Entry::item_id() gives the item that holds an entry. IDs
belong to one document: a reparsed document, or one parsed from edited text,
has its own.
An Item has kind(), span(), raw(), line_ending(), content_span(),
comment_text() for comments, diagnostic() and invalid_code() for invalid
and ignored items and for entries that break a dialect policy, and entry() for
declarations.
Most items are one line, including its line ending. A multiline quoted value
makes one item of several lines, and some dialects, such as Compose, can read
several statements from one line. line_ending() is Lf, CrLf, Cr or
None. Portable, Node dotenv and python-dotenv end a line at a lone CR, and
Portable reports it as dotenv.lone-cr. Compose and Docker end lines only at
LF and keep a lone CR as text inside the line.
Lookup
Section titled “Lookup”A dialect decides which declarations take part in lookup and which duplicate wins. In Portable, every declaration takes part and the last one wins.
let doc = adotenv::parse_strict("A=1\nA=2\nB=3\n").unwrap();
assert_eq!(doc.get("A"), Some("2"));assert_eq!(doc.get_entry("A").unwrap().line(), 2);assert_eq!(doc.get_all("A").count(), 2);assert!(doc.unique("A").is_err());
let winners: Vec<_> = doc .effective_entries() .map(|entry| (entry.key(), entry.value())) .collect();assert_eq!(winners, [("A", "2"), ("B", "3")]);| Method | Returns |
|---|---|
get(key) |
The effective value |
get_entry(key) |
The declaration that wins lookup |
get_all(key) |
Every declaration of the key, in source order |
unique(key) |
The only declaration, or EditError::Missing or EditError::Duplicate |
effective_entries() |
The winning declaration of each key that has an effective value, in source order |
lookup_index() |
A LookupIndex for many lookups |
get scans the entries. For many lookups, build lookup_index() once; it has
get, get_entry, unique, len and is_empty. effective_entries,
parse_values and LookupIndex::get / get_entry leave out empty keys, and
effective_entries also leaves out a key whose winning declaration is bare,
unless the dialect reads bare keys as empty. LookupIndex::unique matches
Document::unique across every declaration, including bare keys and empty keys.
Compose still exposes an empty key through get("").
Bare and empty declarations
Section titled “Bare and empty declarations”KEY is a bare declaration with no value. KEY= and KEY="" are empty
assignments. The two stay distinct:
use adotenv::AssignmentState;
let doc = adotenv::parse_strict("BARE\nEMPTY=\n").unwrap();
assert_eq!(doc.get_entry("BARE").unwrap().state(), AssignmentState::Bare);assert_eq!(doc.get("BARE"), None);assert_eq!(doc.get_entry("EMPTY").unwrap().state(), AssignmentState::Empty);assert_eq!(doc.get("EMPTY"), Some(""));BareKeys::Empty makes a bare declaration read as an empty value in lookup,
while the entry itself stays bare: its state() is Bare and its value() is
None. A bare declaration never reads the process environment.
When you need compact read-only storage without a full Document,
parse_values_compact returns CompactValues (omitting bare declarations),
while parse_entries_compact returns CompactEntries (preserving winning bare
declarations as None under BareKeys::Preserve).
Raw tokens
Section titled “Raw tokens”An Entry gives the decoded value and the source tokens behind it:
let source = "export A=\"two words\" # note\n";let doc = adotenv::parse_strict(source).unwrap();let entry = doc.unique("A").unwrap();
assert!(entry.exported());assert_eq!(&source[entry.key_span()], "A");assert_eq!(entry.raw_value(), Some("\"two words\""));assert_eq!(entry.value(), Some("two words"));assert_eq!(entry.inline_comment(), Some("# note"));assert_eq!(entry.decoded_span_to_raw(0..3).unwrap(), 10..13);Entry::tokens() returns EntryTokens, with the byte range of every part of
the declaration: indent, export, space_after_export, key,
space_before_eq, eq, space_after_eq, value, body, quoted_tail,
trailing_space and inline_comment. quote() gives the quote style and
state() the assignment state. decoded_offset_to_raw and
decoded_span_to_raw map positions in the decoded value back to the source,
accounting for escapes.
Spans and positions
Section titled “Spans and positions”Every span is a byte range into the original source, byte-order mark
included. Entry and diagnostic lines are one-based. They count LF, CRLF and a
lone CR as line ends in every dialect, the same way as LineIndex, so in
Compose and Docker a lone CR starts a new line number even though it does not
end a statement.
LineIndex converts between byte offsets and zero-based line and column
positions. The column encoding is explicit:
use adotenv::{ColumnEncoding, Position};
let doc = adotenv::parse_strict("A=é\n").unwrap();let index = doc.line_index();let position = index.to_position(2, ColumnEncoding::Scalar).unwrap();
assert_eq!(position, Position { line: 0, column: 2 });assert_eq!(index.to_offset(position, ColumnEncoding::Scalar).unwrap(), 2);assert_eq!(index.to_position(4, ColumnEncoding::Utf16).unwrap().column, 3);ColumnEncoding is Utf8 (bytes), Scalar (Unicode scalar values) or
Utf16 (code units, as in the Language Server Protocol). Scalar columns are
not terminal cell widths. A position that is out of range, between CR and LF,
inside a UTF-8 sequence with Scalar or Utf16 columns, or between the halves
of a surrogate pair returns a PositionError; positions are never rounded or
clamped. Utf8 columns can address any byte.
Position::one_based() converts a position for display.
Document::line_index() builds a new index on each call, so keep it when
converting many positions. A leading byte-order mark is part of line 0: it
takes 3 Utf8 columns, or 1 Scalar or Utf16 column, before the first
character. Build the index with without_bom() when the consumer hides the
mark:
use adotenv::{ColumnEncoding, LineIndex, Position};
let source = "\u{feff}A=1\n";let index = LineIndex::new(source).without_bom();assert_eq!( index.to_position(3, ColumnEncoding::Scalar).unwrap(), Position { line: 0, column: 0 });assert_eq!( index.to_offset(Position { line: 0, column: 0 }, ColumnEncoding::Scalar).unwrap(), 3);The default still counts the mark in 1.x. Offsets inside a hidden mark map to
column 0, and column 0 converts back to the first byte after it. When a dialect
uses another BomPolicy, call LineIndex::with_bom_policy(policy) to match it:
BomPolicy::Strip hides one leading mark (matching without_bom()),
BomPolicy::StripAll hides every consecutive leading mark, and BomPolicy::Keep
and BomPolicy::Error keep leading marks in line 0 columns.
Groups and comments
Section titled “Groups and comments”A group is a run of items between blank lines. These views help tools that reason about layout:
Document::groups()iterates the groups. EachGrouphasitems(),entries(),comments(),leading_comments()andspan().Document::header()is the run of items before the first entry, if any. Unlike a group, it can contain blank lines.Entry::leading_comments()yields the whole-line comments directly above an entry. A blank line ends the attachment.
let doc = adotenv::parse_strict("# database\nDB=x\n\n# cache\nCACHE=y\n").unwrap();let comments: Vec<_> = doc .get_entry("CACHE") .unwrap() .leading_comments() .filter_map(|item| item.comment_text()) .collect();
assert_eq!(doc.groups().count(), 2);assert_eq!(comments, [" cache"]);Untrusted input
Section titled “Untrusted input”Input must be UTF-8. A Document retains item and token metadata for the
whole source, so check the size of untrusted input before parsing it.
Dialect::with_line_byte_limit rejects physical lines longer than a limit with
dotenv.line-too-long; the Docker preset uses 65,535 bytes and the others have
no limit. See Security and limits.