ayaml 0.1.3

A safe, pure-Rust YAML parser, event stream, value model, Serde layer, and emitter
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
//! Event-stream YAML emission.
//!
//! [`to_string_events`] and [`to_writer_event_stream`] share one writer. It
//! keeps block collections in block layout and flow collections in flow
//! layout. It also keeps tags, anchors, aliases, directives, explicit
//! document markers, and each scalar's style, as long as that style can hold
//! the value where the scalar appears. Otherwise the scalar is written
//! double-quoted, which can hold any value. A plain scalar only falls back to
//! quotes when its text resolves to a string, so quoting never changes a
//! value's type. The writer reads one event ahead, and holds each document's
//! text until the document ends, so that the `%TAG` directives that some tags
//! need (see [`Tag`]) come before it. The lazy entry point therefore needs
//! memory for the nesting depth and for one document's text, which
//! `max_output_bytes` bounds.

use std::borrow::Borrow;
use std::io::Write;
use std::marker::PhantomData;

use crate::EventStream;
use crate::emitter::{
    MAX_IMPLICIT_KEY_CHARS, Output, RedefinedHandles, TagDirectives, block_scalar_ok, can_fold,
    emit_double_quoted, expand_folded, is_printable, looks_like_leading_zero_numeral,
    needs_explicit_indent, plain_text_ok, single_quoted_ok,
};
use crate::error::{Error, ErrorKind};
use crate::event::{Anchor, CollectionStyle, Event, ScalarStyle};
use crate::limits::Limits;
use crate::tag::Tag;

/// Indentation step of the event writer.
const STEP: usize = 2;

