rust-yaml 0.0.5

A fast, safe YAML 1.2 library for Rust
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! YAML emitter for generating text output

use crate::{CommentedValue, Comments, Error, IndentStyle, QuoteStyle, Result, Value};
use std::collections::HashMap;
use std::io::Write;

/// Trait for YAML emitters that generate text output from values
pub trait Emitter {
    /// Emit a value to the output
    fn emit<W: Write>(&mut self, value: &Value, writer: W) -> Result<()>;

    /// Emit a commented value to the output with comment preservation
    fn emit_commented<W: Write>(&mut self, value: &CommentedValue, writer: W) -> Result<()>;

    /// Emit a commented value with specific indent style
    fn emit_with_style<W: Write>(
        &mut self,
        value: &CommentedValue,
        indent_style: &IndentStyle,
        writer: W,
    ) -> Result<()>;

    /// Reset the emitter state
    fn reset(&mut self);
}

/// Information about shared values for anchor/alias emission
#[derive(Debug, Clone)]
struct ValueInfo {
    anchor_name: String,
    first_occurrence: bool,
}

/// Basic emitter implementation that generates clean YAML
#[derive(Debug)]
pub struct BasicEmitter {
    indent: usize,
    current_indent: usize,
    shared_values: HashMap<Value, ValueInfo>,
    anchor_counter: usize,
    indent_style: IndentStyle,
    yaml_version: Option<(u8, u8)>,
    tag_directives: Vec<(String, String)>,
}

#[allow(dead_code)]
impl BasicEmitter {
    /// Create a new emitter with default settings
    pub fn new() -> Self {
        Self {
            indent: 2,
            current_indent: 0,
            shared_values: HashMap::new(),
            anchor_counter: 0,
            indent_style: IndentStyle::default(),
            yaml_version: None,
            tag_directives: Vec::new(),
        }
    }

    /// Create an emitter with custom indent
    pub fn with_indent(indent: usize) -> Self {
        Self {
            indent,
            current_indent: 0,
            shared_values: HashMap::new(),
            anchor_counter: 0,
            indent_style: IndentStyle::Spaces(indent),
            yaml_version: None,
            tag_directives: Vec::new(),
        }
    }

    /// Create an emitter with specific indent style
    pub fn with_indent_style(indent_style: IndentStyle) -> Self {
        let indent = match &indent_style {
            IndentStyle::Spaces(width) => *width,
            IndentStyle::Tabs => 1, // Tabs count as 1 indent level
        };
        Self {
            indent,
            current_indent: 0,
            shared_values: HashMap::new(),
            anchor_counter: 0,
            indent_style,
            yaml_version: None,
            tag_directives: Vec::new(),
        }
    }

    /// Set the YAML version directive
    pub fn set_yaml_version(&mut self, major: u8, minor: u8) {
        self.yaml_version = Some((major, minor));
    }

    /// Add a TAG directive
    pub fn add_tag_directive(&mut self, handle: String, prefix: String) {
        self.tag_directives.push((handle, prefix));
    }

    /// Clear all directives
    pub fn clear_directives(&mut self) {
        self.yaml_version = None;
        self.tag_directives.clear();
    }

    /// Emit directives to the writer
    fn emit_directives<W: Write>(&self, writer: &mut W) -> Result<()> {
        // Emit YAML version directive if set
        if let Some((major, minor)) = self.yaml_version {
            writeln!(writer, "%YAML {}.{}", major, minor).map_err(|e| Error::Emission {
                message: format!("Failed to write YAML directive: {}", e),
            })?;
        }

        // Emit TAG directives
        for (handle, prefix) in &self.tag_directives {
            writeln!(writer, "%TAG {} {}", handle, prefix).map_err(|e| Error::Emission {
                message: format!("Failed to write TAG directive: {}", e),
            })?;
        }

        // If we emitted any directives, emit document start marker
        if self.yaml_version.is_some() || !self.tag_directives.is_empty() {
            writeln!(writer, "---").map_err(|e| Error::Emission {
                message: format!("Failed to write document start marker: {}", e),
            })?;
        }

        Ok(())
    }

    /// Analyze the value tree to identify shared values that need anchors
    fn analyze_shared_values(&mut self, value: &Value) {
        let mut value_counts = HashMap::new();
        self.count_value_occurrences(value, &mut value_counts);

        // Generate anchors for values that occur more than once and are complex
        for (val, count) in value_counts {
            if count > 1 && self.is_complex_value(&val) {
                let anchor_name = format!("anchor{}", self.anchor_counter);
                self.anchor_counter += 1;
                self.shared_values.insert(
                    val,
                    ValueInfo {
                        anchor_name,
                        first_occurrence: true,
                    },
                );
            }
        }
    }

