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
//! Visitor pattern for traversing YAML AST nodes.
//!
//! Allows traversing and processing YAML structures without modifying node types.
//! Useful for validation, transformation, collection, or analysis.
//!
//! # Example
//!
//! ```rust
//! use yaml_edit::visitor::{YamlVisitor, YamlAccept};
//! use yaml_edit::{Document, Scalar, Mapping, Sequence};
//! use std::str::FromStr;
//!
//! struct ScalarCounter {
//!     count: usize,
//! }
//!
//! impl YamlVisitor for ScalarCounter {
//!     fn visit_scalar(&mut self, _scalar: &Scalar) {
//!         self.count += 1;
//!     }
//! }
//!
//! let doc = Document::from_str("key: value\nlist:\n  - item1\n  - item2").unwrap();
//! let mut counter = ScalarCounter { count: 0 };
//! doc.accept(&mut counter);
//! assert_eq!(counter.count, 5); // "key", "value", "list", "item1", "item2"
//! ```
//!
//! Default traversal implementations automatically visit child nodes. Override
//! `visit_mapping` or `visit_sequence` for custom traversal logic.

use crate::yaml::{Document, Mapping, Scalar, Sequence, YamlFile};
use rowan::ast::AstNode;

/// Trait for implementing the visitor pattern on YAML nodes.
pub trait YamlVisitor {
    /// Visit a YAML root node
    fn visit_yaml(&mut self, yaml: &YamlFile) {
        // Default implementation visits all documents
        for doc in yaml.documents() {
            self.visit_document(&doc);
        }
    }

    /// Visit a document node
    fn visit_document(&mut self, document: &Document) {
        // Default implementation visits the document's content
        if let Some(mapping) = document.as_mapping() {
            self.visit_mapping(&mapping);
        } else if let Some(sequence) = document.as_sequence() {
            self.visit_sequence(&sequence);
        } else if let Some(scalar) = document.as_scalar() {
            self.visit_scalar(&scalar);
        }
    }

    /// Visit a scalar node
    fn visit_scalar(&mut self, _scalar: &Scalar) {}

    /// Visit a mapping node
    ///
    /// The default implementation traverses all key-value pairs in the mapping,
    /// visiting both keys and values recursively. Override this method if you need
    /// custom logic when encountering mappings.
    fn visit_mapping(&mut self, mapping: &Mapping) {
        self.walk_mapping(mapping);
    }

    /// Visit a sequence node
    ///
    /// The default implementation traverses all items in the sequence recursively.
    /// Override this method if you need custom logic when encountering sequences.
    fn visit_sequence(&mut self, sequence: &Sequence) {
        self.walk_sequence(sequence);
    }

    /// Traverse all key-value pairs in a mapping (helper for default traversal).
    ///
    /// This method is called by the default `visit_mapping` implementation.
    /// You can call it explicitly if you override `visit_mapping` and want to
    /// preserve the default traversal behavior.
    fn walk_mapping(&mut self, mapping: &Mapping) {
        use crate::yaml::{extract_mapping, extract_scalar, extract_sequence};

        for (key_node, value_node) in mapping.pairs() {
            // Visit key
            if let Some(scalar) = extract_scalar(&key_node) {
                self.visit_scalar(&scalar);
            } else if let Some(sequence) = extract_sequence(&key_node) {
                self.visit_sequence(&sequence);
            } else if let Some(mapping) = extract_mapping(&key_node) {
                self.visit_mapping(&mapping);
            }

            // Visit value
            if let Some(scalar) = extract_scalar(&value_node) {
                self.visit_scalar(&scalar);
            } else if let Some(nested_mapping) = extract_mapping(&value_node) {
                self.visit_mapping(&nested_mapping);
            } else if let Some(nested_sequence) = extract_sequence(&value_node) {
                self.visit_sequence(&nested_sequence);
            }
        }
    }

    /// Traverse all items in a sequence (helper for default traversal).
    ///
    /// This method is called by the default `visit_sequence` implementation.
    /// You can call it explicitly if you override `visit_sequence` and want to
    /// preserve the default traversal behavior.
    fn walk_sequence(&mut self, sequence: &Sequence) {
        for item in sequence.items() {
            if let Some(scalar) = Scalar::cast(item.clone()) {
                self.visit_scalar(&scalar);
            } else if let Some(nested_mapping) = Mapping::cast(item.clone()) {
                self.visit_mapping(&nested_mapping);
            } else if let Some(nested_sequence) = Sequence::cast(item.clone()) {
                self.visit_sequence(&nested_sequence);
            }
        }
    }
}

