toml-spanner 1.0.2

High Performance Toml parser and deserializer that preserves span information with fast compile times.
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
use crate::emit::partition::ensure_body_order;
use crate::{Array, ArrayStyle, Item, Kind, Table, TableStyle, Value, ValueMut};
impl<'de> Table<'de> {
    fn normalize_inner(&mut self) -> &'de NormalizedTable<'de> {
        normalize_entries(self.value.entries_mut(), true);
        ensure_body_order(self.value.entries_mut());
        // SAFETY: NormalizedTable is #[repr(transparent)] over Table, so the
        // cast preserves layout. The normalize_item calls above have ensured
        // every item in the tree is reachable by the emit algorithm. The
        // NormalizedTable wrapper is a marker type that encodes this guarantee
        // at the type level.
        unsafe { &*(self as *const Table<'de> as *const NormalizedTable<'de>) }
    }

    /// Recursively corrects table and array kinds so the tree is valid
    /// for emission.
    ///
    /// Promotes implicit tables to headers when they contain values that
    /// would otherwise be unreachable, downgrades invalid
    /// array-of-tables to inline arrays, and fixes kind mismatches in
    /// nested contexts (e.g. a header table inside an inline table).
    pub(crate) fn normalize(&mut self) -> &'de NormalizedTable<'de> {
        self.normalize_inner()
    }

    /// Checks whether this table tree is already valid for emission
    /// without modifying it.
    ///
    /// Returns `Some` if every item is reachable by the emit algorithm,
    /// `None` otherwise. Use [`normalize`](Self::normalize) to fix an
    /// invalid tree instead.
    #[allow(dead_code)]
    pub(crate) fn try_as_normalized(&self) -> Option<&NormalizedTable<'de>> {
        if is_valid(self, true) {
            // SAFETY: NormalizedTable is #[repr(transparent)] over Table.
            // The validation confirmed the tree is emit-safe.
            Some(unsafe { &*(self as *const Table<'de> as *const NormalizedTable<'de>) })
        } else {
            None
        }
    }
}

#[repr(transparent)]
pub(crate) struct NormalizedTable<'de>(Table<'de>);

impl<'de> NormalizedTable<'de> {
    pub(crate) fn table(&self) -> &Table<'de> {
        &self.0
    }
}

impl<'de> std::ops::Deref for NormalizedTable<'de> {
    type Target = Table<'de>;
    fn deref(&self) -> &Table<'de> {
        &self.0
    }
}

/// Normalizes an item inside a frozen (inline/dotted) context.
/// `allow_dotted` is true for named table entries (dotted keys like `{a.b = 1}`
/// are valid), false for positional items like array elements (no key to dot).
fn normalize_inline(item: &mut Item<'_>, allow_dotted: bool) {
    match item.value_mut() {
        ValueMut::Table(sub) => {
            if allow_dotted {
                match sub.style() {
                    TableStyle::Inline | TableStyle::Dotted => {}
                    _ => sub.set_style(TableStyle::Inline),
                }
            } else {
                sub.set_style(TableStyle::Inline);
            }
            let is_dotted = sub.style() == TableStyle::Dotted;
            for (_, child) in sub.value.entries_mut().iter_mut() {
                normalize_inline(child, true);
            }
            // Empty dotted tables emit nothing; promote to frozen so
            // they appear as `key = {}`.
            if is_dotted && sub.value.is_empty() {
                sub.set_style(TableStyle::Inline);
            }
        }
        ValueMut::Array(arr) => {
            arr.set_style(ArrayStyle::Inline);
            arr.clear_expanded();
            for elem in &mut *arr {
                normalize_inline(elem, false);
            }
        }
        _ => {}
    }
}

/// Returns `true` if every item in the table tree is reachable by the
/// emit algorithm. Mirror of the old `validate_table` but returns bool.
#[allow(dead_code)]
fn is_valid(table: &Table<'_>, body_emitted: bool) -> bool {
    for (_, item) in table {
        if item.meta.is_auto_style() {
            return false;
        }
        if item.has_dotted_bit() {
            let Some(sub) = item.as_table() else {
                return false;
            };
            if !is_valid(sub, body_emitted) {
                return false;
            }
        } else if item.has_header_bit() {
            let Some(sub) = item.as_table() else {
                return false;
            };
            if !is_valid(sub, true) {
                return false;
            }
        } else if item.is_implicit_table() {
            let Some(sub) = item.as_table() else {
                return false;
            };
            if !is_valid(sub, false) {
                return false;
            }
        } else if item.is_aot() {
            let Some(arr) = item.as_array() else {
                return false;
            };
            for elem in arr {
                let Some(sub) = elem.as_table() else {
                    return false;
                };
                if !is_valid(sub, true) {
                    return false;
                }
            }
        } else if !body_emitted {
            return false;
        }
    }
    true
}