    /// Recursively count occurrences of each value
    fn count_value_occurrences(&self, value: &Value, counts: &mut HashMap<Value, usize>) {
        // Only track complex values (sequences and mappings)
        if self.is_complex_value(value) {
            *counts.entry(value.clone()).or_insert(0) += 1;
        }

        // Recurse into child values
        match value {
            Value::Sequence(seq) => {
                for item in seq {
                    self.count_value_occurrences(item, counts);
                }
            }
            Value::Mapping(map) => {
                for (key, val) in map {
                    self.count_value_occurrences(key, counts);
                    self.count_value_occurrences(val, counts);
                }
            }
            _ => {}
        }
    }

    /// Check if a value is complex enough to warrant anchor/alias handling
    const fn is_complex_value(&self, value: &Value) -> bool {
        matches!(value, Value::Sequence(_) | Value::Mapping(_))
    }

    /// Generate next anchor name
    fn next_anchor_name(&mut self) -> String {
        let name = format!("anchor{}", self.anchor_counter);
        self.anchor_counter += 1;
        name
    }

    /// Update the indent style (useful for round-trip preservation)
    pub const fn set_indent_style(&mut self, indent_style: IndentStyle) {
        self.indent = match &indent_style {
            IndentStyle::Spaces(width) => *width,
            IndentStyle::Tabs => 1,
        };
        self.indent_style = indent_style;
    }

    /// Write indentation to the output
    fn write_indent<W: Write>(&self, writer: &mut W) -> Result<()> {
        match &self.indent_style {
            IndentStyle::Spaces(_width) => {
                let total_spaces = self.current_indent;
                for _ in 0..total_spaces {
                    write!(writer, " ")?;
                }
            }
            IndentStyle::Tabs => {
                let indent_levels = self.current_indent / self.indent;
                for _ in 0..indent_levels {
                    write!(writer, "\t")?;
                }
            }
        }
        Ok(())
    }

    /// Write leading comments to the output
    fn emit_leading_comments<W: Write>(&self, comments: &[String], writer: &mut W) -> Result<()> {
        for comment in comments {
            self.write_indent(writer)?;
            writeln!(writer, "# {}", comment)?;
        }
        Ok(())
    }

    /// Write a trailing comment on the same line
    fn emit_trailing_comment<W: Write>(&self, comment: &str, writer: &mut W) -> Result<()> {
        write!(writer, " # {}", comment)?;
        Ok(())
    }

    /// Write inner comments (between collection items)
    fn emit_inner_comments<W: Write>(&self, comments: &[String], writer: &mut W) -> Result<()> {
        for comment in comments {
            writeln!(writer)?;
            self.write_indent(writer)?;
            writeln!(writer, "# {}", comment)?;
        }
        Ok(())
    }

    /// Emit a scalar value
    fn emit_scalar<W: Write>(&self, value: &Value, writer: &mut W) -> Result<()> {
        self.emit_scalar_with_comments(value, None, writer)
    }

    /// Emit a scalar value with optional comments and style
    fn emit_scalar_with_comments<W: Write>(
        &self,
        value: &Value,
        comments: Option<&Comments>,
        writer: &mut W,
    ) -> Result<()> {
        self.emit_scalar_with_comments_and_style(value, comments, None, writer)
    }

    /// Emit a scalar value with optional comments and style information
    fn emit_scalar_with_comments_and_style<W: Write>(
        &self,
        value: &Value,
        comments: Option<&Comments>,
        quote_style: Option<&QuoteStyle>,
        writer: &mut W,
    ) -> Result<()> {
        // Emit leading comments
        if let Some(comments) = comments {
            self.emit_leading_comments(&comments.leading, writer)?;
        }

        // Emit the scalar value
        match value {
            Value::Null => write!(writer, "null")?,
            Value::Bool(b) => write!(writer, "{}", b)?,
            Value::Int(i) => write!(writer, "{}", i)?,
            Value::Float(f) => {
                // Handle special float values
                if f.is_nan() {
                    write!(writer, ".nan")?;
                } else if f.is_infinite() {
                    if f.is_sign_positive() {
                        write!(writer, ".inf")?;
                    } else {
                        write!(writer, "-.inf")?;
                    }
                } else {
                    // Ensure the float is written with decimal point to preserve type
                    if f.fract() == 0.0 {
                        write!(writer, "{:.1}", f)?;
                    } else {
                        write!(writer, "{}", f)?;
                    }
                }
            }
            Value::String(s) => {
                self.emit_string_with_style(s, quote_style, writer)?;
            }
            _ => return Err(Error::emission("Non-scalar passed to emit_scalar")),
        }

        // Emit trailing comment
        if let Some(comments) = comments {
            if let Some(ref trailing) = comments.trailing {
                self.emit_trailing_comment(trailing, writer)?;
            }
        }

        Ok(())
    }

