yaml-edit 0.2.1

A lossless parser and editor for YAML files
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
//! Additional edge cases from section 3 of better.md
//!
//! Tests cover:
//! - Block scalar edge cases (mixed indentation, line endings, empty blocks)
//! - Comment edge cases (inside quotes, Unicode, at EOF)
//! - Flow collection edge cases (deeply nested, trailing commas)

use std::str::FromStr;
use yaml_edit::YamlFile;

// ========================================
// Block Scalar Edge Cases (Section 3.1)
// ========================================

/// Test Windows line endings (CRLF) in block scalars
#[test]
fn test_block_scalar_windows_line_endings() {
    let yaml = "text: |\r\n  Line 1\r\n  Line 2\r\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse Windows line endings"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let text_val = mapping.get("text").expect("Should have text key");
    let text_str = text_val.as_scalar().unwrap().as_string();

    // Line endings should be normalized to \n
    assert_eq!(text_str, "Line 1\nLine 2\n");

    // Verify round-trip produces valid YAML
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test empty block scalar with literal indicator
#[test]
fn test_empty_block_scalar_literal() {
    let yaml = "literal: |\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse empty block scalar");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let literal_val = mapping.get("literal").expect("Should have literal key");
    let literal_str = literal_val.as_scalar().unwrap().as_string();

    // Empty block scalar should produce empty string or single newline
    assert!(
        literal_str.is_empty() || literal_str == "\n",
        "Empty block scalar should be empty or newline, got: {:?}",
        literal_str
    );

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test empty block scalar with folded indicator
#[test]
fn test_empty_block_scalar_folded() {
    let yaml = "folded: >\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse empty folded scalar");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let folded_val = mapping.get("folded").expect("Should have folded key");
    let folded_str = folded_val.as_scalar().unwrap().as_string();

    // Empty folded scalar behavior
    assert!(
        folded_str.is_empty() || folded_str == "\n",
        "Empty folded scalar should be empty or newline, got: {:?}",
        folded_str
    );

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test empty block scalars with strip chomping indicator
#[test]
fn test_empty_block_scalar_with_strip() {
    let yaml = "strip: |-\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse empty block scalar with strip"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    // Should have the strip key
    assert_eq!(mapping.keys().count(), 1, "Should have 1 key");
    mapping.get("strip").expect("Should have strip key");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test empty block scalars with keep chomping indicator
#[test]
fn test_empty_block_scalar_with_keep() {
    let yaml = "keep: |+\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse empty block scalar with keep"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    // Should have the keep key
    assert_eq!(mapping.keys().count(), 1, "Should have 1 key");
    mapping.get("keep").expect("Should have keep key");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test block scalar immediately after flow collection
#[test]
fn test_block_scalar_after_flow_collection() {
    let yaml = r#"flow: [a, b, c]
block: |
  Content here
"#;

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse block after flow");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    // Verify flow collection
    let flow_val = mapping.get("flow").expect("Should have flow key");
    assert!(flow_val.is_sequence(), "flow should be sequence");

    // Verify block scalar
    let block_val = mapping.get("block").expect("Should have block key");
    assert!(block_val.is_scalar(), "block should be scalar");
    assert_eq!(block_val.as_scalar().unwrap().as_string(), "Content here\n");

    // Verify round-trip
    let output = doc.to_string();
    assert_eq!(output, yaml);
}

// ========================================
// Comment Edge Cases (Section 3.3)
// ========================================

/// Test comment inside quoted string (should be literal, not a comment)
#[test]
fn test_comment_inside_quoted_string() {
    let yaml = r#"text: "This is # not a comment""#;

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse quoted string with #"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let text_val = mapping.get("text").expect("Should have text key");
    assert_eq!(
        text_val.as_scalar().unwrap().as_string(),
        "This is # not a comment"
    );

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test comment with only whitespace
#[test]
fn test_comment_with_only_whitespace() {
    let yaml = "key: value  #   \n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse comment with whitespace"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let val = mapping.get("key").expect("Should have key");
    assert_eq!(val.as_scalar().unwrap().as_string(), "value");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test comment at EOF without trailing newline
#[test]
fn test_comment_at_eof_no_newline() {
    let yaml = "key: value\n# Final comment";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse comment at EOF");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    assert_eq!(mapping.keys().count(), 1);
    assert!(mapping.contains_key("key"));

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test comment with Unicode and emoji
#[test]
fn test_comment_with_unicode_emoji() {
    let yaml = "key: value  # 🎉 important! 日本語\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse Unicode comments");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let val = mapping.get("key").expect("Should have key");
    assert_eq!(val.as_scalar().unwrap().as_string(), "value");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test multiple # characters in comment
#[test]
fn test_multiple_hash_in_comment() {
    let yaml = "key: value  ## comment ### more\n";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(
        parsed.errors().len(),
        0,
        "Should parse multiple # in comment"
    );

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let val = mapping.get("key").expect("Should have key");
    assert_eq!(val.as_scalar().unwrap().as_string(), "value");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0);
}

/// Test comment immediately after directive
#[test]
fn test_comment_after_directive() {
    let yaml = "%YAML 1.2 # this is a comment\n---\nkey: value\n";

    let parsed = YamlFile::parse(yaml);
    // Parser may or may not support this - just verify no crash
    let tree = parsed.tree();
    assert!(tree.document().is_some());
}

// ========================================
// Flow Collection Edge Cases (Section 3.4)
// ========================================

/// Test deeply nested flow collections (5 levels)
#[test]
fn test_deeply_nested_flow_collections() {
    let yaml = "nested: [[[[[a]]]]]";

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse deeply nested flow");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    let nested = mapping.get("nested").expect("Should have nested key");
    assert!(nested.is_sequence(), "Should be sequence");

    // Navigate down the nesting - verify structure exists
    let level1 = nested.as_sequence().unwrap();
    assert_eq!(level1.len(), 1, "Level 1 should have 1 element");

    let level2_elem = level1.get(0).expect("Should have level 2");
    assert!(level2_elem.is_sequence(), "Level 2 should be sequence");
    let level2 = level2_elem.as_sequence().unwrap();
    assert_eq!(level2.len(), 1, "Level 2 should have 1 element");

    let level3_elem = level2.get(0).expect("Should have level 3");
    assert!(level3_elem.is_sequence(), "Level 3 should be sequence");
    let level3 = level3_elem.as_sequence().unwrap();
    assert_eq!(level3.len(), 1, "Level 3 should have 1 element");

    let level4_elem = level3.get(0).expect("Should have level 4");
    assert!(level4_elem.is_sequence(), "Level 4 should be sequence");
    let level4 = level4_elem.as_sequence().unwrap();
    assert_eq!(level4.len(), 1, "Level 4 should have 1 element");

    let level5_elem = level4.get(0).expect("Should have level 5");
    assert!(level5_elem.is_sequence(), "Level 5 should be sequence");
    let level5 = level5_elem.as_sequence().unwrap();
    assert_eq!(level5.len(), 1, "Level 5 should have 1 element");

    // Final element should be scalar 'a'
    let final_elem = level5.get(0).unwrap();
    assert!(final_elem.is_scalar());
    assert_eq!(final_elem.as_scalar().unwrap().as_string(), "a");

    // Verify round-trip
    let output = doc.to_string();
    let reparsed = YamlFile::parse(&output);
    assert_eq!(reparsed.errors().len(), 0, "Round-trip should be valid");
}

/// Test trailing comma with whitespace in array
#[test]
fn test_trailing_comma_with_whitespace_array() {
    let yaml = "array: [a, b, ]";

    let parsed = YamlFile::parse(yaml);
    // Trailing comma may be accepted or rejected depending on strictness
    let tree = parsed.tree();
    assert!(tree.document().is_some());

    if parsed.errors().is_empty() {
        // If accepted, verify structure
        let doc = tree.document().unwrap();
        let mapping = doc.as_mapping().expect("Should be mapping");
        let arr = mapping.get_sequence("array").expect("Should have array");
        // Should have 2 elements (a and b), not 3
        assert!(arr.len() >= 2, "Should have at least 2 elements");
    }
}

/// Test trailing comma with whitespace in mapping
#[test]
fn test_trailing_comma_with_whitespace_mapping() {
    let yaml = "config: {a: 1, b: 2, }";

    let parsed = YamlFile::parse(yaml);
    // Trailing comma may be accepted or rejected
    let tree = parsed.tree();
    assert!(tree.document().is_some());

    if parsed.errors().is_empty() {
        // If accepted, verify structure
        let doc = tree.document().unwrap();
        let mapping = doc.as_mapping().expect("Should be mapping");
        let config = mapping.get_mapping("config").expect("Should have config");
        // Should have 2 keys (a and b)
        assert!(config.keys().count() >= 2, "Should have at least 2 keys");
    }
}

/// Test mixed flow and block styles in same document
#[test]
fn test_mixed_flow_and_block_styles() {
    let yaml = r#"flow: [a, b, c]
block:
  - item1
  - item2
nested: {key: value}
"#;

    let parsed = YamlFile::parse(yaml);
    assert_eq!(parsed.errors().len(), 0, "Should parse mixed styles");

    let tree = parsed.tree();
    let doc = tree.document().expect("Should have document");
    let mapping = doc.as_mapping().expect("Should be mapping");

    assert_eq!(mapping.keys().count(), 3);
    assert!(mapping.contains_key("flow"));
    assert!(mapping.contains_key("block"));
    assert!(mapping.contains_key("nested"));

    // Verify flow sequence
    let flow = mapping
        .get_sequence("flow")
        .expect("flow should be sequence");
    assert_eq!(flow.len(), 3);

    // Verify block sequence
    let block = mapping
        .get_sequence("block")
        .expect("block should be sequence");
    assert_eq!(block.len(), 2);

    // Verify flow mapping
    let nested = mapping
        .get_mapping("nested")
        .expect("nested should be mapping");
    assert_eq!(nested.keys().count(), 1);

    // Verify round-trip
    let output = doc.to_string();
    assert_eq!(output, yaml);
}

// ========================================
// Document Stream Edge Cases (Section 3.6)
// ========================================

/// Test multiple documents with different schemas (flow vs block)
#[test]
fn test_multiple_documents_different_schemas() {
    let yaml = "---\njson: {a: 1, b: 2}\n---\nyaml:\n  nested: value\n";

    let parsed = YamlFile::from_str(yaml).unwrap();

    // Verify we have exactly 2 documents
    let docs: Vec<_> = parsed.documents().collect();
    assert_eq!(docs.len(), 2, "Should have 2 documents");

    // First document has flow mapping
    let doc1 = &docs[0];
    let map1 = doc1.as_mapping().unwrap();
    assert_eq!(map1.keys().count(), 1);
    let json_val = map1.get("json").unwrap();
    let json_map = json_val.as_mapping().unwrap();
    assert_eq!(json_map.keys().count(), 2);
    assert_eq!(
        json_map.get("a").unwrap().as_scalar().unwrap().as_string(),
        "1"
    );
    assert_eq!(
        json_map.get("b").unwrap().as_scalar().unwrap().as_string(),
        "2"
    );

    // Second document has block mapping
    let doc2 = &docs[1];
    let map2 = doc2.as_mapping().unwrap();
    assert_eq!(map2.keys().count(), 1);
    let yaml_val = map2.get("yaml").unwrap();
    let yaml_map = yaml_val.as_mapping().unwrap();
    assert_eq!(
        yaml_map
            .get("nested")
            .unwrap()
            .as_scalar()
            .unwrap()
            .as_string(),
        "value"
    );

    // Verify exact round-trip
    let output = parsed.to_string();
    assert_eq!(output, yaml);
}

/// Test document with YAML directive
#[test]
fn test_document_with_yaml_directive() {
    let yaml = "%YAML 1.2\n---\nkey: value\n";

    let parsed = YamlFile::from_str(yaml).unwrap();

    // Should have 1 document
    let docs: Vec<_> = parsed.documents().collect();
    assert_eq!(docs.len(), 1, "Should have 1 document");

    // Verify content
    let doc = &docs[0];
    let mapping = doc.as_mapping().unwrap();
    assert_eq!(mapping.keys().count(), 1);
    assert_eq!(
        mapping.get("key").unwrap().as_scalar().unwrap().as_string(),
        "value"
    );

    // Verify exact round-trip
    let output = parsed.to_string();
    assert_eq!(output, yaml);
}

/// Test empty document markers
#[test]
fn test_empty_document_markers() {
    let yaml = "---\n---\n---\nkey: value\n";

    let parsed = YamlFile::from_str(yaml).unwrap();

    // Parser should handle empty documents
    let docs: Vec<_> = parsed.documents().collect();

    // Find the non-empty document (last one)
    let last_doc = docs.last().unwrap();
    let mapping = last_doc.as_mapping().unwrap();
    assert_eq!(mapping.keys().count(), 1);
    assert_eq!(
        mapping.get("key").unwrap().as_scalar().unwrap().as_string(),
        "value"
    );

    // Verify exact round-trip
    let output = parsed.to_string();
    assert_eq!(output, yaml);
}

/// Test moderately large document stream (100 documents)
#[test]
fn test_large_document_stream() {
    // Build a stream of 100 small documents
    let mut yaml = String::new();
    for i in 0..100 {
        yaml.push_str("---\n");
        yaml.push_str(&format!("doc{}: value{}\n", i, i));
    }

    let parsed = YamlFile::from_str(&yaml).unwrap();

    // Should have exactly 100 documents
    let docs: Vec<_> = parsed.documents().collect();
    assert_eq!(docs.len(), 100, "Should have 100 documents");

    // Verify first document
    let doc0 = &docs[0];
    let map0 = doc0.as_mapping().unwrap();
    assert_eq!(map0.keys().count(), 1);
    assert_eq!(
        map0.get("doc0").unwrap().as_scalar().unwrap().as_string(),
        "value0"
    );

    // Verify middle document
    let doc50 = &docs[50];
    let map50 = doc50.as_mapping().unwrap();
    assert_eq!(map50.keys().count(), 1);
    assert_eq!(
        map50.get("doc50").unwrap().as_scalar().unwrap().as_string(),
        "value50"
    );

    // Verify last document
    let doc99 = &docs[99];
    let map99 = doc99.as_mapping().unwrap();
    assert_eq!(map99.keys().count(), 1);
    assert_eq!(
        map99.get("doc99").unwrap().as_scalar().unwrap().as_string(),
        "value99"
    );

    // Verify exact round-trip
    let output = parsed.to_string();
    assert_eq!(output, yaml);
}