/// Trait for nodes that can accept a visitor
pub trait YamlAccept {
    /// Accept a visitor for traversal
    fn accept<V: YamlVisitor>(&self, visitor: &mut V);
}

impl YamlAccept for YamlFile {
    fn accept<V: YamlVisitor>(&self, visitor: &mut V) {
        visitor.visit_yaml(self);
    }
}

impl YamlAccept for Document {
    fn accept<V: YamlVisitor>(&self, visitor: &mut V) {
        visitor.visit_document(self);
    }
}

impl YamlAccept for Scalar {
    fn accept<V: YamlVisitor>(&self, visitor: &mut V) {
        visitor.visit_scalar(self);
    }
}

impl YamlAccept for Mapping {
    fn accept<V: YamlVisitor>(&self, visitor: &mut V) {
        visitor.visit_mapping(self);
    }
}

impl YamlAccept for Sequence {
    fn accept<V: YamlVisitor>(&self, visitor: &mut V) {
        visitor.visit_sequence(self);
    }
}

/// A visitor that collects all scalar values from a YAML document
pub struct ScalarCollector {
    /// The collected scalar values
    pub scalars: Vec<String>,
}

impl ScalarCollector {
    /// Create a new scalar collector
    pub fn new() -> Self {
        Self {
            scalars: Vec::new(),
        }
    }
}

impl Default for ScalarCollector {
    fn default() -> Self {
        Self::new()
    }
}

impl YamlVisitor for ScalarCollector {
    fn visit_scalar(&mut self, scalar: &Scalar) {
        self.scalars.push(scalar.to_string());
    }
    // Default traversal methods handle mapping and sequence traversal automatically
}

/// A visitor that counts different types of nodes
pub struct NodeCounter {
    /// Number of document nodes encountered
    pub document_count: usize,
    /// Number of scalar nodes encountered
    pub scalar_count: usize,
    /// Number of mapping nodes encountered
    pub mapping_count: usize,
    /// Number of sequence nodes encountered
    pub sequence_count: usize,
}

impl NodeCounter {
    /// Create a new node counter
    pub fn new() -> Self {
        Self {
            document_count: 0,
            scalar_count: 0,
            mapping_count: 0,
            sequence_count: 0,
        }
    }

    /// Get total node count
    pub fn total(&self) -> usize {
        self.document_count + self.scalar_count + self.mapping_count + self.sequence_count
    }
}

impl Default for NodeCounter {
    fn default() -> Self {
        Self::new()
    }
}

impl YamlVisitor for NodeCounter {
    fn visit_document(&mut self, document: &Document) {
        self.document_count += 1;
        // Continue visiting children
        if let Some(mapping) = document.as_mapping() {
            self.visit_mapping(&mapping);
        } else if let Some(sequence) = document.as_sequence() {
            self.visit_sequence(&sequence);
        } else if let Some(scalar) = document.as_scalar() {
            self.visit_scalar(&scalar);
        }
    }

    fn visit_scalar(&mut self, _scalar: &Scalar) {
        self.scalar_count += 1;
    }

    fn visit_mapping(&mut self, mapping: &Mapping) {
        self.mapping_count += 1;
        // Call default traversal to visit children
        self.walk_mapping(mapping);
    }

    fn visit_sequence(&mut self, sequence: &Sequence) {
        self.sequence_count += 1;
        // Call default traversal to visit children
        self.walk_sequence(sequence);
    }
}

/// A visitor that transforms scalar values
pub struct ScalarTransformer<F>
where
    F: FnMut(&str) -> String,
{
    transform: F,
    transformed: Vec<(String, String)>, // (original, transformed) pairs
}

impl<F> ScalarTransformer<F>
where
    F: FnMut(&str) -> String,
{
    /// Create a new scalar transformer with the given transformation function
    pub fn new(transform: F) -> Self {
        Self {
            transform,
            transformed: Vec::new(),
        }
    }

    /// Get the transformed pairs
    pub fn results(&self) -> &[(String, String)] {
        &self.transformed
    }
}

