tomlini 0.1.0

SAX TOML/INI parser and editor. Zero-dependency, no footguns.
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Validation pass for `FlatDoc`.
//!
//! Validates structural TOML rules, duplicate keys, table conflicts,
//! AOT ordering, and (in strict mode) control characters and key syntax.

use crate::edit;
use crate::{FlatDoc, SpanKind};

#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(feature = "std")]
use std::collections::BTreeMap;

/// Validation strictness level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationMode {
    /// Accept everything, no validation (current behavior)
    Lenient,
    /// Check structural TOML rules, accept INI extensions (`;` comments, loose quoting)
    Relaxed,
    /// Full TOML 1.1.0 spec compliance
    Strict,
}

/// The kind of validation problem found.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationErrorKind {
    DuplicateKey,
    TableConflict,
    AotOrdering,
    ControlCharacter,
    InvalidKey,
    MissingValue,
}

/// A single validation error with position and description.
#[derive(Debug)]
pub struct ValidationError {
    pub pos: usize,
    pub kind: ValidationErrorKind,
    pub msg: String,
}

/// Run the full validation suite against a document.
pub(crate) fn validate(doc: &FlatDoc, mode: ValidationMode) -> Vec<ValidationError> {
    let mut errors = Vec::new();
    let index = edit::build_index(doc);

    // Always check: duplicate keys (and duplicate tables)
    check_duplicate_keys(doc, &index, &mut errors);

    if mode == ValidationMode::Relaxed || mode == ValidationMode::Strict {
        // Check: table conflicts (defining table after using as scalar)
        check_table_conflicts(doc, &index, &mut errors);
        // Check: AOT ordering
        check_aot_ordering(doc, &mut errors);
    }

    if mode == ValidationMode::Strict {
        // Check: control characters in keys/values
        check_control_characters(doc, &mut errors);
        // Check: invalid key syntax
        check_key_syntax(doc, &mut errors);
        // Check: missing values
        check_missing_values(doc, &mut errors);
    }

    errors
}

// =========================================================================
// Span-walk helper: ordered walk collecting structural info
// =========================================================================

struct SpanWalk {
    table_headers: Vec<Vec<String>>,
    entries: Vec<(Vec<String>, usize)>,
    /// Implicit tables: dotted-key prefixes NOT covered by an explicit header.
    implicit_tables: Vec<(Vec<String>, usize)>,
}