    /// Emit a string, choosing appropriate quoting style
    fn emit_string<W: Write>(&self, s: &str, writer: &mut W) -> Result<()> {
        self.emit_string_with_style(s, None, writer)
    }

    /// Emit a string with specific quote style
    fn emit_string_with_style<W: Write>(
        &self,
        s: &str,
        preferred_style: Option<&QuoteStyle>,
        writer: &mut W,
    ) -> Result<()> {
        match preferred_style {
            Some(QuoteStyle::Single) => self.emit_single_quoted_string(s, writer),
            Some(QuoteStyle::Double) => self.emit_double_quoted_string(s, writer),
            Some(QuoteStyle::Plain) | None => {
                // Check if string needs quoting
                if self.needs_quoting(s) {
                    // Default to double quotes when quoting is needed
                    self.emit_double_quoted_string(s, writer)
                } else {
                    write!(writer, "{}", s)?;
                    Ok(())
                }
            }
        }
    }

    /// Check if a string needs to be quoted
    fn needs_quoting(&self, s: &str) -> bool {
        if s.is_empty() {
            return true;
        }

        // String needs quoting if it could be interpreted as another type
        if s == "null"
            || s == "~"
            || s == "true"
            || s == "false"
            || s == "yes"
            || s == "no"
            || s == "on"
            || s == "off"
            || s.parse::<i64>().is_ok()
            || s.parse::<f64>().is_ok()
        {
            return true;
        }

        // Check for version-like strings that could be ambiguous
        // e.g., "2.1.0", "1.2.3", etc. - these contain dots and could be misinterpreted
        if s.chars().any(|c| c == '.') && s.chars().any(|c| c.is_ascii_digit()) {
            // If it looks like a version number (contains dots and digits), quote it
            // to prevent parsing ambiguity
            return true;
        }

        // Check for special characters
        if s.contains('\n')
            || s.contains('\r')
            || s.contains('\t')
            || s.contains('"')
            || s.contains('\'')
            || s.contains(':')
            || s.contains('#')
            || s.contains('-')
            || s.contains('[')
            || s.contains(']')
            || s.contains('{')
            || s.contains('}')
            || s.starts_with(' ')
            || s.ends_with(' ')
        {
            return true;
        }

        false
    }

    /// Emit a double-quoted string
    fn emit_double_quoted_string<W: Write>(&self, s: &str, writer: &mut W) -> Result<()> {
        write!(writer, "\"")?;
        for ch in s.chars() {
            match ch {
                '"' => write!(writer, "\\\"")?,
                '\\' => write!(writer, "\\\\")?,
                '\n' => write!(writer, "\\n")?,
                '\r' => write!(writer, "\\r")?,
                '\t' => write!(writer, "\\t")?,
                c if c.is_control() => write!(writer, "\\u{:04x}", c as u32)?,
                c => write!(writer, "{}", c)?,
            }
        }
        write!(writer, "\"")?;
        Ok(())
    }

    /// Emit a single-quoted string
    fn emit_single_quoted_string<W: Write>(&self, s: &str, writer: &mut W) -> Result<()> {
        write!(writer, "'")?;
        for ch in s.chars() {
            match ch {
                '\'' => write!(writer, "''")?, // In YAML, single quotes are escaped by doubling
                c => write!(writer, "{}", c)?,
            }
        }
        write!(writer, "'")?;
        Ok(())
    }

    /// Emit a quoted string (legacy method)
    fn emit_quoted_string<W: Write>(&self, s: &str, writer: &mut W) -> Result<()> {
        self.emit_double_quoted_string(s, writer)
    }