impl<F> YamlVisitor for ScalarTransformer<F>
where
    F: FnMut(&str) -> String,
{
    fn visit_scalar(&mut self, scalar: &Scalar) {
        let original = scalar.to_string();
        let transformed = (self.transform)(&original);
        self.transformed.push((original, transformed));
    }

    // Default traversal methods handle mapping and sequence traversal automatically
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::YamlFile;

    #[test]
    fn test_scalar_collector() {
        let yaml_text = r#"
name: John Doe
age: 30
address:
  street: 123 Main St
  city: New York
  country: USA
hobbies:
  - reading
  - coding
  - hiking
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut collector = ScalarCollector::new();
        yaml.accept(&mut collector);

        // Should collect all scalar values from the document
        assert_eq!(
            collector.scalars,
            vec![
                "name",
                "John Doe",
                "age",
                "30",
                "address",
                "street",
                "123 Main St",
                "city",
                "New York",
                "country",
                "USA",
                "hobbies",
                "reading",
                "coding",
                "hiking",
            ]
        );
    }

    #[test]
    fn test_node_counter() {
        let yaml_text = r#"
name: John Doe
age: 30
address:
  street: 123 Main St
  city: New York
  country: USA
hobbies:
  - reading
  - coding
  - hiking
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut counter = NodeCounter::new();
        yaml.accept(&mut counter);

        // Should count all node types
        assert_eq!(counter.document_count, 1);
        assert_eq!(counter.scalar_count, 15);
        assert_eq!(counter.mapping_count, 2); // Root mapping and address mapping
        assert_eq!(counter.sequence_count, 1); // hobbies sequence

        // Total should be sum of all counts
        assert_eq!(
            counter.total(),
            counter.document_count
                + counter.scalar_count
                + counter.mapping_count
                + counter.sequence_count
        );
    }

    #[test]
    fn test_scalar_transformer() {
        let yaml_text = r#"
name: john
city: new york
country: usa
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        // Transform all scalars to uppercase
        let mut transformer = ScalarTransformer::new(|s: &str| s.to_uppercase());
        yaml.accept(&mut transformer);

        let results = transformer.results();

        // Check that transformations were applied
        assert_eq!(
            results,
            &[
                ("name".to_string(), "NAME".to_string()),
                ("john".to_string(), "JOHN".to_string()),
                ("city".to_string(), "CITY".to_string()),
                ("new york".to_string(), "NEW YORK".to_string()),
                ("country".to_string(), "COUNTRY".to_string()),
                ("usa".to_string(), "USA".to_string()),
            ]
        );
    }

    #[test]
    fn test_visitor_on_sequence() {
        let yaml_text = r#"
- item1
- item2
- nested:
    - subitem1
    - subitem2
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut collector = ScalarCollector::new();
        yaml.accept(&mut collector);

        // Should collect all scalars including nested ones
        assert_eq!(
            collector.scalars,
            vec!["item1", "item2", "nested", "subitem1", "subitem2"]
        );
    }

    #[test]
    fn test_visitor_on_empty_document() {
        let yaml_text = "";

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut counter = NodeCounter::new();
        yaml.accept(&mut counter);

        // Empty document should have no nodes (or minimal structure)
        assert_eq!(counter.total(), 0);
    }

    #[test]
    fn test_custom_visitor() {
        // Define a custom visitor that counts only keys in mappings
        struct KeyCounter {
            key_count: usize,
        }

        impl YamlVisitor for KeyCounter {
            fn visit_scalar(&mut self, _scalar: &crate::Scalar) {
                // Don't count scalars that are not keys
            }

            fn visit_mapping(&mut self, mapping: &crate::Mapping) {
                // Count keys in this mapping
                for (_key, value) in mapping.iter() {
                    self.key_count += 1;
                    // Recursively visit nested structures
                    if let Some(nested_mapping) = value.as_mapping() {
                        nested_mapping.accept(self);
                    } else if let Some(nested_sequence) = value.as_sequence() {
                        nested_sequence.accept(self);
                    }
                }
            }

            fn visit_sequence(&mut self, sequence: &crate::Sequence) {
                // Visit items in sequence to find nested mappings
                for value in sequence.values() {
                    if let Some(nested_mapping) = value.as_mapping() {
                        nested_mapping.accept(self);
                    } else if let Some(nested_sequence) = value.as_sequence() {
                        nested_sequence.accept(self);
                    }
                }
            }
        }

        let yaml_text = r#"
name: John
age: 30
address:
  street: 123 Main St
  city: New York
metadata:
  created: 2024-01-01
  updated: 2024-01-02
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut key_counter = KeyCounter { key_count: 0 };
        yaml.accept(&mut key_counter);

        // Should count: name, age, address, street, city, metadata, created, updated = 8 keys
        assert_eq!(key_counter.key_count, 8);
    }

    #[test]
    fn test_visitor_with_multiple_documents() {
        let yaml_text = r#"
---
doc1: value1
---
doc2: value2
---
doc3: value3
"#;

        let parsed = YamlFile::parse(yaml_text);
        let yaml = parsed.tree();

        let mut counter = NodeCounter::new();
        yaml.accept(&mut counter);

        // Should count 3 documents
        assert_eq!(counter.document_count, 3);
        assert!(counter.scalar_count >= 6); // At least 3 keys and 3 values
    }
}