nested-text 0.1.0

A fully spec-compliant NestedText v3.8 parser and serializer
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use crate::error::{Error, ErrorKind};
use crate::inline::InlineParser;
use crate::lexer::{Lexer, LineKind};
use crate::value::Value;

/// Constraint on the top-level type of a NestedText document.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Top {
    Dict,
    List,
    String,
    Any,
}

/// Parse a NestedText string into a Value.
///
/// Returns `None` for empty documents (only comments/blank lines).
pub fn loads(input: &str, top: Top) -> Result<Option<Value>, Error> {
    let mut lexer = Lexer::new(input)?;
    let mut parser = Parser {
        lexer: &mut lexer,
        indent_stack: vec![],
        all_indent_levels: vec![0],
    };
    let value = parser.read_value(0)?;

    // Check for extra content after top-level value
    if value.is_some() {
        if let Some(line) = parser.lexer.peek() {
            // Check if this is a partial dedent (depth > 0, established indentation exists,
            // but this depth was never used as an indentation level)
            if line.depth > 0
                && parser.all_indent_levels.len() > 1
                && !parser.all_indent_levels.contains(&line.depth)
            {
                return Err(Error::new(
                    ErrorKind::InvalidIndentLevel,
                    "invalid indentation, partial dedent.",
                )
                .with_lineno(line.lineno)
                .with_colno(0)
                .with_line(line.text.clone()));
            }
            return Err(Error::new(ErrorKind::UnexpectedLineType, "extra content.")
                .with_lineno(line.lineno)
                .with_colno(0)
                .with_line(line.text.clone()));
        }
    }

    // Handle empty documents based on Top constraint
    let value = match (value, top) {
        (None, Top::Any) => None,
        (None, Top::Dict) => Some(Value::Dict(vec![])),
        (None, Top::List) => Some(Value::List(vec![])),
        (None, Top::String) => Some(Value::String(String::new())),
        (Some(v), Top::Any) => Some(v),
        (Some(v @ Value::Dict(_)), Top::Dict) => Some(v),
        (Some(v @ Value::List(_)), Top::List) => Some(v),
        (Some(v @ Value::String(_)), Top::String) => Some(v),
        (Some(_), Top::Dict) => {
            return Err(Error::new(
                ErrorKind::UnexpectedLineType,
                "expected dictionary top-level",
            ));
        }
        (Some(_), Top::List) => {
            return Err(Error::new(
                ErrorKind::UnexpectedLineType,
                "expected list top-level",
            ));
        }
        (Some(_), Top::String) => {
            return Err(Error::new(
                ErrorKind::UnexpectedLineType,
                "expected string top-level",
            ));
        }
    };

    Ok(value)
}

/// Parse a NestedText document from a reader.
pub fn load<R: std::io::Read>(reader: R, top: Top) -> Result<Option<Value>, Error> {
    let mut buf = String::new();
    let mut reader = reader;
    reader.read_to_string(&mut buf)?;
    loads(&buf, top)
}

struct Parser<'a> {
    lexer: &'a mut Lexer,
    /// Stack of indentation levels we've entered, for detecting partial dedents.
    indent_stack: Vec<usize>,
    /// All indentation levels ever established during parsing.
    all_indent_levels: Vec<usize>,
}

