fyaml 0.5.0

Safe Rust bindings for libfyaml YAML parser with DOM navigation, path queries, and serde-compatible Value type
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
//! Editor edge cases and error path tests.
//!
//! Tests for editor operations at boundaries and error conditions.

use fyaml::Document;
use fyaml::NodeStyle;

// =============================================================================
// Root Operations
// =============================================================================

#[test]
fn editor_set_yaml_at_slash_root() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/", "key: value").unwrap();
    }
    assert!(doc.root().is_some());
    assert!(doc.root().unwrap().is_mapping());
}

#[test]
fn editor_set_yaml_at_empty_path() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("", "key: value").unwrap();
    }
    assert!(doc.root().is_some());
}

#[test]
fn editor_delete_at_root_fails() {
    let mut doc = Document::parse_str("key: value").unwrap();
    {
        let mut ed = doc.edit();
        let result = ed.delete_at("/");
        assert!(result.is_err());
    }
}

#[test]
fn editor_delete_at_empty_path_fails() {
    let mut doc = Document::parse_str("key: value").unwrap();
    {
        let mut ed = doc.edit();
        let result = ed.delete_at("");
        assert!(result.is_err());
    }
}

// =============================================================================
// Sequence Operations
// =============================================================================

#[test]
fn editor_delete_sequence_first_element() {
    let mut doc = Document::parse_str("items:\n  - a\n  - b\n  - c").unwrap();
    {
        let mut ed = doc.edit();
        let deleted = ed.delete_at("/items/0").unwrap();
        assert!(deleted);
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 2);
    assert_eq!(items.seq_get(0).unwrap().scalar_str().unwrap(), "b");
}

#[test]
fn editor_delete_sequence_middle_element() {
    let mut doc = Document::parse_str("items:\n  - a\n  - b\n  - c").unwrap();
    {
        let mut ed = doc.edit();
        let deleted = ed.delete_at("/items/1").unwrap();
        assert!(deleted);
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 2);
    assert_eq!(items.seq_get(0).unwrap().scalar_str().unwrap(), "a");
    assert_eq!(items.seq_get(1).unwrap().scalar_str().unwrap(), "c");
}

#[test]
fn editor_delete_sequence_last_element() {
    let mut doc = Document::parse_str("items:\n  - a\n  - b\n  - c").unwrap();
    {
        let mut ed = doc.edit();
        let deleted = ed.delete_at("/items/2").unwrap();
        assert!(deleted);
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 2);
    assert_eq!(items.seq_get(1).unwrap().scalar_str().unwrap(), "b");
}

#[test]
fn editor_delete_sequence_out_of_bounds() {
    let mut doc = Document::parse_str("items:\n  - a\n  - b").unwrap();
    {
        let mut ed = doc.edit();
        let deleted = ed.delete_at("/items/10").unwrap();
        assert!(!deleted);
    }
}

#[test]
fn editor_seq_append_at() {
    let mut doc = Document::parse_str("items:\n  - a\n  - b").unwrap();
    {
        let mut ed = doc.edit();
        let new_item = ed.build_scalar("c").unwrap();
        ed.seq_append_at("/items", new_item).unwrap();
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 3);
    assert_eq!(items.seq_get(2).unwrap().scalar_str().unwrap(), "c");
}

#[test]
fn editor_seq_append_at_empty_sequence() {
    let mut doc = Document::parse_str("items: []").unwrap();
    {
        let mut ed = doc.edit();
        let item = ed.build_scalar("first").unwrap();
        ed.seq_append_at("/items", item).unwrap();
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 1);
}

#[test]
fn editor_seq_append_at_non_sequence_fails() {
    let mut doc = Document::parse_str("mapping:\n  key: value").unwrap();
    {
        let mut ed = doc.edit();
        let item = ed.build_scalar("x").unwrap();
        let result = ed.seq_append_at("/mapping", item);
        assert!(result.is_err());
    }
}

#[test]
fn editor_seq_append_at_root_sequence() {
    let mut doc = Document::parse_str("[a, b]").unwrap();
    {
        let mut ed = doc.edit();
        let item = ed.build_scalar("c").unwrap();
        ed.seq_append_at("", item).unwrap();
    }
    let root = doc.root().unwrap();
    assert_eq!(root.seq_len().unwrap(), 3);
}