    /// Emit a sequence (array/list)
    fn emit_sequence<W: Write>(&mut self, seq: &[Value], writer: &mut W) -> Result<()> {
        if seq.is_empty() {
            write!(writer, "[]")?;
            return Ok(());
        }

        for (index, item) in seq.iter().enumerate() {
            if index > 0 {
                writeln!(writer)?;
            }
            self.write_indent(writer)?;
            write!(writer, "- ")?;

            match item {
                Value::Sequence(_) | Value::Mapping(_) => {
                    writeln!(writer)?; // Add newline before nested structure
                    self.current_indent += self.indent;
                    self.emit_value(item, writer)?;
                    self.current_indent -= self.indent;
                }
                _ => {
                    self.emit_scalar(item, writer)?;
                }
            }
        }

        Ok(())
    }

    /// Emit a mapping with an anchor
    fn emit_mapping_with_anchor<W: Write>(
        &mut self,
        map: &indexmap::IndexMap<Value, Value>,
        anchor: &str,
        writer: &mut W,
    ) -> Result<()> {
        if map.is_empty() {
            write!(writer, "&{} {{}}", anchor)?;
            return Ok(());
        }

        // For anchored mappings, we need to format it as:
        // &anchor
        // key1: value1
        // key2: value2
        writeln!(writer, "&{}", anchor)?;

        let mut first = true;
        for (key, value) in map {
            if !first {
                writeln!(writer)?;
            }
            first = false;

            self.write_indent(writer)?;

            // Handle both simple and complex keys
            let is_complex_key = matches!(key, Value::Sequence(_) | Value::Mapping(_));

            if is_complex_key {
                // Complex key - emit with explicit key marker
                write!(writer, "? ")?;
                self.emit_value(key, writer)?;
                writeln!(writer)?;
                self.write_indent(writer)?;
            } else {
                // Simple key
                self.emit_scalar(key, writer)?;
            }

            write!(writer, ": ")?;

            match value {
                Value::Sequence(_) | Value::Mapping(_) => {
                    writeln!(writer)?; // Add newline before nested structure
                    self.current_indent += self.indent;
                    self.emit_value(value, writer)?;
                    self.current_indent -= self.indent;
                }
                _ => {
                    self.emit_scalar(value, writer)?;
                }
            }
        }

        Ok(())
    }

    /// Emit a mapping (dictionary/object)
    fn emit_mapping<W: Write>(
        &mut self,
        map: &indexmap::IndexMap<Value, Value>,
        writer: &mut W,
    ) -> Result<()> {
        if map.is_empty() {
            write!(writer, "{{}}")?;
            return Ok(());
        }

        let mut first = true;
        for (key, value) in map {
            if !first {
                writeln!(writer)?;
            }
            first = false;

            self.write_indent(writer)?;

            // Handle both simple and complex keys
            let is_complex_key = matches!(key, Value::Sequence(_) | Value::Mapping(_));

            if is_complex_key {
                // Complex key - emit with explicit key marker and flow style
                write!(writer, "? ")?;
                match key {
                    Value::Mapping(map) => {
                        // Emit mapping in flow style to avoid ambiguity
                        self.emit_mapping_flow_style(map, writer)?;
                    }
                    Value::Sequence(seq) => {
                        // Emit sequence in flow style
                        self.emit_sequence_flow_style(seq, writer)?;
                    }
                    _ => {
                        self.emit_value(key, writer)?;
                    }
                }
                writeln!(writer)?;
                self.write_indent(writer)?;
            } else {
                // Simple key
                self.emit_scalar(key, writer)?;
            }

            write!(writer, ": ")?;

            match value {
                Value::Sequence(_) | Value::Mapping(_) => {
                    writeln!(writer)?; // Add newline before nested structure
                    self.current_indent += self.indent;
                    self.emit_value(value, writer)?;
                    self.current_indent -= self.indent;
                }
                _ => {
                    self.emit_scalar(value, writer)?;
                }
            }
        }

        Ok(())
    }

    /// Emit a mapping in flow style for complex keys
    fn emit_mapping_flow_style<W: Write>(
        &self,
        map: &indexmap::IndexMap<Value, Value>,
        writer: &mut W,
    ) -> Result<()> {
        write!(writer, "{{")?;
        let mut first = true;
        for (key, value) in map {
            if !first {
                write!(writer, ", ")?;
            }
            first = false;

            // Emit key (handle nested complex values)
            match key {
                Value::Mapping(nested_map) => {
                    self.emit_mapping_flow_style(nested_map, writer)?;
                }
                Value::Sequence(nested_seq) => {
                    self.emit_sequence_flow_style(nested_seq, writer)?;
                }
                _ => {
                    self.emit_scalar(key, writer)?;
                }
            }

            write!(writer, ": ")?;

            // Emit value (handle nested complex values)
            match value {
                Value::Mapping(nested_map) => {
                    self.emit_mapping_flow_style(nested_map, writer)?;
                }
                Value::Sequence(nested_seq) => {
                    self.emit_sequence_flow_style(nested_seq, writer)?;
                }
                _ => {
                    self.emit_scalar(value, writer)?;
                }
            }
        }
        write!(writer, "}}")?;
        Ok(())
    }

