adotenv 0.6.0

A tooling-grade .env parser: literal values (no variable expansion), exact byte spans, and edits that leave the rest of the file untouched.
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Opt-in `serde` deserialization of literal documents.
//!
//! The deserializer exposes effective declarations as a string-valued map.
//! Literal values are parsed into Serde primitives when requested; nested
//! sequences/maps and enum representations are not invented. Errors carry the
//! source line for value conversion failures.

use alloc::{
    collections::BTreeSet,
    format,
    string::{String, ToString},
    vec::Vec,
};
use core::fmt;

use serde::de::{self, DeserializeOwned, Deserializer, MapAccess, Visitor};

use crate::{BareKeys, Document};

/// A serde error tied to the literal document line where possible.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeserializeError {
    line: usize,
    key: Option<String>,
    message: String,
}

impl DeserializeError {
    fn new(message: impl Into<String>) -> Self {
        Self {
            line: 0,
            key: None,
            message: message.into(),
        }
    }
    fn at(line: usize, key: String, message: impl Into<String>) -> Self {
        Self {
            line,
            key: Some(key),
            message: message.into(),
        }
    }
    fn with_context(mut self, line: usize, key: String) -> Self {
        if self.line == 0 {
            self.line = line;
            self.key = Some(key);
        }
        self
    }
    /// One-based source line, or zero when Serde produced a structural error
    /// without a value location.
    #[must_use]
    pub const fn line(&self) -> usize {
        self.line
    }
    /// Key associated with the error, when available.
    #[must_use]
    pub fn key(&self) -> Option<&str> {
        self.key.as_deref()
    }
    /// Human-readable error message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for DeserializeError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.line == 0 {
            formatter.write_str(&self.message)
        } else {
            write!(
                formatter,
                "line {}: {}{}",
                self.line,
                self.message,
                self.key
                    .as_ref()
                    .map_or(String::new(), |key| format!(" (key {key:?})"))
            )
        }
    }
}

impl core::error::Error for DeserializeError {}

impl de::Error for DeserializeError {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self::new(message.to_string())
    }
}

impl Document<'_> {
    /// Deserialize effective literal declarations into a Serde type.
    ///
    /// Duplicate keys follow the selected dialect; nonparticipating
    /// declarations and empty keys are ignored. Bare declarations are exposed
    /// only when the dialect’s `BareKeys::Empty` policy makes them effective
    /// empty strings. Values remain literal and are never expanded.
    ///
    /// # Errors
    /// Returns [`DeserializeError`] when a value cannot be parsed as the
    /// requested type. Value-conversion errors carry the declaration line.
    ///
    /// # Examples
    ///
    /// ```
    /// use adotenv::parse_strict;
    ///
    /// #[derive(serde::Deserialize)]
    /// struct Config {
    ///     host: String,
    ///     port: u16,
    /// }
    ///
    /// let doc = parse_strict("host=localhost\nport=8080\n").unwrap();
    /// let config: Config = doc.deserialize().unwrap();
    /// assert_eq!(config.host, "localhost");
    /// assert_eq!(config.port, 8080);
    /// ```
    pub fn deserialize<T: DeserializeOwned>(&self) -> Result<T, DeserializeError> {
        T::deserialize(DocumentDeserializer { document: self })
    }
}

struct DocumentDeserializer<'doc, 'src> {
    document: &'doc Document<'src>,
}

impl<'de> Deserializer<'de> for DocumentDeserializer<'_, '_> {
    type Error = DeserializeError;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_map(EntriesAccess {
            entries: entries(self.document).into_iter(),
            current: None,
        })
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier ignored_any
    }
}

fn entries(document: &Document<'_>) -> Vec<(String, String, usize)> {
    let mut seen = BTreeSet::new();
    let mut entries = Vec::new();
    for entry in document.entries() {
        if !entry.participates_in_lookup() || entry.key().is_empty() {
            continue;
        }
        if !seen.insert(entry.key().to_string()) {
            continue;
        }
        let Some(effective) = document.get_entry(entry.key()) else {
            continue;
        };
        let value = if let Some(value) = effective.value() {
            value.to_string()
        } else if document.dialect().bare_keys == BareKeys::Empty {
            String::new()
        } else {
            continue;
        };
        entries.push((effective.key().to_string(), value, effective.line()));
    }
    entries
}

struct EntriesAccess {
    entries: alloc::vec::IntoIter<(String, String, usize)>,
    current: Option<(String, String, usize)>,
}