// =============================================================================
// Error Paths
// =============================================================================

#[test]
fn editor_set_yaml_at_non_mapping_parent_fails() {
    let mut doc = Document::parse_str("scalar_root").unwrap();
    {
        let mut ed = doc.edit();
        let result = ed.set_yaml_at("/child", "value");
        assert!(result.is_err());
    }
}

#[test]
fn editor_set_yaml_at_nonexistent_parent_fails() {
    let mut doc = Document::parse_str("existing: value").unwrap();
    {
        let mut ed = doc.edit();
        let result = ed.set_yaml_at("/nonexistent/child", "value");
        assert!(result.is_err());
    }
}

#[test]
fn editor_delete_at_nonexistent_parent() {
    let mut doc = Document::parse_str("key: value").unwrap();
    {
        let mut ed = doc.edit();
        let deleted = ed.delete_at("/nonexistent/child").unwrap();
        assert!(!deleted);
    }
}

#[test]
fn editor_build_from_yaml_invalid() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let result = ed.build_from_yaml("[unclosed");
        assert!(result.is_err());
    }
}

#[test]
fn editor_build_from_yaml_multiple_docs() {
    // Multiple documents in one snippet - takes first
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let node = ed.build_from_yaml("first").unwrap();
        ed.set_root(node).unwrap();
    }
    assert_eq!(doc.root().unwrap().scalar_str().unwrap(), "first");
}

// =============================================================================
// Reading During Edit
// =============================================================================

#[test]
fn editor_read_root_during_edit() {
    let mut doc = Document::parse_str("name: Alice").unwrap();
    {
        let ed = doc.edit();
        let root = ed.root().unwrap();
        assert_eq!(
            root.at_path("/name").unwrap().scalar_str().unwrap(),
            "Alice"
        );
    }
}

#[test]
fn editor_at_path_during_edit() {
    let mut doc = Document::parse_str("nested:\n  key: value").unwrap();
    {
        let ed = doc.edit();
        let node = ed.at_path("/nested/key").unwrap();
        assert_eq!(node.scalar_str().unwrap(), "value");
    }
}

#[test]
fn editor_at_path_nonexistent_during_edit() {
    let mut doc = Document::parse_str("key: value").unwrap();
    {
        let ed = doc.edit();
        assert!(ed.at_path("/nonexistent").is_none());
    }
}

#[test]
fn editor_root_on_empty_document() {
    let mut doc = Document::new().unwrap();
    {
        let ed = doc.edit();
        assert!(ed.root().is_none());
    }
}

// =============================================================================
// Node Building
// =============================================================================

#[test]
fn editor_build_scalar() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let scalar = ed.build_scalar("test value").unwrap();
        ed.set_root(scalar).unwrap();
    }
    assert_eq!(doc.root().unwrap().scalar_str().unwrap(), "test value");
}

#[test]
fn editor_build_sequence() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let seq = ed.build_sequence().unwrap();
        ed.set_root(seq).unwrap();
    }
    let root = doc.root().unwrap();
    assert!(root.is_sequence());
    assert_eq!(root.seq_len().unwrap(), 0);
}

#[test]
fn editor_build_mapping() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let map = ed.build_mapping().unwrap();
        ed.set_root(map).unwrap();
    }
    let root = doc.root().unwrap();
    assert!(root.is_mapping());
    assert_eq!(root.map_len().unwrap(), 0);
}

#[test]
fn editor_build_complex_yaml() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let node = ed
            .build_from_yaml("users:\n  - name: Alice\n    age: 30\n  - name: Bob\n    age: 25")
            .unwrap();
        ed.set_root(node).unwrap();
    }

    let root = doc.root().unwrap();
    assert!(root.is_mapping());
    assert_eq!(
        root.at_path("/users/0/name").unwrap().scalar_str().unwrap(),
        "Alice"
    );
    assert_eq!(
        root.at_path("/users/1/name").unwrap().scalar_str().unwrap(),
        "Bob"
    );
}

// =============================================================================
// Style Preservation
// =============================================================================

#[test]
fn editor_preserves_single_quotes() {
    let mut doc = Document::parse_str("name: plain").unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/name", "'single quoted'").unwrap();
    }

    let node = doc.at_path("/name").unwrap();
    assert_eq!(node.style(), NodeStyle::SingleQuoted);
    assert_eq!(node.scalar_str().unwrap(), "single quoted");
}