impl<'a> Parser<'a> {
    /// Read a value at the given indentation depth.
    fn read_value(&mut self, depth: usize) -> Result<Option<Value>, Error> {
        let line = match self.lexer.peek() {
            Some(l) => l,
            None => return Ok(None),
        };

        if line.depth < depth {
            return Ok(None);
        }
        if line.depth > depth {
            if depth == 0 && self.indent_stack.is_empty() {
                // Top-level content with indentation
                return Err(Error::new(
                    ErrorKind::InvalidIndentLevel,
                    "top-level content must start in column 1.",
                )
                .with_lineno(line.lineno)
                .with_colno(0)
                .with_line(line.text.clone()));
            }
            // Check for partial dedent — returning to an indentation level
            // that was never established
            if !self.indent_stack.is_empty() && !self.indent_stack.contains(&line.depth) {
                return Err(Error::new(
                    ErrorKind::InvalidIndentLevel,
                    "invalid indentation, partial dedent.",
                )
                .with_lineno(line.lineno)
                .with_colno(0)
                .with_line(line.text.clone()));
            }
            return Err(Error::new(
                ErrorKind::InvalidIndentLevel,
                "invalid indentation.",
            )
            .with_lineno(line.lineno)
            .with_colno(line.depth)
            .with_line(line.text.clone()));
        }

        match line.kind {
            LineKind::DictItem | LineKind::KeyItem => self.read_dict(depth).map(Some),
            LineKind::ListItem => self.read_list(depth).map(Some),
            LineKind::StringItem => self.read_string(depth).map(Some),
            LineKind::InlineList | LineKind::InlineDict => {
                let line = self.lexer.next_line().unwrap();
                let input = line.value.as_ref().unwrap();
                let lineno = line.lineno;
                let colno_offset = line.depth;
                let line_text = &line.text;
                InlineParser::parse(input, lineno, colno_offset, line_text).map(Some)
            }
            LineKind::Unrecognized => {
                let line = self.lexer.peek().unwrap();
                Err(Error::new(ErrorKind::UnrecognizedLine, "unrecognized line.")
                    .with_lineno(line.lineno)
                    .with_colno(line.depth)
                    .with_line(line.text.clone()))
            }
        }
    }

    /// Read a dictionary at the given depth.
    fn read_dict(&mut self, depth: usize) -> Result<Value, Error> {
        let mut pairs: Vec<(String, Value)> = Vec::new();
        let mut seen_keys: Vec<String> = Vec::new();

        while let Some(line) = self.lexer.peek() {
            if line.depth != depth {
                break;
            }

            match line.kind {
                LineKind::DictItem => {
                    let line = self.lexer.next_line().unwrap();
                    let key = line.key.clone().unwrap();
                    let raw_value = line.value.clone().unwrap();
                    let lineno = line.lineno;
                    let line_text = line.text.clone();

                    // Check for duplicate keys
                    if seen_keys.contains(&key) {
                        return Err(Error::new(
                            ErrorKind::DuplicateKey,
                            format!("duplicate key: {}.", key),
                        )
                        .with_lineno(lineno)
                        .with_colno(0)
                        .with_line(line_text));
                    }
                    seen_keys.push(key.clone());

                    let value = if !raw_value.is_empty() {
                        // Value on the same line — no indented content allowed
                        self.check_no_indented_content(depth, lineno)?;
                        Value::String(raw_value)
                    } else {
                        // Value on indented lines below
                        self.read_indented_value(depth)?
                    };

                    pairs.push((key, value));
                }
                LineKind::KeyItem => {
                    // Save first key item line info for error reporting
                    let first_key_lineno = self.lexer.peek().unwrap().lineno;
                    let first_key_text = self.lexer.peek().unwrap().text.clone();
                    let key = self.read_key(depth)?;

                    // Check for duplicate keys
                    if seen_keys.contains(&key) {
                        return Err(Error::new(
                            ErrorKind::DuplicateKey,
                            format!("duplicate key: {}.", key),
                        ));
                    }
                    seen_keys.push(key.clone());

                    // After a multiline key, an indented value MUST follow
                    let next = self.lexer.peek();
                    match next {
                        Some(l) if l.depth > depth => {
                            let child_depth = l.depth;
                            self.indent_stack.push(child_depth);
                            self.all_indent_levels.push(child_depth);
                            let value = self
                                .read_value(child_depth)?
                                .unwrap_or(Value::String(String::new()));
                            self.indent_stack.pop();
                            pairs.push((key, value));
                        }
                        Some(_l) => {
                            return Err(Error::new(
                                ErrorKind::InvalidIndentLevel,
                                "multiline key requires a value.",
                            )
                            .with_lineno(first_key_lineno)
                            .with_colno(depth)
                            .with_line(first_key_text.clone()));
                        }
                        None => {
                            return Err(Error::new(
                                ErrorKind::InvalidIndentLevel,
                                "indented value must follow multiline key.",
                            )
                            .with_lineno(first_key_lineno)
                            .with_line(first_key_text));
                        }
                    }
                }
                _ => {
                    // Wrong line type at this depth in a dict context
                    let line = self.lexer.peek().unwrap();
                    return Err(Error::new(
                        ErrorKind::UnexpectedLineType,
                        "expected dictionary item.",
                    )
                    .with_lineno(line.lineno)
                    .with_colno(line.depth)
                    .with_line(line.text.clone()));
                }
            }
        }

        Ok(Value::Dict(pairs))
    }