/// Emit a collected event stream as YAML text.
///
/// # Errors
///
/// Returns an error when the events cannot be written, a configured limit is exceeded, or the writer fails.
pub fn to_string_events(events: &[Event<'_>]) -> Result<String, Error> {
    to_string_events_with_limits(events, &Limits::default())
}

/// Emit a collected event stream with an explicit output-size limit.
///
/// # Errors
///
/// Returns an error when the events cannot be written, a configured limit is exceeded, or the writer fails.
pub fn to_string_events_with_limits(
    events: &[Event<'_>],
    limits: &Limits,
) -> Result<String, Error> {
    let mut output = Vec::new();
    write_events(
        Output::direct(&mut output, limits),
        events.iter().map(Ok::<_, Error>),
        limits,
    )?;
    String::from_utf8(output).map_err(|error| {
        Error::new(ErrorKind::Emitter, "emitter produced invalid UTF-8").with_source(error)
    })
}

/// Write a collected event stream to a writer.
///
/// # Errors
///
/// Returns an error when the events cannot be written, a configured limit is exceeded, or the writer fails.
pub fn to_writer_events<W: Write>(writer: &mut W, events: &[Event<'_>]) -> Result<(), Error> {
    let limits = Limits::default();
    write_events(
        Output::new(writer, &limits),
        events.iter().map(Ok::<_, Error>),
        &limits,
    )
}

/// Write a lazy event stream as it is parsed.
///
/// The output is the same as [`to_string_events`] would produce for the
/// collected events. It holds one document's text at a time, since the
/// document's `%TAG` directives come first, and otherwise needs memory for the
/// nesting depth.
///
/// # Errors
///
/// Returns an error when the events cannot be written, a configured limit is exceeded, or the writer fails.
pub fn to_writer_event_stream<W: Write>(
    writer: &mut W,
    stream: EventStream<'_>,
) -> Result<(), Error> {
    to_writer_event_stream_with_limits(writer, stream, &Limits::default())
}

/// Write a lazy event stream with a cumulative output limit.
///
/// # Errors
///
/// Returns an error when the events cannot be written, a configured limit is exceeded, or the writer fails.
pub fn to_writer_event_stream_with_limits<W: Write>(
    writer: &mut W,
    stream: EventStream<'_>,
    limits: &Limits,
) -> Result<(), Error> {
    write_events(Output::new(writer, limits), stream, limits)
}

fn emitter_error(message: impl Into<String>) -> Error {
    Error::new(ErrorKind::Emitter, message)
}

fn write_events<'e, W, I, B>(
    mut output: Output<'_, W>,
    events: I,
    limits: &Limits,
) -> Result<(), Error>
where
    W: Write,
    I: Iterator<Item = Result<B, Error>>,
    B: Borrow<Event<'e>>,
{
    limits.validate()?;
    let mut emitter = EventWriter {
        output: &mut output,
        events,
        pending: None,
        limits,
        depth: 0,
        handles: RedefinedHandles::default(),
        directives: TagDirectives::default(),
        declared: Vec::new(),
        marker: PhantomData,
    };
    emitter.run()?;
    output.finish()
}

/// Where a block-context node is written.
#[derive(Clone, Copy)]
enum Pos {
    /// A document's root, at the start of a line.
    Root,
    /// After `-`, `?`, or an explicit-value `:` that belongs to a block
    /// collection at this column. A block collection can start on the same
    /// line (a compact entry).
    Entry(usize),
    /// After `key:` in a block mapping at this column.
    Value(usize),
}

/// An open collection in the writer; the innermost is on top of the stack.
#[derive(Clone, Copy)]
enum Frame {
    /// A block sequence at this column. With `inline`, its next entry
    /// continues the current line (a compact entry).
    BlockSequence {
        indent: usize,
        inline: bool,
    },
    /// A block mapping at this column, before its next key.
    BlockMapping {
        indent: usize,
        inline: bool,
    },
    /// After `? key` in a block mapping at this column: the `:` line and
    /// the value.
    BlockValue {
        indent: usize,
    },
    FlowSequence {
        first: bool,
    },
    FlowMapping {
        first: bool,
    },
    /// After a flow-mapping key that is a collection: `: ` and the value.
    FlowValue,
    /// After a flow collection written in block context: its line break.
    LineEnd,
}

impl Pos {
    fn content_indent(self) -> usize {
        match self {
            Self::Root => STEP,
            Self::Entry(indent) | Self::Value(indent) => indent + STEP,
        }
    }
}

fn is_empty_plain(event: &Event<'_>) -> bool {
    matches!(
        event,
        Event::Scalar {
            value,
            style: ScalarStyle::Plain,
            tag: None,
            anchor: None,
            ..
        } if value.is_empty()
    )
}

/// Whether an anchor name reads back unchanged: non-empty, printable, and
/// free of white space, line breaks, and flow indicators.
fn anchor_name_ok(name: &str) -> bool {
    !name.is_empty()
        && name.chars().all(|ch| {
            is_printable(ch)
                && !matches!(
                    ch,
                    ' ' | '\t' | '\n' | '\r' | '\u{feff}' | ',' | '[' | ']' | '{' | '}'
                )
        })
}

/// Append a scalar on one line: plain or single-quoted when the text allows
/// that style in this context, double-quoted otherwise.
fn scalar_inline(
    value: &str,
    style: ScalarStyle,
    flow: bool,
    tag: Option<&Tag>,
    output: &mut String,
) {
    let plain =
        plain_text_ok(value, flow) && (tag.is_some() || !looks_like_leading_zero_numeral(value));
    match style {
        ScalarStyle::Plain if plain => output.push_str(value),
        ScalarStyle::SingleQuoted if single_quoted_ok(value) => {
            output.push('\'');
            for (index, part) in value.split('\'').enumerate() {
                if index > 0 {
                    output.push_str("''");
                }
                output.push_str(part);
            }
            output.push('\'');
        }
        _ => emit_double_quoted(value, output),
    }
}

struct EventWriter<'o, 'w, 'l, 'e, W: Write, I, B> {
    output: &'o mut Output<'w, W>,
    events: I,
    pending: Option<B>,
    limits: &'l Limits,
    depth: usize,
    handles: RedefinedHandles,
    /// The `%TAG` directives added to the document being written (D82).
    directives: TagDirectives,
    /// The named handles that the next document's own `%TAG` directives declare.
    declared: Vec<String>,
    marker: PhantomData<Event<'e>>,
}

impl<'e, W, I, B> EventWriter<'_, '_, '_, 'e, W, I, B>
where
    W: Write,
    I: Iterator<Item = Result<B, Error>>,
    B: Borrow<Event<'e>>,
{
    fn next(&mut self) -> Result<Option<B>, Error> {
        if let Some(event) = self.pending.take() {
            return Ok(Some(event));
        }
        self.events.next().transpose()
    }

    fn peek(&mut self) -> Result<Option<&Event<'e>>, Error> {
        if self.pending.is_none() {
            self.pending = self.events.next().transpose()?;
        }
        Ok(self.pending.as_ref().map(Borrow::borrow))
    }

    fn node_event(&mut self) -> Result<B, Error> {
        self.next()?
            .ok_or_else(|| emitter_error("unexpected end of event stream"))
    }

    fn push(&mut self, value: &str) -> Result<(), Error> {
        self.output.push_str(value)
    }

    fn enter(&mut self) -> Result<(), Error> {
        self.depth += 1;
        self.limits.check_depth(self.depth).map_err(Error::from)
    }

    fn run(&mut self) -> Result<(), Error> {
        let mut documents = 0_usize;
        let mut open_ended = false;
        let mut directives = false;
        while let Some(event) = self.next()? {
            match event.borrow() {
                Event::StreamStart { .. } | Event::StreamEnd { .. } => {}
                Event::Directive { name, value, .. } => {
                    // The parser reads a name up to a space or tab, and
                    // neither may hold a line break or a byte order mark.
                    // NEL, LS, and PS are content; a directive has no
                    // escapes, so they are written as they are.
                    let directive_char =
                        |ch: char| is_printable(ch) && !matches!(ch, '\n' | '\r' | '\u{feff}');
                    if name.is_empty()
                        || !name
                            .chars()
                            .all(|ch| directive_char(ch) && !matches!(ch, ' ' | '\t'))
                        || !value.chars().all(directive_char)
                    {
                        return Err(emitter_error(format!(
                            "cannot write the directive %{name} {value}"
                        )));
                    }
                    // A directive cannot follow a document left open.
                    if open_ended {
                        self.push("...\n")?;
                        open_ended = false;
                    }
                    self.push("%")?;
                    self.push(name)?;
                    self.push(" ")?;
                    self.push(value)?;
                    self.push("\n")?;
                    if name == "TAG" {
                        match value.split([' ', '\t']).find(|part| !part.is_empty()) {
                            Some("!") => self.handles.primary = true,
                            Some("!!") => self.handles.secondary = true,
                            Some(handle) => self.declared.push(handle.to_owned()),
                            None => {}
                        }
                    }
                    directives = true;
                }
                Event::DocumentStart { explicit, .. } => {
                    let empty_root = match self.peek()? {
                        Some(Event::DocumentEnd { .. }) | None => {
                            return Err(emitter_error("document has no root node"));
                        }
                        Some(root) => is_empty_plain(root),
                    };
                    // The directives a tag needs are known only once the document is written,
                    // so it is held until then, and they go in before it.
                    self.directives = TagDirectives::declaring(std::mem::take(&mut self.declared));
                    let held = self.output.hold();
                    // An empty root needs `---`, or the document would vanish.
                    let marker = *explicit || documents > 0 || directives || empty_root;
                    if marker {
                        self.push("---\n")?;
                    }
                    let root = self.node_event()?;
                    self.node(root, Pos::Root)?;
                    if !self.directives.is_empty() {
                        let mut header = String::new();
                        // A directive can't follow a document that `...` didn't end. The
                        // document's own directives wrote one already, and cleared `open_ended`.
                        if open_ended {
                            header.push_str("...\n");
                        }
                        header.push_str(&self.directives.lines());
                        if !marker {
                            header.push_str("---\n");
                        }
                        self.output.insert_held(held, &header)?;
                    }
                    self.output.release()?;
                    let end = self.next()?;
                    match end.as_ref().map(Borrow::borrow) {
                        Some(Event::DocumentEnd { explicit, .. }) => {
                            open_ended = !*explicit;
                            if *explicit {
                                self.push("...\n")?;
                            }
                        }
                        _ => return Err(emitter_error("document has no end event")),
                    }
                    documents += 1;
                    directives = false;
                    self.handles = RedefinedHandles::default();
                }
                _ => return Err(emitter_error("unexpected event outside a document")),
            }
        }
        if directives {
            return Err(emitter_error("directives are not followed by a document"));
        }
        Ok(())
    }

    /// `&anchor !tag`, or an empty string.
    fn properties(
        &mut self,
        anchor: Option<&Anchor<'_>>,
        tag: Option<&Tag>,
    ) -> Result<String, Error> {
        let mut output = String::new();
        if let Some(anchor) = anchor {
            if !anchor_name_ok(anchor.as_str()) {
                return Err(emitter_error(format!(
                    "cannot write the anchor name {:?}",
                    anchor.as_str()
                )));
            }
            output.push('&');
            output.push_str(anchor.as_str());
        }
        if let Some(tag) = tag {
            if !output.is_empty() {
                output.push(' ');
            }
            output.push_str(&self.directives.token(tag, self.handles)?);
        }
        Ok(output)
    }

    fn alias(&mut self, anchor: &Anchor<'_>) -> Result<(), Error> {
        if !anchor_name_ok(anchor.as_str()) {
            return Err(emitter_error(format!(
                "cannot write the alias name {:?}",
                anchor.as_str()
            )));
        }
        self.push("*")?;
        self.push(anchor.as_str())
    }

    fn separate(&mut self, pos: Pos) -> Result<(), Error> {
        if matches!(pos, Pos::Root) {
            Ok(())
        } else {
            self.push(" ")
        }
    }

    /// Write a node in block context, ending with a line break. Open
    /// collections are kept on an explicit stack, so nesting costs heap, not
    /// native stack.
    fn node(&mut self, event: B, pos: Pos) -> Result<(), Error> {
        let mut stack = Vec::new();
        self.block_node(event, pos, &mut stack)?;
        while let Some(frame) = stack.pop() {
            self.resume(frame, &mut stack)?;
        }
        Ok(())
    }

    /// Start a node in block context: write a scalar or an alias, or open a
    /// collection on `stack`.
    fn block_node(&mut self, event: B, pos: Pos, stack: &mut Vec<Frame>) -> Result<(), Error> {
        match event.borrow() {
            Event::Alias { anchor, .. } => {
                self.separate(pos)?;
                self.alias(anchor)?;
                self.push("\n")
            }
            Event::Scalar {
                value,
                style,
                tag,
                anchor,
                ..
            } => self.scalar_node(value, *style, tag.as_ref(), anchor.as_ref(), pos),
            Event::SequenceStart {
                tag, anchor, style, ..
            }
            | Event::MappingStart {
                tag, anchor, style, ..
            } => {
                let sequence = matches!(event.borrow(), Event::SequenceStart { .. });
                let empty = match self.peek()? {
                    Some(Event::SequenceEnd { .. }) => sequence,
                    Some(Event::MappingEnd { .. }) => !sequence,
                    _ => false,
                };
                if *style == CollectionStyle::Flow || empty {
                    self.separate(pos)?;
                    stack.push(Frame::LineEnd);
                    self.flow_collection(sequence, tag.as_ref(), anchor.as_ref(), stack)
                } else {
                    self.block_collection(sequence, tag.as_ref(), anchor.as_ref(), pos, stack)
                }
            }
            _ => Err(emitter_error("unexpected event in node position")),
        }
    }

    fn scalar_node(
        &mut self,
        value: &str,
        style: ScalarStyle,
        tag: Option<&Tag>,
        anchor: Option<&Anchor<'_>>,
        pos: Pos,
    ) -> Result<(), Error> {
        let mut text = self.properties(anchor, tag)?;
        if matches!(style, ScalarStyle::Literal | ScalarStyle::Folded) && block_scalar_ok(value) {
            self.separate(pos)?;
            if !text.is_empty() {
                text.push(' ');
                self.push(&text)?;
            }
            return self.block_scalar(value, style, pos.content_indent(), matches!(pos, Pos::Root));
        }
        if style == ScalarStyle::Plain && value.is_empty() {
            if text.is_empty() {
                // The root of a document that is only `---`.
                return if matches!(pos, Pos::Root) {
                    Ok(())
                } else {
                    self.push("\n")
                };
            }
            self.separate(pos)?;
            self.push(&text)?;
            return self.push("\n");
        }
        self.separate(pos)?;
        if !text.is_empty() {
            text.push(' ');
        }
        scalar_inline(value, style, false, tag, &mut text);
        text.push('\n');
        self.push(&text)
    }

    #[allow(
        clippy::cast_possible_truncation,
        reason = "the indentation indicator is a single digit in 1..=9"
    )]
    fn block_scalar(
        &mut self,
        value: &str,
        style: ScalarStyle,
        content_indent: usize,
        root: bool,
    ) -> Result<(), Error> {
        let content_indent = if root {
            content_indent.min(8)
        } else {
            content_indent
        };
        let trailing = value.chars().rev().take_while(|ch| *ch == '\n').count();
        let body = &value[..value.len() - trailing];
        let folded = style == ScalarStyle::Folded && can_fold(body);
        let mut header = String::from(if folded { ">" } else { "|" });
        if needs_explicit_indent(value) {
            // Content sits one step below the parent node; a root node's indentation is -1, so
            // a root scalar's indicator is one more than its content indentation.
            let indicator = if root { content_indent + 1 } else { STEP };
            header.push(char::from_digit(indicator as u32, 10).unwrap_or('0'));
        }
        if trailing == 0 {
            header.push('-');
        } else if trailing > 1 || body.is_empty() {
            header.push('+');
        }
        header.push('\n');
        self.push(&header)?;
        if body.is_empty() {
            for _ in 0..trailing {
                self.push("\n")?;
            }
            return Ok(());
        }
        let folded_body = folded.then(|| expand_folded(body));
        for line in folded_body.as_deref().unwrap_or(body).split('\n') {
            if !line.is_empty() {
                self.output.spaces(content_indent)?;
                self.push(line)?;
            }
            self.push("\n")?;
        }
        for _ in 1..trailing {
            self.push("\n")?;
        }
        Ok(())
    }

    /// Write a block collection's properties and open it on `stack`.
    fn block_collection(
        &mut self,
        sequence: bool,
        tag: Option<&Tag>,
        anchor: Option<&Anchor<'_>>,
        pos: Pos,
        stack: &mut Vec<Frame>,
    ) -> Result<(), Error> {
        let properties = self.properties(anchor, tag)?;
        let (indent, inline) = match pos {
            Pos::Root => {
                if !properties.is_empty() {
                    self.push(&properties)?;
                    self.push("\n")?;
                }
                (0, false)
            }
            Pos::Entry(indent) if properties.is_empty() => {
                self.push(" ")?;
                (indent + STEP, true)
            }
            Pos::Entry(indent) => {
                self.push(" ")?;
                self.push(&properties)?;
                self.push("\n")?;
                (indent + STEP, false)
            }
            Pos::Value(indent) => {
                if !properties.is_empty() {
                    self.push(" ")?;
                    self.push(&properties)?;
                }
                self.push("\n")?;
                // Sequences under a mapping key are indentless, as libyaml
                // writes them.
                (if sequence { indent } else { indent + STEP }, false)
            }
        };
        self.enter()?;
        stack.push(if sequence {
            Frame::BlockSequence { indent, inline }
        } else {
            Frame::BlockMapping { indent, inline }
        });
        Ok(())
    }

    /// Take the next step of the innermost open collection: write its next
    /// entry, opening any collection the entry starts above it, or close it.
    fn resume(&mut self, frame: Frame, stack: &mut Vec<Frame>) -> Result<(), Error> {
        match frame {
            Frame::BlockSequence { indent, inline } => {
                let event = self
                    .next()?
                    .ok_or_else(|| emitter_error("unterminated sequence"))?;
                if matches!(event.borrow(), Event::SequenceEnd { .. }) {
                    self.depth -= 1;
                    return Ok(());
                }
                if !inline {
                    self.output.spaces(indent)?;
                }
                self.push("-")?;
                stack.push(Frame::BlockSequence {
                    indent,
                    inline: false,
                });
                self.block_node(event, Pos::Entry(indent), stack)
            }
            Frame::BlockMapping { indent, inline } => {
                let key = self
                    .next()?
                    .ok_or_else(|| emitter_error("unterminated mapping"))?;
                if matches!(key.borrow(), Event::MappingEnd { .. }) {
                    self.depth -= 1;
                    return Ok(());
                }
                if !inline {
                    self.output.spaces(indent)?;
                }
                stack.push(Frame::BlockMapping {
                    indent,
                    inline: false,
                });
                if let Some(mut text) = self.implicit_key(key.borrow())? {
                    text.push(':');
                    self.push(&text)?;
                    let value = self.node_event()?;
                    self.block_node(value, Pos::Value(indent), stack)
                } else {
                    self.push("?")?;
                    stack.push(Frame::BlockValue { indent });
                    self.block_node(key, Pos::Entry(indent), stack)
                }
            }
            Frame::BlockValue { indent } => {
                self.output.spaces(indent)?;
                self.push(":")?;
                let value = self.node_event()?;
                self.block_node(value, Pos::Entry(indent), stack)
            }
            Frame::FlowSequence { first } => {
                let event = self
                    .next()?
                    .ok_or_else(|| emitter_error("unterminated sequence"))?;
                if matches!(event.borrow(), Event::SequenceEnd { .. }) {
                    return self.close_flow("]");
                }
                if !first {
                    self.push(", ")?;
                }
                if is_empty_plain(event.borrow()) {
                    return Err(emitter_error(
                        "an empty plain scalar without a tag or anchor cannot be a flow sequence entry",
                    ));
                }
                stack.push(Frame::FlowSequence { first: false });
                self.flow_node(event, stack).map(drop)
            }
            Frame::FlowMapping { first } => {
                let key = self
                    .next()?
                    .ok_or_else(|| emitter_error("unterminated mapping"))?;
                if matches!(key.borrow(), Event::MappingEnd { .. }) {
                    return self.close_flow("}");
                }
                if !first {
                    self.push(", ")?;
                }
                if self.flow_key_is_explicit(key.borrow())? {
                    self.push("? ")?;
                }
                stack.push(Frame::FlowMapping { first: false });
                if matches!(
                    key.borrow(),
                    Event::SequenceStart { .. } | Event::MappingStart { .. }
                ) {
                    // The `: ` follows once the key collection closes.
                    stack.push(Frame::FlowValue);
                    return self.flow_node(key, stack).map(drop);
                }
                if self.flow_node(key, stack)? {
                    self.push(" ")?;
                }
                self.flow_value(stack)
            }
            Frame::FlowValue => self.flow_value(stack),
            Frame::LineEnd => self.push("\n"),
        }
    }

    /// Write a flow mapping's `: ` and start its value.
    fn flow_value(&mut self, stack: &mut Vec<Frame>) -> Result<(), Error> {
        self.push(": ")?;
        let value = self.node_event()?;
        self.flow_node(value, stack).map(drop)
    }

    fn close_flow(&mut self, close: &str) -> Result<(), Error> {
        self.push(close)?;
        self.depth -= 1;
        Ok(())
    }

    /// The text of a block-mapping key written in the implicit form, or
    /// `None` when the key needs `? `: collections, empty keys, block
    /// scalars, and keys longer than [`MAX_IMPLICIT_KEY_CHARS`].
    fn implicit_key(&mut self, key: &Event<'_>) -> Result<Option<String>, Error> {
        match key {
            Event::Alias { anchor, .. } => {
                if !anchor_name_ok(anchor.as_str()) {
                    return Err(emitter_error(format!(
                        "cannot write the alias name {:?}",
                        anchor.as_str()
                    )));
                }
                // `:` can be part of an alias name, so it needs a space.
                Ok(Some(format!("*{} ", anchor.as_str())))
            }
            Event::Scalar {
                value,
                style,
                tag,
                anchor,
                ..
            } => {
                let block = matches!(style, ScalarStyle::Literal | ScalarStyle::Folded)
                    && block_scalar_ok(value);
                if block || (*style == ScalarStyle::Plain && value.is_empty()) {
                    return Ok(None);
                }
                let mut text = self.properties(anchor.as_ref(), tag.as_ref())?;
                if !text.is_empty() {
                    text.push(' ');
                }
                scalar_inline(value, *style, false, tag.as_ref(), &mut text);
                Ok((text.chars().count() <= MAX_IMPLICIT_KEY_CHARS).then_some(text))
            }
            _ => Ok(None),
        }
    }

    /// Write a flow collection's properties and opening bracket, and open it
    /// on `stack`; its start event is consumed.
    fn flow_collection(
        &mut self,
        sequence: bool,
        tag: Option<&Tag>,
        anchor: Option<&Anchor<'_>>,
        stack: &mut Vec<Frame>,
    ) -> Result<(), Error> {
        let mut properties = self.properties(anchor, tag)?;
        if !properties.is_empty() {
            properties.push(' ');
            self.push(&properties)?;
        }
        self.enter()?;
        self.push(if sequence { "[" } else { "{" })?;
        stack.push(if sequence {
            Frame::FlowSequence { first: true }
        } else {
            Frame::FlowMapping { first: true }
        });
        Ok(())
    }

    /// Whether a flow-mapping key needs `? `, as libyaml writes it: an empty
    /// key, a non-empty collection, or a long scalar.
    fn flow_key_is_explicit(&mut self, key: &Event<'_>) -> Result<bool, Error> {
        Ok(match key {
            Event::SequenceStart { .. } => !matches!(self.peek()?, Some(Event::SequenceEnd { .. })),
            Event::MappingStart { .. } => !matches!(self.peek()?, Some(Event::MappingEnd { .. })),
            Event::Scalar {
                value,
                style,
                tag,
                anchor,
                ..
            } => {
                if is_empty_plain(key) {
                    true
                } else {
                    let mut text = self.properties(anchor.as_ref(), tag.as_ref())?;
                    scalar_inline(value, *style, true, tag.as_ref(), &mut text);
                    text.chars().count() > MAX_IMPLICIT_KEY_CHARS
                }
            }
            _ => false,
        })
    }

    /// Start a node inside a flow collection: write a scalar or an alias, or
    /// open a collection on `stack`. Returns true when a following `:` must
    /// be separated by a space.
    fn flow_node(&mut self, event: B, stack: &mut Vec<Frame>) -> Result<bool, Error> {
        match event.borrow() {
            Event::Alias { anchor, .. } => {
                self.alias(anchor)?;
                Ok(true)
            }
            Event::Scalar {
                value,
                style,
                tag,
                anchor,
                ..
            } => {
                let mut text = self.properties(anchor.as_ref(), tag.as_ref())?;
                if !text.is_empty() {
                    // `[!t ]`: libyaml needs a space between properties and
                    // a flow indicator.
                    text.push(' ');
                }
                if !(*style == ScalarStyle::Plain && value.is_empty()) {
                    scalar_inline(value, *style, true, tag.as_ref(), &mut text);
                }
                self.push(&text)?;
                Ok(false)
            }
            Event::SequenceStart { tag, anchor, .. } => {
                self.flow_collection(true, tag.as_ref(), anchor.as_ref(), stack)?;
                Ok(false)
            }
            Event::MappingStart { tag, anchor, .. } => {
                self.flow_collection(false, tag.as_ref(), anchor.as_ref(), stack)?;
                Ok(false)
            }
            _ => Err(emitter_error("unexpected event in node position")),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::Event;

    #[test]
    fn event_emission_preserves_directives() {
        let input = "%YAML 1.2\n---\na: [1, 2]\n";
        let events: Vec<Event<'_>> = crate::parse_events(input)
            .expect("events")
            .collect::<Result<Vec<_>, _>>()
            .expect("items");
        let text = crate::to_string_events(&events).expect("emit");
        assert!(text.contains("%YAML 1.2"));
        assert!(text.contains("---"));
        let value = crate::parse_value(&text).expect("reparse");
        let original = crate::parse_value(input).expect("original");
        assert_eq!(value, original);
    }

    #[test]
    fn event_emission_preserves_simple_folded_style() {
        let input = "folded: >\n  a b\n";
        let events: Vec<Event<'_>> = crate::parse_events(input)
            .expect("events")
            .collect::<Result<Vec<_>, _>>()
            .expect("items");
        let text = crate::to_string_events(&events).expect("emit");
        assert!(text.contains("folded: >"), "{text:?}");
        let value = crate::parse_value(&text).expect("reparse");
        let original = crate::parse_value(input).expect("original");
        assert_eq!(value, original);
    }

    #[test]
    fn event_emission_preserves_block_scalar_values() {
        let input = "literal: |\n  line1\n  line2\nfolded: >\n  a b\n";
        let events: Vec<Event<'_>> = crate::parse_events(input)
            .expect("events")
            .collect::<Result<Vec<_>, _>>()
            .expect("items");
        let text = crate::to_string_events(&events).expect("emit");
        assert!(text.contains("literal: |"));
        let value = crate::parse_value(&text).expect("reparse");
        let original = crate::parse_value(input).expect("original");
        assert_eq!(value, original);
    }

    #[test]
    fn event_emission_preserves_tags_anchors_aliases_and_scalar_styles() {
        let input = "base: &b {x: \"quoted\"}\ncopy: *b\ntagged: !Custom value\n";
        let events: Vec<Event<'_>> = crate::parse_events(input)
            .expect("events")
            .collect::<Result<Vec<_>, _>>()
            .expect("items");
        let text = crate::to_string_events(&events).expect("emit");
        assert!(text.contains("base:"));
        assert!(text.contains("&b"));
        assert!(text.contains("*b"));
        assert!(text.contains("!Custom"));
        let value = crate::parse_value(&text).expect("reparse");
        let original = crate::parse_value(input).expect("original");
        assert_eq!(value, original);
    }
}

#[cfg(test)]
mod writer_tests {
    use crate::Event;

    #[test]
    fn event_writer_round_trips() {
        let events: Vec<Event<'_>> = crate::parse_events("a: [1, 2]\n")
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        let mut output = Vec::new();
        crate::to_writer_events(&mut output, &events).expect("write");
        let text = std::str::from_utf8(&output).expect("utf8");
        assert_eq!(crate::parse_value(text).unwrap()["a"][1].as_u64(), Some(2));
    }
}

#[cfg(test)]
mod stream_writer_tests {
    #[test]
    fn lazy_event_writer_enforces_cumulative_output_limit() {
        let limits = crate::Limits {
            max_documents: 2,
            max_output_bytes: 10,
            ..crate::Limits::default()
        };
        let stream =
            crate::parse_events_with_limits("---\na: 1\n---\nb: 2\n", &limits).expect("stream");
        let mut output = Vec::new();
        let error = crate::to_writer_event_stream_with_limits(&mut output, stream, &limits)
            .expect_err("cumulative limit");
        assert_eq!(error.kind(), crate::ErrorKind::Limit);
    }

    #[test]
    fn lazy_event_writer_emits_multiple_documents() {
        let limits = crate::Limits {
            max_documents: 2,
            ..crate::Limits::default()
        };
        let stream =
            crate::parse_events_with_limits("---\na: 1\n---\nb: 2\n", &limits).expect("stream");
        let mut output = Vec::new();
        crate::to_writer_event_stream_with_limits(&mut output, stream, &limits).expect("write");
        let text = std::str::from_utf8(&output).expect("utf8");
        assert!(text.contains("a: 1"), "{text:?}");
        let documents = crate::parse_documents_with_limits(text, &limits).expect("reparse");
        assert_eq!(documents.len(), 2);
        assert_eq!(documents[1]["b"].as_u64(), Some(2));
    }
}

#[cfg(test)]
mod stream_block_scalar_tests {
    #[test]
    fn per_event_writer_preserves_folded_blank_lines() {
        let input = "folded: >\n  a\n\n  b\n";
        let limits = crate::Limits::default();
        let stream = crate::parse_events_with_limits(input, &limits).expect("stream");
        let mut output = Vec::new();
        crate::to_writer_event_stream(&mut output, stream).expect("write");
        let text = std::str::from_utf8(&output).expect("utf8");
        assert!(text.contains("folded: >"), "{text:?}");
        assert_eq!(
            crate::parse_value(text).expect("reparse"),
            crate::parse_value(input).expect("original")
        );
    }

    #[test]
    fn per_event_writer_emits_block_scalars() {
        let limits = crate::Limits::default();
        let stream = crate::parse_events_with_limits(
            "literal: |\n  one\n  two\nfolded: >\n  a b\n",
            &limits,
        )
        .expect("stream");
        let mut output = Vec::new();
        crate::to_writer_event_stream(&mut output, stream).expect("write");
        let text = std::str::from_utf8(&output).expect("utf8");
        assert!(text.contains("literal: |"), "{text:?}");
        assert!(text.contains("folded: >"), "{text:?}");
        let decoded = crate::parse_value(text).expect("reparse");
        let original =
            crate::parse_value("literal: |\n  one\n  two\nfolded: >\n  a b\n").expect("original");
        assert_eq!(decoded, original);
    }
}