#[test]
fn editor_preserves_double_quotes() {
    let mut doc = Document::parse_str("name: plain").unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/name", "\"double quoted\"").unwrap();
    }

    let node = doc.at_path("/name").unwrap();
    assert_eq!(node.style(), NodeStyle::DoubleQuoted);
}

#[test]
fn editor_set_nested_value() {
    let mut doc = Document::parse_str("a:\n  b:\n    c: old").unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/a/b/c", "new").unwrap();
    }
    assert_eq!(doc.at_path("/a/b/c").unwrap().scalar_str().unwrap(), "new");
}

#[test]
fn editor_add_new_nested_key() {
    let mut doc = Document::parse_str("existing:\n  key: value").unwrap();
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/existing/new_key", "new_value").unwrap();
    }
    assert_eq!(
        doc.at_path("/existing/new_key")
            .unwrap()
            .scalar_str()
            .unwrap(),
        "new_value"
    );
    // Original key unchanged
    assert_eq!(
        doc.at_path("/existing/key").unwrap().scalar_str().unwrap(),
        "value"
    );
}

// =============================================================================
// Replace Root
// =============================================================================

#[test]
fn editor_replace_existing_root() {
    let mut doc = Document::parse_str("old: root").unwrap();
    {
        let mut ed = doc.edit();
        let new_root = ed.build_from_yaml("new: root").unwrap();
        ed.set_root(new_root).unwrap();
    }
    assert!(doc.at_path("/old").is_none());
    assert_eq!(doc.at_path("/new").unwrap().scalar_str().unwrap(), "root");
}

#[test]
fn editor_set_root_scalar() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let scalar = ed.build_from_yaml("just_a_scalar").unwrap();
        ed.set_root(scalar).unwrap();
    }
    let root = doc.root().unwrap();
    assert!(root.is_scalar());
    assert_eq!(root.scalar_str().unwrap(), "just_a_scalar");
}

#[test]
fn editor_set_root_sequence() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let seq = ed.build_from_yaml("[1, 2, 3]").unwrap();
        ed.set_root(seq).unwrap();
    }
    let root = doc.root().unwrap();
    assert!(root.is_sequence());
    assert_eq!(root.seq_len().unwrap(), 3);
}

// =============================================================================
// Handle-Level Assembly
// =============================================================================

#[test]
fn editor_build_structure_then_mutate_with_paths() {
    // Build a structure with handle-level API, then modify it with path-based API
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let mut root = ed.build_mapping().unwrap();
        let k = ed.build_scalar("name").unwrap();
        let v = ed.build_scalar("Alice").unwrap();
        ed.map_insert(&mut root, k, v).unwrap();
        let k2 = ed.build_scalar("age").unwrap();
        let v2 = ed.build_scalar("30").unwrap();
        ed.map_insert(&mut root, k2, v2).unwrap();
        ed.set_root(root).unwrap();
    }
    // Now use path-based mutation on the programmatically built structure
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/name", "'Bob'").unwrap();
        ed.delete_at("/age").unwrap();
        ed.set_yaml_at("/role", "admin").unwrap();
    }
    assert_eq!(doc.at_path("/name").unwrap().scalar_str().unwrap(), "Bob");
    assert!(doc.at_path("/age").is_none());
    assert_eq!(doc.at_path("/role").unwrap().scalar_str().unwrap(), "admin");
}

#[test]
fn editor_seq_append_then_append_at() {
    // Build a sequence with handle-level API, then extend it with path-based API
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let mut root = ed.build_mapping().unwrap();
        let k = ed.build_scalar("items").unwrap();
        let mut seq = ed.build_sequence().unwrap();
        let a = ed.build_scalar("a").unwrap();
        let b = ed.build_scalar("b").unwrap();
        ed.seq_append(&mut seq, a).unwrap();
        ed.seq_append(&mut seq, b).unwrap();
        ed.map_insert(&mut root, k, seq).unwrap();
        ed.set_root(root).unwrap();
    }
    {
        let mut ed = doc.edit();
        let c = ed.build_scalar("c").unwrap();
        ed.seq_append_at("/items", c).unwrap();
        ed.set_yaml_at("/items/0", "replaced").unwrap();
    }
    let items = doc.at_path("/items").unwrap();
    assert_eq!(items.seq_len().unwrap(), 3);
    assert_eq!(items.seq_get(0).unwrap().scalar_str().unwrap(), "replaced");
    assert_eq!(items.seq_get(1).unwrap().scalar_str().unwrap(), "b");
    assert_eq!(items.seq_get(2).unwrap().scalar_str().unwrap(), "c");
}