    /// Read a list at the given depth.
    fn read_list(&mut self, depth: usize) -> Result<Value, Error> {
        let mut items = Vec::new();

        while let Some(line) = self.lexer.peek() {
            if line.depth != depth {
                break;
            }

            if line.kind == LineKind::ListItem {
                let line = self.lexer.next_line().unwrap();
                let raw_value = line.value.clone().unwrap();
                let lineno = line.lineno;

                let value = if !raw_value.is_empty() {
                    // Value on same line — no indented content allowed
                    self.check_no_indented_content(depth, lineno)?;
                    Value::String(raw_value)
                } else {
                    self.read_indented_value(depth)?
                };

                items.push(value);
            } else {
                // Wrong line type at this depth in a list context
                let line = self.lexer.peek().unwrap();
                return Err(Error::new(
                    ErrorKind::UnexpectedLineType,
                    "expected list item.",
                )
                .with_lineno(line.lineno)
                .with_colno(line.depth)
                .with_line(line.text.clone()));
            }
        }

        Ok(Value::List(items))
    }

    /// Read a multiline string at the given depth.
    fn read_string(&mut self, depth: usize) -> Result<Value, Error> {
        let mut parts = Vec::new();

        while self.lexer.next_is(depth, LineKind::StringItem) {
            let line = self.lexer.next_line().unwrap();
            parts.push(line.value.clone().unwrap());
        }

        // Check for invalid indentation after string
        // (e.g., a string item at deeper indentation mixed in)
        if let Some(next) = self.lexer.peek() {
            if next.depth > depth && next.kind == LineKind::StringItem {
                return Err(Error::new(
                    ErrorKind::InvalidIndentLevel,
                    "invalid indentation.",
                )
                .with_lineno(next.lineno)
                .with_colno(depth)
                .with_line(next.text.clone()));
            }
        }

        Ok(Value::String(parts.join("\n")))
    }

    /// Read a multiline key at the given depth.
    fn read_key(&mut self, depth: usize) -> Result<String, Error> {
        let mut parts = Vec::new();

        while self.lexer.next_is(depth, LineKind::KeyItem) {
            let line = self.lexer.next_line().unwrap();
            parts.push(line.value.clone().unwrap());
        }

        Ok(parts.join("\n"))
    }

    /// Read an indented value below a list/dict item that had an empty value on its line.
    fn read_indented_value(&mut self, parent_depth: usize) -> Result<Value, Error> {
        match self.lexer.peek() {
            Some(line) if line.depth > parent_depth => {
                let child_depth = line.depth;
                self.indent_stack.push(child_depth);
                            self.all_indent_levels.push(child_depth);
                let result = self
                    .read_value(child_depth)?
                    .ok_or_else(|| Error::new(ErrorKind::UnexpectedLineType, "expected value"));
                self.indent_stack.pop();
                result
            }
            _ => Ok(Value::String(String::new())),
        }
    }