fn is_small_value(item: &Item<'_>) -> bool {
    match item.value() {
        Value::String(text) => {
            if text.len() > 30 {
                return false;
            }
            for byte in text.as_bytes() {
                if byte.is_ascii_control() {
                    return false;
                }
            }
            true
        }
        Value::Array(array) => array.is_empty(),
        Value::Table(table) => table.is_empty(),
        _ => true,
    }
}

fn resolve_auto_table(sub: &mut Table<'_>, body_emitted: bool) {
    if !sub.meta.is_auto_style() {
        return;
    }
    sub.meta.clear_auto_style();
    if !body_emitted {
        return;
    }

    match sub.entries() {
        [(_, a), (_, b)] if is_small_value(a) && is_small_value(b) => (),
        [(_, a)] if is_small_value(a) => (),
        [] => (),
        _ => return,
    };

    sub.set_style(TableStyle::Inline);
}

/// Estimates the inline rendered width of a value in characters.
/// Returns `None` if the value cannot be reasonably inlined (e.g.
/// non-empty nested containers).
fn inline_width(item: &Item<'_>) -> Option<usize> {
    match item.value() {
        Value::String(s) => {
            if s.len() > 40 {
                return None;
            }
            for &b in s.as_bytes() {
                if b.is_ascii_control() {
                    return None;
                }
            }
            Some(s.len() + 2) // quotes
        }
        Value::Integer(_) => Some(6), // conservative estimate
        Value::Float(_) => Some(8),   // conservative estimate
        Value::Boolean(b) => Some(if *b { 4 } else { 5 }),
        Value::DateTime(_) => Some(25), // conservative estimate
        Value::Array(a) if a.is_empty() => Some(2),
        Value::Table(t) if t.is_empty() => Some(2),
        _ => None,
    }
}

fn resolve_auto_array(arr: &mut Array<'_>) {
    if !arr.meta.is_auto_style() {
        return;
    }
    arr.meta.clear_auto_style();

    if arr.is_empty() {
        arr.set_style(ArrayStyle::Inline);
        return;
    }

    let all_tables = arr.iter().all(|e| e.kind() == Kind::Table);
    if all_tables {
        arr.set_style(ArrayStyle::Header);
        return;
    }

    // Estimate inline width: `[` + elements + `, ` separators + `]`
    let mut width: usize = 2; // brackets
    let mut fits_inline = true;
    for (i, elem) in arr.iter().enumerate() {
        if i > 0 {
            width += 2; // ", "
        }
        let Some(w) = inline_width(elem) else {
            fits_inline = false;
            break;
        };
        width += w;
        if width > 40 {
            fits_inline = false;
            break;
        }
    }
    if fits_inline {
        arr.set_style(ArrayStyle::Inline);
    } else {
        arr.set_expanded();
        arr.set_style(ArrayStyle::Inline);
    }
}

fn normalize_entries(entries: &mut [(crate::Key<'_>, Item<'_>)], body_emitted: bool) -> bool {
    let mut has_body = false;
    for (_, item) in entries.iter_mut() {
        has_body |= normalize_item(item, body_emitted);
    }
    has_body
}

fn renormalize_promoted_header_children(entries: &mut [(crate::Key<'_>, Item<'_>)]) {
    for (_, item) in entries.iter_mut() {
        if item.as_table().is_some() {
            normalize_item(item, true);
        }
    }
}

pub(crate) fn normalize_item(item: &mut Item<'_>, body_emitted: bool) -> bool {
    match item.value_mut() {
        ValueMut::Table(sub) => {
            resolve_auto_table(sub, body_emitted);
            let has_body = normalize_table(sub, body_emitted);
            ensure_body_order(sub.value.entries_mut());
            has_body
        }
        ValueMut::Array(arr) => {
            resolve_auto_array(arr);
            normalize_array(arr)
        }
        _ => true,
    }
}