impl<'de> MapAccess<'de> for EntriesAccess {
    type Error = DeserializeError;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
    where
        K: de::DeserializeSeed<'de>,
    {
        let Some((key, value, line)) = self.entries.next() else {
            return Ok(None);
        };
        self.current = Some((key.clone(), value, line));
        seed.deserialize(KeyDeserializer { key, line }).map(Some)
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
    where
        V: de::DeserializeSeed<'de>,
    {
        let Some((key, value, line)) = self.current.take() else {
            return Err(DeserializeError::new("value requested without a key"));
        };
        seed.deserialize(ValueDeserializer { key, value, line })
    }

    fn size_hint(&self) -> Option<usize> {
        Some(self.entries.len())
    }
}

struct KeyDeserializer {
    key: String,
    line: usize,
}

impl<'de> Deserializer<'de> for KeyDeserializer {
    type Error = DeserializeError;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor
            .visit_str(&self.key)
            .map_err(|error: DeserializeError| error.with_context(self.line, String::new()))
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier ignored_any
    }
}

struct ValueDeserializer {
    key: String,
    value: String,
    line: usize,
}

impl ValueDeserializer {
    fn invalid(&self, expected: &str) -> DeserializeError {
        DeserializeError::at(
            self.line,
            self.key.clone(),
            format!("expected {expected}, found {:?}", self.value),
        )
    }

    fn context<T>(&self, result: Result<T, DeserializeError>) -> Result<T, DeserializeError> {
        result.map_err(|error: DeserializeError| error.with_context(self.line, self.key.clone()))
    }
}

macro_rules! deserialize_number {
    ($method:ident, $visit:ident, $ty:ty, $name:literal) => {
        fn $method<V>(self, visitor: V) -> Result<V::Value, Self::Error>
        where
            V: Visitor<'de>,
        {
            let value = self.value.parse::<$ty>().map_err(|_| self.invalid($name))?;
            let result = visitor.$visit(value);
            self.context(result)
        }
    };
}

impl<'de> Deserializer<'de> for ValueDeserializer {
    type Error = DeserializeError;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor
            .visit_str(&self.value)
            .map_err(|error: DeserializeError| error.with_context(self.line, self.key.clone()))
    }

    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let value = self
            .value
            .parse::<bool>()
            .map_err(|_| self.invalid("a boolean"))?;
        let result = visitor.visit_bool(value);
        self.context(result)
    }

    deserialize_number!(deserialize_i8, visit_i8, i8, "an i8");
    deserialize_number!(deserialize_i16, visit_i16, i16, "an i16");
    deserialize_number!(deserialize_i32, visit_i32, i32, "an i32");
    deserialize_number!(deserialize_i64, visit_i64, i64, "an i64");
    deserialize_number!(deserialize_i128, visit_i128, i128, "an i128");
    deserialize_number!(deserialize_u8, visit_u8, u8, "a u8");
    deserialize_number!(deserialize_u16, visit_u16, u16, "a u16");
    deserialize_number!(deserialize_u32, visit_u32, u32, "a u32");
    deserialize_number!(deserialize_u64, visit_u64, u64, "a u64");
    deserialize_number!(deserialize_u128, visit_u128, u128, "a u128");
    deserialize_number!(deserialize_f32, visit_f32, f32, "an f32");
    deserialize_number!(deserialize_f64, visit_f64, f64, "an f64");

    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let mut chars = self.value.chars();
        let (Some(character), None) = (chars.next(), chars.next()) else {
            return Err(self.invalid("one character"));
        };
        let result = visitor.visit_char(character);
        self.context(result)
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let result = visitor.visit_str(&self.value);
        self.context(result)
    }

    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        self.deserialize_str(visitor)
    }

    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let result = visitor.visit_bytes(self.value.as_bytes());
        self.context(result)
    }

    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        self.deserialize_bytes(visitor)
    }

    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let result = visitor.visit_some(self);
        result
    }

    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        if self.value.is_empty() {
            visitor.visit_unit()
        } else {
            Err(self.invalid("an empty unit"))
        }
    }

    fn deserialize_unit_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        self.deserialize_unit(visitor)
    }

    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not a sequence"))
    }

    fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not a tuple"))
    }

    fn deserialize_tuple_struct<V>(
        self,
        _name: &'static str,
        _len: usize,
        _visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not a tuple struct"))
    }

    fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not a map"))
    }

    fn deserialize_struct<V>(
        self,
        _name: &'static str,
        _fields: &'static [&'static str],
        _visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not a struct"))
    }

    fn deserialize_enum<V>(
        self,
        _name: &'static str,
        _variants: &'static [&'static str],
        _visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(self.invalid("a scalar value, not an enum"))
    }

    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        self.deserialize_str(visitor)
    }

    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_unit()
    }
}