#[test]
fn editor_build_with_nulls_and_tags() {
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();
        let mut root = ed.build_mapping().unwrap();

        let k1 = ed.build_scalar("present").unwrap();
        let v1 = ed.build_scalar("yes").unwrap();
        ed.map_insert(&mut root, k1, v1).unwrap();

        let k2 = ed.build_scalar("missing").unwrap();
        let v2 = ed.build_null().unwrap();
        ed.map_insert(&mut root, k2, v2).unwrap();

        ed.set_tag(&mut root, "!mytype").unwrap();
        ed.set_root(root).unwrap();
    }
    let root = doc.root().unwrap();
    assert_eq!(root.tag_str().unwrap().unwrap(), "!mytype");
    assert_eq!(
        root.at_path("/present").unwrap().scalar_str().unwrap(),
        "yes"
    );
    // Null node has empty scalar_str
    let missing = root.at_path("/missing").unwrap();
    assert!(missing.is_scalar());
    // Replace the null with a real value via path API
    {
        let mut ed = doc.edit();
        ed.set_yaml_at("/missing", "now here").unwrap();
    }
    assert_eq!(
        doc.at_path("/missing").unwrap().scalar_str().unwrap(),
        "now here"
    );
}

#[test]
fn editor_nested_handle_assembly() {
    // Build a nested structure entirely with handle-level API
    let mut doc = Document::new().unwrap();
    {
        let mut ed = doc.edit();

        // Inner mapping: {host: localhost, port: 5432}
        let mut db = ed.build_mapping().unwrap();
        let k = ed.build_scalar("host").unwrap();
        let v = ed.build_scalar("localhost").unwrap();
        ed.map_insert(&mut db, k, v).unwrap();
        let k = ed.build_scalar("port").unwrap();
        let v = ed.build_scalar("5432").unwrap();
        ed.map_insert(&mut db, k, v).unwrap();

        // Sequence: [web, api]
        let mut services = ed.build_sequence().unwrap();
        let s1 = ed.build_scalar("web").unwrap();
        let s2 = ed.build_scalar("api").unwrap();
        ed.seq_append(&mut services, s1).unwrap();
        ed.seq_append(&mut services, s2).unwrap();

        // Root mapping
        let mut root = ed.build_mapping().unwrap();
        let k = ed.build_scalar("database").unwrap();
        ed.map_insert(&mut root, k, db).unwrap();
        let k = ed.build_scalar("services").unwrap();
        ed.map_insert(&mut root, k, services).unwrap();

        ed.set_root(root).unwrap();
    }
    assert_eq!(
        doc.at_path("/database/host").unwrap().scalar_str().unwrap(),
        "localhost"
    );
    assert_eq!(
        doc.at_path("/services/1").unwrap().scalar_str().unwrap(),
        "api"
    );

    // Emit and reparse to verify structural integrity
    let yaml = doc.emit().unwrap();
    let reparsed = Document::parse_str(&yaml).unwrap();
    assert_eq!(
        reparsed
            .at_path("/database/port")
            .unwrap()
            .scalar_str()
            .unwrap(),
        "5432"
    );
    assert_eq!(
        reparsed
            .at_path("/services/0")
            .unwrap()
            .scalar_str()
            .unwrap(),
        "web"
    );
}

#[test]
fn editor_copy_then_tag_and_insert() {
    // Copy a node from one document, tag it, and insert into another
    let src = Document::parse_str("template:\n  x: 1\n  y: 2").unwrap();
    let mut dest = Document::new().unwrap();
    {
        let mut ed = dest.edit();
        let mut copied = ed.copy_node(src.at_path("/template").unwrap()).unwrap();
        ed.set_tag(&mut copied, "!copied").unwrap();
        ed.set_root(copied).unwrap();
    }
    let root = dest.root().unwrap();
    assert_eq!(root.tag_str().unwrap().unwrap(), "!copied");
    assert_eq!(root.at_path("/x").unwrap().scalar_str().unwrap(), "1");
}