    /// Emit a sequence in flow style for complex keys
    fn emit_sequence_flow_style<W: Write>(&self, seq: &[Value], writer: &mut W) -> Result<()> {
        write!(writer, "[")?;
        let mut first = true;
        for item in seq {
            if !first {
                write!(writer, ", ")?;
            }
            first = false;
            // Handle nested complex values
            match item {
                Value::Mapping(nested_map) => {
                    self.emit_mapping_flow_style(nested_map, writer)?;
                }
                Value::Sequence(nested_seq) => {
                    self.emit_sequence_flow_style(nested_seq, writer)?;
                }
                _ => {
                    self.emit_scalar(item, writer)?;
                }
            }
        }
        write!(writer, "]")?;
        Ok(())
    }

    /// Emit any value, dispatching to the appropriate method with anchor/alias support
    fn emit_value<W: Write>(&mut self, value: &Value, writer: &mut W) -> Result<()> {
        // Check if this value has an anchor/alias
        if let Some(info) = self.shared_values.get(value).cloned() {
            if info.first_occurrence {
                // First occurrence: emit with anchor
                match value {
                    Value::Sequence(seq) => {
                        write!(writer, "&{} ", info.anchor_name)?;
                        self.emit_sequence(seq, writer)?;
                    }
                    Value::Mapping(map) => {
                        self.emit_mapping_with_anchor(map, &info.anchor_name, writer)?;
                    }
                    _ => self.emit_scalar(value, writer)?, // Scalars shouldn't be shared
                }
                // Mark as no longer first occurrence
                if let Some(info_mut) = self.shared_values.get_mut(value) {
                    info_mut.first_occurrence = false;
                }
            } else {
                // Subsequent occurrence: emit alias
                write!(writer, "*{}", info.anchor_name)?;
            }
        } else {
            // Regular value without sharing
            match value {
                Value::Sequence(seq) => self.emit_sequence(seq, writer),
                Value::Mapping(map) => self.emit_mapping(map, writer),
                _ => self.emit_scalar(value, writer),
            }?;
        }
        Ok(())
    }

    /// Emit any value (old method for backwards compatibility)
    fn emit_value_simple<W: Write>(&mut self, value: &Value, writer: &mut W) -> Result<()> {
        match value {
            Value::Sequence(seq) => self.emit_sequence(seq, writer),
            Value::Mapping(map) => self.emit_mapping(map, writer),
            _ => self.emit_scalar(value, writer),
        }
    }

    /// Emit a commented value with full comment and style support
    fn emit_commented_value<W: Write>(
        &mut self,
        commented: &CommentedValue,
        writer: &mut W,
    ) -> Result<()> {
        let comments = if commented.has_comments() {
            Some(&commented.comments)
        } else {
            None
        };

        let quote_style = commented.quote_style();

        match &commented.value {
            Value::Sequence(_) | Value::Mapping(_) => {
                // For collections, emit leading comments first
                if let Some(comments) = comments {
                    self.emit_leading_comments(&comments.leading, writer)?;
                }
                self.emit_value(&commented.value, writer)?;

                // Emit inner comments for collections
                if let Some(comments) = comments {
                    if !comments.inner.is_empty() {
                        self.emit_inner_comments(&comments.inner, writer)?;
                    }
                }

                // Trailing comments for collections go on a new line
                if let Some(comments) = comments {
                    if let Some(ref trailing) = comments.trailing {
                        writeln!(writer)?;
                        self.write_indent(writer)?;
                        writeln!(writer, "# {}", trailing)?;
                    }
                }
            }
            _ => {
                // For scalars, use the scalar comment and style method
                self.emit_scalar_with_comments_and_style(
                    &commented.value,
                    comments,
                    quote_style,
                    writer,
                )?;
            }
        }

        Ok(())
    }