    /// Check that no indented content follows a line that already has a value.
    /// If a dict/list item has text after the tag on the same line, then indented
    /// content below is an error (the value is already set).
    fn check_no_indented_content(
        &self,
        parent_depth: usize,
        _parent_lineno: usize,
    ) -> Result<(), Error> {
        if let Some(next) = self.lexer.peek() {
            if next.depth > parent_depth {
                return Err(Error::new(
                    ErrorKind::InvalidIndentLevel,
                    "invalid indentation.",
                )
                .with_lineno(next.lineno)
                .with_colno(parent_depth)
                .with_line(next.text.clone()));
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_document() {
        assert_eq!(loads("", Top::Any).unwrap(), None);
        assert_eq!(loads("# just a comment\n", Top::Any).unwrap(), None);
        assert_eq!(loads("  \n\n  \n", Top::Any).unwrap(), None);
    }

    #[test]
    fn test_simple_dict() {
        let v = loads("name: John\nage: 30", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![
                ("name".to_string(), Value::String("John".to_string())),
                ("age".to_string(), Value::String("30".to_string())),
            ])
        );
    }

    #[test]
    fn test_simple_list() {
        let v = loads("- apple\n- banana\n- cherry", Top::Any)
            .unwrap()
            .unwrap();
        assert_eq!(
            v,
            Value::List(vec![
                Value::String("apple".to_string()),
                Value::String("banana".to_string()),
                Value::String("cherry".to_string()),
            ])
        );
    }

    #[test]
    fn test_multiline_string() {
        let v = loads("> line one\n> line two\n> line three", Top::Any)
            .unwrap()
            .unwrap();
        assert_eq!(
            v,
            Value::String("line one\nline two\nline three".to_string())
        );
    }

    #[test]
    fn test_nested_dict_with_list() {
        let input = "fruits:\n  - apple\n  - banana\nveggies:\n  - carrot";
        let v = loads(input, Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![
                (
                    "fruits".to_string(),
                    Value::List(vec![
                        Value::String("apple".to_string()),
                        Value::String("banana".to_string()),
                    ])
                ),
                (
                    "veggies".to_string(),
                    Value::List(vec![Value::String("carrot".to_string())])
                ),
            ])
        );
    }

    #[test]
    fn test_nested_list_with_dict() {
        let input = "-\n  name: John\n  age: 30\n-\n  name: Jane\n  age: 25";
        let v = loads(input, Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::List(vec![
                Value::Dict(vec![
                    ("name".to_string(), Value::String("John".to_string())),
                    ("age".to_string(), Value::String("30".to_string())),
                ]),
                Value::Dict(vec![
                    ("name".to_string(), Value::String("Jane".to_string())),
                    ("age".to_string(), Value::String("25".to_string())),
                ]),
            ])
        );
    }

    #[test]
    fn test_empty_list_item() {
        let v = loads("- \n- hello", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::List(vec![
                Value::String("".to_string()),
                Value::String("hello".to_string()),
            ])
        );
    }

    #[test]
    fn test_empty_dict_value() {
        let v = loads("key:", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![(
                "key".to_string(),
                Value::String("".to_string()),
            )])
        );
    }

    #[test]
    fn test_inline_list_in_dict() {
        let v = loads("items: [a, b, c]", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![(
                "items".to_string(),
                Value::String("[a, b, c]".to_string()),
            )])
        );
    }

    #[test]
    fn test_inline_list_standalone() {
        let v = loads("[a, b, c]", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::List(vec![
                Value::String("a".to_string()),
                Value::String("b".to_string()),
                Value::String("c".to_string()),
            ])
        );
    }

    #[test]
    fn test_inline_dict_standalone() {
        let v = loads("{k: v}", Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![("k".to_string(), Value::String("v".to_string()))])
        );
    }

    #[test]
    fn test_top_constraint_dict() {
        let r = loads("- item", Top::Dict);
        assert!(r.is_err());
    }

    #[test]
    fn test_top_constraint_list() {
        let r = loads("key: value", Top::List);
        assert!(r.is_err());
    }

    #[test]
    fn test_multiline_key() {
        let input = ": key part 1\n: key part 2\n  > value";
        let v = loads(input, Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![(
                "key part 1\nkey part 2".to_string(),
                Value::String("value".to_string()),
            )])
        );
    }

    #[test]
    fn test_deeply_nested() {
        let input = "a:\n  b:\n    c: deep";
        let v = loads(input, Top::Any).unwrap().unwrap();
        assert_eq!(
            v,
            Value::Dict(vec![(
                "a".to_string(),
                Value::Dict(vec![(
                    "b".to_string(),
                    Value::Dict(vec![(
                        "c".to_string(),
                        Value::String("deep".to_string()),
                    )]),
                )]),
            )])
        );
    }

    #[test]
    fn test_duplicate_key_error() {
        let r = loads("key: value 1\nkey: value 2", Top::Any);
        assert!(r.is_err());
    }

    #[test]
    fn test_extra_content_after_inline() {
        let r = loads("[]\nfoo: bar", Top::Any);
        assert!(r.is_err());
    }

    #[test]
    fn test_value_on_line_then_indent_error() {
        // "key 1:  " has trailing space as value, so indented content is invalid
        let r = loads("key 1:  \n        key 2: value 2", Top::Any);
        assert!(r.is_err());
    }

    #[test]
    fn test_list_value_on_line_then_indent_error() {
        let r = loads("-  \n   > value", Top::Any);
        assert!(r.is_err());
    }
}