fn normalize_table(sub: &mut Table<'_>, body_emitted: bool) -> bool {
    let kind = sub.style();

    match kind {
        TableStyle::Inline => {
            for (_, item) in sub.value.entries_mut().iter_mut() {
                normalize_inline(item, true);
            }
            return true;
        }
        TableStyle::Header => {
            normalize_entries(sub.value.entries_mut(), true);
            return false;
        }
        _ => {}
    }

    // TableKind::Implicit or TableKind::Dotted.
    // Dotted tables inherit body_emitted from parent; implicit always false.
    let effective_body = kind == TableStyle::Dotted && body_emitted;

    // Empty implicit/dotted tables produce no emit output (no body items,
    // no subsections). Promote to header so they appear as `[section]`.
    // Exception: empty Dotted in a body context stays as Inline (`key = {}`)
    // to preserve body-level ordering.
    if sub.value.is_empty() {
        if effective_body {
            sub.set_style(TableStyle::Inline);
            return true;
        } else {
            sub.set_style(TableStyle::Header);
            return false;
        }
    }

    let mut has_body = normalize_entries(sub.value.entries_mut(), effective_body);

    if !effective_body && has_body {
        sub.set_style(TableStyle::Header);
        // Promotion to Header only changes context-sensitive descendants
        // reachable through table children; arrays and scalars are already
        // normalized identically in either context.
        renormalize_promoted_header_children(sub.value.entries_mut());
        return false;
    }

    // After normalizing children, a DOTTED table whose children were all
    // promoted to subsection items (HEADER/IMPLICIT/AOT) has no body to
    // emit through the dotted-key path. Try demoting children to
    // body-level first to preserve the DOTTED kind. Only promote to
    // IMPLICIT if demotion doesn't produce body items.
    let no_body_after_norm = if kind != TableStyle::Dotted {
        false
    } else if !effective_body {
        true // We didn't return from the Header promotion, so has_body is false
    } else {
        !has_body
    };

    if no_body_after_norm {
        let mut demoted_has_body = false;
        for (_, child) in sub.value.entries_mut().iter_mut() {
            match child.value_mut() {
                ValueMut::Table(ct) => {
                    if matches!(ct.style(), TableStyle::Header | TableStyle::Implicit) {
                        if ct.is_empty() {
                            ct.set_style(TableStyle::Inline);
                            demoted_has_body = true;
                        } else {
                            ct.set_style(TableStyle::Dotted);
                            demoted_has_body |= normalize_table(ct, body_emitted);
                        }
                    } else {
                        demoted_has_body = true;
                    }
                }
                ValueMut::Array(arr) => {
                    if arr.style() == ArrayStyle::Header {
                        arr.set_expanded();
                        arr.set_style(ArrayStyle::Inline);
                        demoted_has_body |= normalize_array(arr);
                    } else {
                        demoted_has_body = true;
                    }
                }
                _ => {
                    demoted_has_body = true;
                }
            }
        }
        if !demoted_has_body {
            sub.set_style(TableStyle::Implicit);
            has_body = false;
        } else {
            has_body = true;
        }
    }

    has_body
}

/// Normalizes an element inside an expanded array.
///
/// Like `normalize_inline` but allows nested arrays to stay expanded.
/// `allow_dotted` is true for named table entries, false for array elements.
fn normalize_expanded_child(item: &mut Item<'_>, allow_dotted: bool) {
    match item.value_mut() {
        ValueMut::Table(sub) => {
            if allow_dotted {
                match sub.style() {
                    TableStyle::Inline | TableStyle::Dotted => {}
                    _ => sub.set_style(TableStyle::Inline),
                }
            } else {
                sub.set_style(TableStyle::Inline);
            }
            for (_, child) in sub.value.entries_mut().iter_mut() {
                normalize_expanded_child(child, true);
            }
        }
        ValueMut::Array(arr) => {
            resolve_auto_array(arr);
            normalize_array(arr);
        }
        _ => {}
    }
}

fn normalize_array(arr: &mut Array<'_>) -> bool {
    let mut kind = arr.style();

    // AOT must be non-empty with all-table elements; otherwise downgrade.
    if let ArrayStyle::Header = kind {
        let mut all_tables = !arr.is_empty();
        for e in arr.iter() {
            if e.kind() != Kind::Table {
                all_tables = false;
                break;
            }
        }
        if !all_tables {
            kind = ArrayStyle::Inline;
            arr.set_expanded();
        }
    }
    arr.set_style(kind);

    match kind {
        ArrayStyle::Header => {
            for elem in &mut *arr {
                let ValueMut::Table(sub) = elem.value_mut() else {
                    continue;
                };
                sub.set_style(TableStyle::Header);
                normalize_entries(sub.value.entries_mut(), true);
                ensure_body_order(sub.value.entries_mut());
            }
            false
        }
        ArrayStyle::Inline if arr.is_expanded() => {
            for elem in &mut *arr {
                normalize_expanded_child(elem, false);
            }
            true
        }
        ArrayStyle::Inline => {
            for elem in &mut *arr {
                normalize_inline(elem, false);
            }
            true
        }
    }
}