    /// Emit a CommentedValue with comment preservation (public API)
    pub fn emit_commented_value_public<W: Write>(
        &mut self,
        commented: &CommentedValue,
        writer: W,
    ) -> Result<()> {
        let mut writer = writer;
        self.emit_commented_value(commented, &mut writer)
    }
}

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

impl Emitter for BasicEmitter {
    fn emit<W: Write>(&mut self, value: &Value, mut writer: W) -> Result<()> {
        // Reset state
        self.current_indent = 0;
        self.shared_values.clear();
        self.anchor_counter = 0;

        // Emit directives if any
        self.emit_directives(&mut writer)?;

        // Analyze for shared values first
        self.analyze_shared_values(value);

        // For top-level sequences, add a leading newline for proper formatting
        if matches!(value, Value::Sequence(_)) {
            writeln!(writer)?;
        }

        // Emit the value
        self.emit_value(value, &mut writer)?;
        writeln!(writer)?; // Add final newline
        Ok(())
    }

    fn emit_commented<W: Write>(&mut self, value: &CommentedValue, mut writer: W) -> Result<()> {
        // Reset state
        self.current_indent = 0;
        self.shared_values.clear();
        self.anchor_counter = 0;

        // Emit directives if any
        self.emit_directives(&mut writer)?;

        // Analyze for shared values first
        self.analyze_shared_values(&value.value);

        // Emit the commented value
        self.emit_commented_value(value, &mut writer)?;
        writeln!(writer)?; // Add final newline
        Ok(())
    }

    fn emit_with_style<W: Write>(
        &mut self,
        value: &CommentedValue,
        indent_style: &IndentStyle,
        mut writer: W,
    ) -> Result<()> {
        // Store current style and temporarily update
        let original_style = self.indent_style.clone();
        self.set_indent_style(indent_style.clone());

        // Reset state
        self.current_indent = 0;
        self.shared_values.clear();
        self.anchor_counter = 0;

        // Emit directives if any
        self.emit_directives(&mut writer)?;

        // Analyze for shared values first
        self.analyze_shared_values(&value.value);

        // Emit the commented value with the specified style
        let result = self.emit_commented_value(value, &mut writer);
        if result.is_ok() {
            writeln!(writer)?; // Add final newline
        }

        // Restore original style
        self.set_indent_style(original_style);

        result
    }

    fn reset(&mut self) {
        self.current_indent = 0;
        self.shared_values.clear();
        self.anchor_counter = 0;
        // Note: We don't reset directives here as they might need to persist
    }
}

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

    #[test]
    fn test_emit_scalar() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        emitter.emit(&Value::Int(42), &mut output).unwrap();
        assert_eq!(String::from_utf8(output).unwrap(), "42\n");
    }

    #[test]
    fn test_emit_string() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        emitter
            .emit(&Value::String("hello".to_string()), &mut output)
            .unwrap();
        assert_eq!(String::from_utf8(output).unwrap(), "hello\n");
    }

    #[test]
    fn test_emit_quoted_string() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        emitter
            .emit(&Value::String("true".to_string()), &mut output)
            .unwrap();
        assert_eq!(String::from_utf8(output).unwrap(), "\"true\"\n");
    }

    #[test]
    fn test_emit_sequence() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        let seq = Value::Sequence(vec![Value::Int(1), Value::Int(2), Value::Int(3)]);

        emitter.emit(&seq, &mut output).unwrap();
        let result = String::from_utf8(output).unwrap();
        let expected = "\n- 1\n- 2\n- 3\n";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_emit_mapping() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        let mut map = IndexMap::new();
        map.insert(
            Value::String("key".to_string()),
            Value::String("value".to_string()),
        );
        map.insert(Value::String("number".to_string()), Value::Int(42));

        emitter.emit(&Value::Mapping(map), &mut output).unwrap();
        let result = String::from_utf8(output).unwrap();

        // Should contain the key-value pairs
        assert!(result.contains("key: value"));
        assert!(result.contains("number: 42"));
    }

    #[test]
    fn test_emit_nested_structure() {
        let mut emitter = BasicEmitter::new();
        let mut output = Vec::new();

        let inner_seq = Value::Sequence(vec![Value::Int(1), Value::Int(2)]);
        let mut outer_map = IndexMap::new();
        outer_map.insert(Value::String("items".to_string()), inner_seq);

        emitter
            .emit(&Value::Mapping(outer_map), &mut output)
            .unwrap();
        let result = String::from_utf8(output).unwrap();

        assert!(result.contains("items:"));
        assert!(result.contains("- 1"));
        assert!(result.contains("- 2"));
    }
}