1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Literal, lossless `.env` parsing and verified editing.
//!
//! adotenv reads `.env` files the way tools need them. It keeps every byte of the source,
//! leaves values literal (`${HOME}` is text, not an expansion), gives every token a byte span
//! and checks every edit by parsing the result again before returning it. It is built for
//! linters, formatters, editors, scanners and other tools that maintain configuration files.
//!
//! The core never reads files or environment variables, never expands references and never
//! runs commands. Each of those happens only through an explicit API or an optional feature.
//!
//! The [user guide](https://asamarts.github.io/adotenv/) explains the behavior in more depth.
//! This reference documents each item.
//!
//! # Quick start
//!
//! ```
//! let source = "# app\nexport PORT=8080\nURL=\"${HOST}/api\" # base\n";
//! let doc = adotenv::parse_strict(source)?;
//!
//! // Values are literal: references are not expanded.
//! assert_eq!(doc.get("PORT"), Some("8080"));
//! assert_eq!(doc.get("URL"), Some("${HOST}/api"));
//!
//! // A document prints back the exact source.
//! assert_eq!(doc.to_string(), source);
//!
//! // An edit returns the new text, already parsed again and checked.
//! let edited = doc.set("PORT", "9090")?;
//! assert_eq!(edited.as_str(), "# app\nexport PORT=9090\nURL=\"${HOST}/api\" # base\n");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! [`parse_strict`] returns the first error instead of a document. [`parse`] never fails: it
//! returns a document and a list of [`Diagnostic`]s, so tools can inspect and report on a broken
//! file without losing any of it.
//!
//! ```
//! use adotenv::{DiagnosticCode, ItemKind};
//!
//! let (doc, diagnostics) = adotenv::parse("BAD KEY=value\nGOOD=kept\n");
//! 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!(diagnostics[0].line(), 1);
//! ```
//!
//! # Where to start
//!
//! | Task | Start with |
//! |---|---|
//! | Parse text | [`parse`], [`parse_strict`], [`parse_owned`], [`parse_values`], and their `_with` variants that take a [`Dialect`] |
//! | Look up values | [`Document::get`], [`Document::get_entry`], [`Document::get_all`], [`Document::effective_entries`], [`Document::lookup_index`] |
//! | Walk the source | [`Document::items`], [`Document::entries`], [`Item`], [`Entry`], [`EntryTokens`] |
//! | Report problems | [`Diagnostic`], [`DiagnosticCode`], [`Severity`] |
//! | Choose reading rules | [`Dialect`] and its presets |
//! | Edit | [`Document::set`], [`Document::remove`], [`Document::rename`], [`Document::insert`], [`Document::comment`], [`Document::uncomment`], [`Document::set_export`], or [`Document::edit`] with a batch of [`Edit`]s |
//! | Set or remove one key of Portable text in one call | [`set`], [`remove`] |
//! | Quote a value for a file | [`encode_value`], [`encode_value_with`] |
//! | Format and sort | [`Document::format`] with [`FormatOptions`], [`Document::sort`] |
//! | Compare two files | [`Document::compare`] |
//! | Write an example file | [`Document::example`] |
//! | Check portability across dialects | [`Document::portability`] |
//! | Groups and comments | [`Document::groups`], [`Document::header`], [`Entry::leading_comments`] |
//! | Lines and columns | [`Document::line_index`], [`LineIndex`], [`Position`], [`ColumnEncoding`] |
//! | Find `$VAR` references | [`Entry::references`], [`ReferenceRules`] |
//! | Resolve references | [`Document::resolve`] with [`ExpandOptions`] |
//! | Load files from disk | the [`load`] module |
//! | Deserialize into a struct | [`Document::deserialize`] |
//! | Run command substitutions | [`Document::resolve_with_commands`] and the [`exec`] module |
//!
//! # Features
//!
//! | Feature | Default | Adds |
//! |---|---|---|
//! | `std` | yes | Standard library support. Required by `load` and `exec`. |
//! | `load` | no | The [`load`] module, which reads `.env` cascades and file lists from disk. |
//! | `serde` | no | [`Document::deserialize`] and [`DeserializeError`]; with `load` also [`load::LoadedEnvironment::deserialize`]. |
//! | `exec` | no | The [`exec`] module, which runs command substitutions through the platform shell. |
//!
//! Without default features the crate is `no_std` and uses `alloc`:
//!
//! ```toml
//! adotenv = { version = "1", default-features = false }
//! ```
//!
//! The only dependency is `serde`, and only with the `serde` feature. The crate forbids unsafe
//! code with every feature combination and supports Rust 1.81 and later.
//!
//! # Dialects
//!
//! Every parse uses a [`Dialect`]: the syntax and literal lookup rules of one `.env` reader.
//! The functions without a suffix use [`Dialect::PORTABLE`]. The other presets follow Node
//! dotenv, python-dotenv, Docker Compose and Docker `--env-file`, and a custom dialect starts
//! from a preset and changes single rules. No dialect expands references, reads the process
//! environment or loads files.
//!
//! ```
//! use adotenv::{parse_with, Dialect};
//!
//! let (doc, _) = parse_with("A=a#b\n", Dialect::NODE_DOTENV);
//! assert_eq!(doc.get("A"), Some("a"));
//! assert_eq!(doc.reparse(Dialect::DOCKER_RAW).0.get("A"), Some("a#b"));
//! ```
//!
//! # Source, spans and positions
//!
//! A [`Document`] borrows its source, or owns it after [`parse_owned`] or
//! [`Document::into_owned`]. Its items cover the whole source without gaps, malformed
//! statements included; only a leading byte-order mark that the dialect strips belongs to no
//! item. Every span is a byte range into the original text, byte-order mark included.
//!
//! [`Entry::line`] and [`Diagnostic::line`] are one-based. [`LineIndex`] converts between byte
//! offsets and zero-based lines and columns in UTF-8 bytes, Unicode scalar values or UTF-16
//! code units, and [`Position::one_based`] converts a position for display. Lines and line
//! numbers count LF, CRLF and a lone CR as line ends, whatever the dialect.
//!
//! # Verified edits
//!
//! Methods such as [`Document::set`] select the only declaration of a key. A batch of
//! [`Edit`]s selects entries and items by id, so it can also change one of several
//! declarations of a key. Every edit and every batch is applied to the whole source, parsed
//! again with the same dialect and checked: the requested change happened and nothing else
//! changed meaning. [`VerifiedEdits`] then gives the new text and the source-relative
//! [`TextEdit`]s, for example to send to an editor. [`Document::format`] and
//! [`Document::sort`] are verified the same way.
//!
//! # References and resolution
//!
//! Values stay literal. [`Entry::references`] lists `$VAR`, `${VAR:-default}` and
//! `$(command)` forms with their spans without resolving anything. [`Document::resolve`]
//! substitutes references only on request, from a lookup closure you pass, with explicit
//! scope, precedence and size limits. Commands run only through a callback you pass to
//! [`Document::resolve_with_commands`].
//!
//! # Secrets
//!
//! Values may be secrets. `Debug` output of documents, items, entries, edit and format results,
//! comparisons, loaded environments and resolved values includes them, and so do the messages
//! of serde conversion errors. Do not log them where values are secret. [`Document::example`]
//! removes assigned values but keeps comments verbatim, including any secret written in a
//! comment.
//!
//! # Related crates
//!
//! - [`adotenv-process`](https://docs.rs/adotenv-process) applies a loaded environment to the
//! current process.
//! - [`adotenv-config`](https://docs.rs/adotenv-config) and
//! [`adotenv-figment`](https://docs.rs/adotenv-figment) provide values to `config` and
//! Figment.
//! - [`adotenv-cli`](https://crates.io/crates/adotenv-cli) checks, formats, compares and loads
//! files from a shell.
//!
//! # Stability
//!
//! The public API follows semantic versioning. Enums and option structs are
//! `#[non_exhaustive]`, so a minor release can add variants, fields and builders.
// Links to items behind optional features resolve only when those features are enabled.
// docs.rs and CI build the documentation with every feature.
extern crate alloc;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use DeserializeError;
pub use ;
/// Compile and execute the README examples with the crate's documentation.
;
/// Compile and execute the examples of the documentation site.
///
/// Every book page is listed, so a Rust example added to any page is tested.
/// Pages with examples that need optional features are tested with them.