/// Walk spans in order, tracking current table scope.
fn walk_spans(doc: &FlatDoc) -> SpanWalk {
    let mut table_headers: Vec<Vec<String>> = Vec::new();
    let mut entries: Vec<(Vec<String>, usize)> = Vec::new();
    let mut implicit_tables: Vec<(Vec<String>, usize)> = Vec::new();
    let mut current_table: Vec<String> = Vec::new();
    let mut i = 0;

    while i < doc.spans.len() {
        let span = doc.spans[i];

        match span.kind {
            SpanKind::ArrayOpen | SpanKind::ArrayTableOpen => {
                let mut path: Vec<String> = Vec::with_capacity(4);
                i += 1;
                while i < doc.spans.len() {
                    match doc.spans[i].kind {
                        SpanKind::BareKey | SpanKind::BasicString | SpanKind::LiteralString => {
                            let key = edit::clean_key(&doc.source, &doc.spans[i]);
                            path.push(key.to_string());
                            i += 1;
                        }
                        SpanKind::Dot => {
                            i += 1;
                        }
                        SpanKind::ArrayClose | SpanKind::ArrayTableClose => {
                            i += 1;
                            break;
                        }
                        _ => {
                            i += 1;
                            break;
                        }
                    }
                }
                if !path.is_empty() {
                    table_headers.push(path.clone());
                    current_table = path;
                }
                continue;
            }

            SpanKind::BareKey | SpanKind::BasicString | SpanKind::LiteralString => {
                let mut key_parts: Vec<String> = Vec::with_capacity(4);
                key_parts.push(edit::clean_key(&doc.source, &span).to_string());
                let key_start = i;
                let mut j = i + 1;

                loop {
                    if j >= doc.spans.len() {
                        break;
                    }
                    match doc.spans[j].kind {
                        SpanKind::Whitespace | SpanKind::Newline | SpanKind::Comment => {
                            j += 1;
                        }
                        SpanKind::Dot => {
                            j += 1;
                        }
                        SpanKind::BareKey | SpanKind::BasicString | SpanKind::LiteralString => {
                            key_parts.push(edit::clean_key(&doc.source, &doc.spans[j]).to_string());
                            j += 1;
                        }
                        SpanKind::Equals => {
                            let mut full_path: Vec<String> =
                                Vec::with_capacity(current_table.len() + key_parts.len());
                            full_path.extend_from_slice(&current_table);
                            full_path.extend(key_parts.clone());

                            entries.push((full_path.clone(), key_start));

                            // Track implicit tables from dotted keys.
                            // A prefix is implicit if there are multiple key parts
                            // AND the prefix is not an explicit table header.
                            if key_parts.len() > 1 {
                                let prefix_base_len = current_table.len();
                                for prefix_extra in 1..key_parts.len() {
                                    let prefix_len = prefix_base_len + prefix_extra;
                                    let prefix: Vec<String> = full_path[..prefix_len].to_vec();
                                    if !table_headers.contains(&prefix)
                                        && !implicit_tables.iter().any(|(p, _)| p == &prefix)
                                    {
                                        implicit_tables.push((prefix, key_start));
                                    }
                                }
                            }

                            // Skip past the value
                            j += 1;
                            let mut k = j;
                            while k < doc.spans.len() {
                                if is_value_kind(doc.spans[k].kind) {
                                    i = k;
                                    break;
                                }
                                match doc.spans[k].kind {
                                    SpanKind::Whitespace
                                    | SpanKind::Newline
                                    | SpanKind::Comment => {
                                        k += 1;
                                    }
                                    _ => {
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        _ => {
                            break;
                        }
                    }
                }
                i += 1;
            }
            _ => {
                i += 1;
            }
        }
    }

    SpanWalk {
        table_headers,
        entries,
        implicit_tables,
    }
}

fn is_value_kind(k: SpanKind) -> bool {
    matches!(
        k,
        SpanKind::Integer
            | SpanKind::Float
            | SpanKind::Boolean
            | SpanKind::Datetime
            | SpanKind::BasicString
            | SpanKind::LiteralString
            | SpanKind::MlBasicString
            | SpanKind::MlLiteralString
            | SpanKind::InlineTableOpen
            | SpanKind::ArrayOpen
    )
}

// ---------------------------------------------------------------------------
// check_duplicate_keys
// ---------------------------------------------------------------------------

fn check_duplicate_keys(
    doc: &FlatDoc,
    index: &[(Vec<String>, edit::Entry)],
    errors: &mut Vec<ValidationError>,
) {
    let walk = walk_spans(doc);

    // 1. Duplicate key-value entries (from index)
    let mut seen: BTreeMap<&[String], usize> = BTreeMap::new();
    for (i, (path, entry)) in index.iter().enumerate() {
        if let Some(prev_idx) = seen.get(path.as_slice()) {
            let prev_entry = &index[*prev_idx].1;
            errors.push(ValidationError {
                pos: entry.key_start,
                kind: ValidationErrorKind::DuplicateKey,
                msg: format!(
                    "duplicate key `{}` (first defined at span index {})",
                    path.join("."),
                    prev_entry.key_start,
                ),
            });
        } else {
            seen.insert(path.as_slice(), i);
        }
    }

    // 2. Duplicate explicit table/AOT headers
    let mut seen_tables: BTreeMap<&[String], usize> = BTreeMap::new();
    for (seq_idx, path) in walk.table_headers.iter().enumerate() {
        if let Some(prev_seq) = seen_tables.get(path.as_slice()) {
            errors.push(ValidationError {
                pos: 0,
                kind: ValidationErrorKind::DuplicateKey,
                msg: format!(
                    "duplicate table `[{}]` (first defined as occurrence #{})",
                    path.join("."),
                    prev_seq + 1,
                ),
            });
        } else {
            seen_tables.insert(path.as_slice(), seq_idx);
        }
    }
}

// ---------------------------------------------------------------------------
// check_table_conflicts
// ---------------------------------------------------------------------------

/// Detect table conflicts:
/// 1. Scalar-then-subtable: `a = 1` then `[a.b]`
/// 2. Implicit-then-explicit: `a.b = 1` then `[a]`
/// 3. Same path as both scalar and table
fn check_table_conflicts(
    doc: &FlatDoc,
    index: &[(Vec<String>, edit::Entry)],
    errors: &mut Vec<ValidationError>,
) {
    let walk = walk_spans(doc);

    // Conflict: scalar path vs table header path
    for (scalar_path, entry) in index {
        for table_path in &walk.table_headers {
            if scalar_path == table_path {
                errors.push(ValidationError {
                    pos: entry.key_start,
                    kind: ValidationErrorKind::TableConflict,
                    msg: format!(
                        "key `{}` is defined as both a value and a table",
                        scalar_path.join("."),
                    ),
                });
            } else if table_path.len() > scalar_path.len()
                && table_path.starts_with(scalar_path.as_slice())
            {
                errors.push(ValidationError {
                    pos: entry.key_start,
                    kind: ValidationErrorKind::TableConflict,
                    msg: format!(
                        "key `{}` used as a value conflicts with table `{}`",
                        scalar_path.join("."),
                        table_path.join("."),
                    ),
                });
            }
        }
    }

    // Conflict: implicit table from dotted key vs explicit table header.
    // e.g. `a.b = 1` then `[a]` — dotted key creates implicit table "a",
    // then explicit table header redefines it.
    for (imp_path, key_start) in &walk.implicit_tables {
        if walk.table_headers.iter().any(|th| th == imp_path) {
            // Find the dotted-key entry that created this implicit table
            if let Some((entry_path, _)) = walk
                .entries
                .iter()
                .find(|(ep, _)| ep.starts_with(imp_path.as_slice()) && ep.len() > imp_path.len())
            {
                errors.push(ValidationError {
                    pos: *key_start,
                    kind: ValidationErrorKind::TableConflict,
                    msg: format!(
                        "table `[{}]` conflicts with implicit table from dotted key `{}`",
                        imp_path.join("."),
                        entry_path.join("."),
                    ),
                });
            }
        }
    }
}

// ---------------------------------------------------------------------------
// check_aot_ordering
// ---------------------------------------------------------------------------

fn check_aot_ordering(doc: &FlatDoc, errors: &mut Vec<ValidationError>) {
    let mut aots: Vec<(usize, Vec<String>)> = Vec::new();
    let mut i = 0;
    while i < doc.spans.len() {
        if doc.spans[i].kind == SpanKind::ArrayTableOpen {
            let mut path: Vec<String> = Vec::with_capacity(4);
            i += 1;
            while i < doc.spans.len() {
                match doc.spans[i].kind {
                    SpanKind::BareKey | SpanKind::BasicString | SpanKind::LiteralString => {
                        let key = edit::clean_key(&doc.source, &doc.spans[i]);
                        path.push(key.to_string());
                        i += 1;
                    }
                    SpanKind::Dot => {
                        i += 1;
                    }
                    SpanKind::ArrayTableClose => {
                        i += 1;
                        break;
                    }
                    _ => {
                        i += 1;
                        break;
                    }
                }
            }
            aots.push((aots.len(), path));
            continue;
        }
        i += 1;
    }

    let mut groups: BTreeMap<&[String], Vec<usize>> = BTreeMap::new();
    for (seq_idx, path) in &aots {
        groups.entry(path.as_slice()).or_default().push(*seq_idx);
    }
    for (path, indices) in &groups {
        if indices.len() <= 1 {
            continue;
        }
        for w in indices.windows(2) {
            if w[0] + 1 != w[1] {
                errors.push(ValidationError {
                    pos: 0,
                    kind: ValidationErrorKind::AotOrdering,
                    msg: format!(
                        "array-of-tables `[[{}]]` entries are not consecutive",
                        path.join("."),
                    ),
                });
                break;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// check_control_characters
// ---------------------------------------------------------------------------

fn check_control_characters(doc: &FlatDoc, errors: &mut Vec<ValidationError>) {
    let source = doc.source.as_bytes();
    for (i, &b) in source.iter().enumerate() {
        if b < 0x20 && b != b'\t' && b != b'\n' && b != b'\r' {
            errors.push(ValidationError {
                pos: i,
                kind: ValidationErrorKind::ControlCharacter,
                msg: format!("control character U+{:04X} is not valid in TOML", b),
            });
        } else if b == 0x7F {
            errors.push(ValidationError {
                pos: i,
                kind: ValidationErrorKind::ControlCharacter,
                msg: "control character U+007F is not valid in TOML".to_string(),
            });
        }
    }
}

// ---------------------------------------------------------------------------
// check_key_syntax
// ---------------------------------------------------------------------------

fn check_key_syntax(doc: &FlatDoc, errors: &mut Vec<ValidationError>) {
    for span in &doc.spans {
        if span.kind != SpanKind::BareKey {
            continue;
        }
        let text = &doc.source[span.start as usize..span.end as usize];
        if text.is_empty() {
            errors.push(ValidationError {
                pos: span.start as usize,
                kind: ValidationErrorKind::InvalidKey,
                msg: "bare key must not be empty".to_string(),
            });
        } else if !text
            .bytes()
            .all(|b| matches!(b, b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_'))
        {
            errors.push(ValidationError {
                pos: span.start as usize,
                kind: ValidationErrorKind::InvalidKey,
                msg: format!("bare key `{}` contains invalid characters", text),
            });
        }
    }
}

// ---------------------------------------------------------------------------
// check_missing_values
// ---------------------------------------------------------------------------

fn check_missing_values(doc: &FlatDoc, errors: &mut Vec<ValidationError>) {
    let mut i = 0;
    while i < doc.spans.len() {
        if doc.spans[i].kind == SpanKind::Equals {
            let mut j = i + 1;
            while j < doc.spans.len() {
                match doc.spans[j].kind {
                    SpanKind::Whitespace | SpanKind::Newline | SpanKind::Comment => {
                        j += 1;
                    }
                    _ => break,
                }
            }
            if j >= doc.spans.len() {
                errors.push(ValidationError {
                    pos: doc.spans[i].start as usize,
                    kind: ValidationErrorKind::MissingValue,
                    msg: "key has no value".to_string(),
                });
            } else {
                let next_kind = doc.spans[j].kind;
                let is_value = matches!(
                    next_kind,
                    SpanKind::Integer
                        | SpanKind::Float
                        | SpanKind::Boolean
                        | SpanKind::Datetime
                        | SpanKind::BasicString
                        | SpanKind::LiteralString
                        | SpanKind::MlBasicString
                        | SpanKind::MlLiteralString
                        | SpanKind::InlineTableOpen
                        | SpanKind::ArrayOpen
                );
                if !is_value {
                    errors.push(ValidationError {
                        pos: doc.spans[i].start as usize,
                        kind: ValidationErrorKind::MissingValue,
                        msg: "key has no value".to_string(),
                    });
                }
            }
        }
        i += 1;
    }
}