rlsp-yaml-parser 0.11.1

Spec-faithful streaming YAML 1.2 parser
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
// SPDX-License-Identifier: MIT

//! High-level parse events produced by the streaming parser.
//!
//! The public entry point is [`crate::parse_events`], which returns an
//! iterator of <code>Result<([Event], [crate::pos::Span]), [crate::error::Error]></code>.
//!
//! Each event carries a [`crate::pos::Span`] covering the input bytes that
//! contributed to it.  For zero-width synthetic events (e.g. `StreamStart`
//! at the very beginning of input), the span has equal `start` and `end`.
//!

use std::borrow::Cow;

use crate::pos::Span;

/// Rare per-event fields for node-typed events (`Scalar`, `SequenceStart`, `MappingStart`).
///
/// Bundled behind `Option<Box<EventMeta>>` so that the common case — no anchor, no
/// source-text tag — pays only one 8-byte pointer instead of ~96 bytes of inline storage.
/// Events with tags and anchors are rare in block-heavy and Kubernetes YAML; boxing them
/// moves the cost to the uncommon path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventMeta<'input> {
    /// The anchor name, if any (e.g. `&foo`).
    pub anchor: Option<&'input str>,
    /// Source span of the `&name` anchor token — from `&` through the last byte of the name.
    /// `Some` when `anchor` is `Some`, `None` otherwise.
    pub anchor_loc: Option<Span>,
    /// The resolved tag, if any (e.g. `"tag:yaml.org,2002:str"` for `!!str`).
    ///
    /// Verbatim tags (`!<URI>`) borrow from input.  Shorthand tags resolved via `%TAG`
    /// directives or the built-in `!!` default produce owned strings.
    pub tag: Option<Cow<'input, str>>,
    /// Source span of the tag token — from `!` through the last byte of the tag token.
    /// `Some` when `tag` is `Some`, `None` otherwise.
    pub tag_loc: Option<Span>,
}

/// Block scalar chomping mode per YAML 1.2 §8.1.1.2.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Chomp {
    /// `-` — trailing newlines stripped.
    Strip,
    /// (default, no indicator) — single trailing newline kept.
    Clip,
    /// `+` — all trailing newlines kept.
    Keep,
}

/// The style (block or flow) of a collection (sequence or mapping).
///
/// Currently only `Block` is produced; `Flow` will be used when flow sequences
/// (`[a, b]`) and flow mappings (`{a: b}`) are implemented in Task 14.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollectionStyle {
    /// A block-style collection using indentation and `-`/`:` indicators.
    Block,
    /// A flow-style collection using `[]` or `{}` delimiters (Task 14).
    Flow,
}

/// The style in which a scalar value was written in the source.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScalarStyle {
    /// An unquoted plain scalar (YAML 1.2 §7.3.3).
    Plain,
    /// A `'single-quoted'` scalar (YAML 1.2 §7.3.2).
    SingleQuoted,
    /// A `"double-quoted"` scalar (YAML 1.2 §7.3.1).
    DoubleQuoted,
    /// A `|` literal block scalar (YAML 1.2 §8.1.2).
    Literal(Chomp),
    /// A `>` folded block scalar (YAML 1.2 §8.1.3).
    ///
    /// Line folding is applied to the collected content: a single line break
    /// between two equally-indented non-blank lines becomes a space; N blank
    /// lines between non-blank lines produce N newlines; more-indented lines
    /// preserve their relative leading whitespace and the line break before
    /// them is kept as `\n` rather than folded to a space.  Callers must not
    /// treat the value as whitespace-safe — more-indented lines can inject
    /// arbitrary leading spaces into the parsed value.
    Folded(Chomp),
}

/// A high-level YAML parse event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event<'input> {
    /// The YAML stream has started.
    ///
    /// Always the first event in any parse.  The associated span is a
    /// zero-width span at [`crate::pos::Pos::ORIGIN`].
    StreamStart,
    /// The YAML stream has ended.
    ///
    /// Always the last event in any parse.  The associated span is a
    /// zero-width span at the position immediately after the last byte of
    /// input.
    StreamEnd,
    /// A YAML comment (YAML 1.2 §6.6).
    ///
    /// `text` is the comment body — the content of the line after the `#`
    /// character, with the `#` itself excluded.  Leading whitespace after `#`
    /// is preserved (e.g. `# hello` → text `" hello"`; `#nospace` → text
    /// `"nospace"`).  The associated span covers from the `#` character
    /// through the last byte of comment text (the newline is not included).
    ///
    /// One `Comment` event is emitted per physical line.
    Comment {
        /// Comment body (everything after the `#`, excluding the newline).
        text: &'input str,
    },
    /// An alias node (`*name`) that references a previously anchored node.
    ///
    /// The associated span covers the entire `*name` token (from `*` through
    /// the last character of the name).  Resolution of the alias to its
    /// anchored node is the loader's responsibility (Task 20) — the parser
    /// emits this event without expansion.
    Alias {
        /// The anchor name being referenced (e.g. `"foo"` for `*foo`).
        /// Borrowed directly from input — no allocation.
        name: &'input str,
    },
    /// A document has started.
    ///
    /// `explicit` is `true` when the document was introduced with `---`.
    /// `false` for bare documents (no marker).
    DocumentStart {
        /// Whether the document was introduced with `---`.
        explicit: bool,
        /// Version from the `%YAML` directive preceding this document, if any.
        ///
        /// `Some((1, 2))` for `%YAML 1.2`, `None` when no `%YAML` directive was present.
        version: Option<(u8, u8)>,
        /// Tag handle/prefix pairs from `%TAG` directives preceding this document.
        ///
        /// Each entry is `(handle, prefix)` — e.g. `("!foo!", "tag:example.com,2026:")`.
        /// Empty when no `%TAG` directives were present.
        tag_directives: Vec<(String, String)>,
    },
    /// A document has ended.
    ///
    /// `explicit` is `true` when the document was closed with `...`.
    /// `false` for implicitly-ended documents.
    DocumentEnd {
        /// Whether the document was closed with `...`.
        explicit: bool,
    },
    /// A block or flow sequence has started.
    ///
    /// Followed by zero or more node events (scalars or nested collections),
    /// then a matching [`Event::SequenceEnd`].
    SequenceStart {
        /// Whether this is a block (`-` indicator) or flow (`[...]`) sequence.
        style: CollectionStyle,
        /// Rare fields: `anchor`, `anchor_loc`, `tag`, `tag_loc`.
        /// `None` when no anchor or source-text tag is present (the common case).
        meta: Option<Box<EventMeta<'input>>>,
    },
    /// A sequence has ended.
    ///
    /// Matches the most recent [`Event::SequenceStart`] on the event stack.
    SequenceEnd,
    /// A block or flow mapping has started.
    ///
    /// Followed by alternating key/value node events (scalars or nested
    /// collections), then a matching [`Event::MappingEnd`].
    MappingStart {
        /// Whether this is a block (indentation-based) or flow (`{...}`) mapping.
        style: CollectionStyle,
        /// Rare fields: `anchor`, `anchor_loc`, `tag`, `tag_loc`.
        /// `None` when no anchor or source-text tag is present (the common case).
        meta: Option<Box<EventMeta<'input>>>,
    },
    /// A mapping has ended.
    ///
    /// Matches the most recent [`Event::MappingStart`] on the event stack.
    MappingEnd,
    /// A scalar value.
    ///
    /// `value` borrows from input when no transformation is required (the
    /// vast majority of plain scalars).  It owns when line folding produces
    /// a string that doesn't exist contiguously in the input.
    Scalar {
        /// The scalar's decoded value.
        value: Cow<'input, str>,
        /// The style in which the scalar appeared in the source.
        style: ScalarStyle,
        /// Rare fields: `anchor`, `anchor_loc`, `tag`, `tag_loc`.
        /// `None` when no anchor or source-text tag is present (the common case).
        meta: Option<Box<EventMeta<'input>>>,
    },
}

impl Event<'_> {
    /// Returns the anchor name if this event defines one.
    #[must_use]
    #[inline]
    pub fn anchor(&self) -> Option<&str> {
        match self {
            Self::Scalar { meta, .. }
            | Self::SequenceStart { meta, .. }
            | Self::MappingStart { meta, .. } => meta.as_ref().and_then(|m| m.anchor),
            Self::StreamStart
            | Self::StreamEnd
            | Self::Comment { .. }
            | Self::Alias { .. }
            | Self::DocumentStart { .. }
            | Self::DocumentEnd { .. }
            | Self::SequenceEnd
            | Self::MappingEnd => None,
        }
    }

    /// Returns the source span of the `&name` anchor token, if any.
    #[must_use]
    #[inline]
    pub fn anchor_loc(&self) -> Option<Span> {
        match self {
            Self::Scalar { meta, .. }
            | Self::SequenceStart { meta, .. }
            | Self::MappingStart { meta, .. } => meta.as_ref().and_then(|m| m.anchor_loc),
            Self::StreamStart
            | Self::StreamEnd
            | Self::Comment { .. }
            | Self::Alias { .. }
            | Self::DocumentStart { .. }
            | Self::DocumentEnd { .. }
            | Self::SequenceEnd
            | Self::MappingEnd => None,
        }
    }

    /// Returns the resolved tag string, if any.
    #[must_use]
    #[inline]
    pub fn tag(&self) -> Option<&str> {
        match self {
            Self::Scalar { meta, .. }
            | Self::SequenceStart { meta, .. }
            | Self::MappingStart { meta, .. } => meta.as_ref().and_then(|m| m.tag.as_deref()),
            Self::StreamStart
            | Self::StreamEnd
            | Self::Comment { .. }
            | Self::Alias { .. }
            | Self::DocumentStart { .. }
            | Self::DocumentEnd { .. }
            | Self::SequenceEnd
            | Self::MappingEnd => None,
        }
    }

    /// Returns the source span of the tag token, if any.
    #[must_use]
    #[inline]
    pub fn tag_loc(&self) -> Option<Span> {
        match self {
            Self::Scalar { meta, .. }
            | Self::SequenceStart { meta, .. }
            | Self::MappingStart { meta, .. } => meta.as_ref().and_then(|m| m.tag_loc),
            Self::StreamStart
            | Self::StreamEnd
            | Self::Comment { .. }
            | Self::Alias { .. }
            | Self::DocumentStart { .. }
            | Self::DocumentEnd { .. }
            | Self::SequenceEnd
            | Self::MappingEnd => None,
        }
    }
}

/// Build an `EventMeta` box when at least one field is `Some`.
///
/// Returns `None` when all four fields are `None` (the common case).
#[expect(
    clippy::redundant_pub_crate,
    reason = "pub(crate) inside private module — accessibility requires crate-wide visibility"
)]
#[inline]
pub(crate) fn make_meta<'input>(
    anchor: Option<&'input str>,
    anchor_loc: Option<Span>,
    tag: Option<Cow<'input, str>>,
    tag_loc: Option<Span>,
) -> Option<Box<EventMeta<'input>>> {
    if anchor.is_none() && tag.is_none() {
        None
    } else {
        Some(Box::new(EventMeta {
            anchor,
            anchor_loc,
            tag,
            tag_loc,
        }))
    }
}

const _: () = assert!(
    std::mem::size_of::<Event<'_>>() <= 56,
    "Event must be at most 56 bytes after EventMeta boxing"
);

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use rstest::rstest;

    use super::*;
    use crate::pos::Span;

    const SPAN: Span = Span { start: 0, end: 4 };
    const SPAN2: Span = Span { start: 5, end: 9 };

    // EM-1: meta is None when all four fields are absent.
    #[test]
    fn make_meta_returns_none_when_all_fields_absent() {
        let meta = make_meta(None, None, None, None);
        assert!(
            meta.is_none(),
            "make_meta must return None when anchor and tag are both None"
        );
    }

    // EM-2: meta is Some when only anchor is present.
    #[test]
    fn make_meta_returns_some_when_anchor_only() {
        let meta = make_meta(Some("a"), Some(SPAN), None, None).unwrap();
        assert_eq!(meta.anchor, Some("a"));
        assert_eq!(meta.anchor_loc, Some(SPAN));
        assert!(meta.tag.is_none());
        assert!(meta.tag_loc.is_none());
    }

    // EM-3: meta is Some when only tag is present.
    #[test]
    fn make_meta_returns_some_when_tag_only() {
        let meta = make_meta(None, None, Some(Cow::Borrowed("!str")), Some(SPAN)).unwrap();
        assert!(meta.anchor.is_none());
        assert!(meta.anchor_loc.is_none());
        assert_eq!(meta.tag.as_deref(), Some("!str"));
        assert_eq!(meta.tag_loc, Some(SPAN));
    }

    // EM-4: meta is Some when both anchor and tag are present.
    #[test]
    fn make_meta_returns_some_when_both_anchor_and_tag() {
        let meta = make_meta(
            Some("a"),
            Some(SPAN),
            Some(Cow::Borrowed("!str")),
            Some(SPAN),
        )
        .unwrap();
        assert_eq!(meta.anchor, Some("a"));
        assert_eq!(meta.tag.as_deref(), Some("!str"));
    }

    // EM-5: Event size at or below 56 bytes.
    #[test]
    fn event_size_at_most_56_bytes() {
        assert!(
            std::mem::size_of::<Event<'_>>() <= 56,
            "Event size {} exceeds 56 bytes",
            std::mem::size_of::<Event<'_>>()
        );
    }

    // -----------------------------------------------------------------------
    // Accessor tests — anchor(), anchor_loc(), tag(), tag_loc()
    //
    // EA-1..EA-4: Each parameterized test covers all 11 Event variants.
    // Node-typed variants (Scalar, SequenceStart, MappingStart) are exercised
    // with both meta=None and meta=Some to hit both arms of the accessor.
    // -----------------------------------------------------------------------

    fn meta_anchor_only() -> Option<Box<EventMeta<'static>>> {
        make_meta(Some("anc"), Some(SPAN), None, None)
    }

    fn meta_tag_only() -> Option<Box<EventMeta<'static>>> {
        make_meta(None, None, Some(Cow::Borrowed("!str")), Some(SPAN2))
    }

    fn meta_both() -> Option<Box<EventMeta<'static>>> {
        make_meta(
            Some("anc"),
            Some(SPAN),
            Some(Cow::Borrowed("!str")),
            Some(SPAN2),
        )
    }

    // EA-1: anchor() returns Some for node-typed variants with anchor meta,
    //       and None for all non-node variants and node variants without anchor.
    #[rstest]
    // Node-typed, meta=None → None
    #[case::scalar_no_meta(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: None }, None)]
    #[case::sequence_start_no_meta(Event::SequenceStart { style: CollectionStyle::Block, meta: None }, None)]
    #[case::mapping_start_no_meta(Event::MappingStart { style: CollectionStyle::Block, meta: None }, None)]
    // Node-typed, meta=Some anchor-only → Some
    #[case::scalar_anchor_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_anchor_only() }, Some("anc"))]
    #[case::sequence_start_anchor_only(Event::SequenceStart { style: CollectionStyle::Block, meta: meta_anchor_only() }, Some("anc"))]
    #[case::mapping_start_anchor_only(Event::MappingStart { style: CollectionStyle::Block, meta: meta_anchor_only() }, Some("anc"))]
    // Node-typed, meta=Some tag-only → None (no anchor)
    #[case::scalar_tag_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_tag_only() }, None)]
    // Node-typed, meta=Some both → Some
    #[case::scalar_both(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_both() }, Some("anc"))]
    // Non-node variants → None
    #[case::stream_start(Event::StreamStart, None)]
    #[case::stream_end(Event::StreamEnd, None)]
    #[case::comment(Event::Comment { text: "hi" }, None)]
    #[case::alias(Event::Alias { name: "x" }, None)]
    #[case::document_start(Event::DocumentStart { explicit: false, version: None, tag_directives: vec![] }, None)]
    #[case::document_end(Event::DocumentEnd { explicit: false }, None)]
    #[case::sequence_end(Event::SequenceEnd, None)]
    #[case::mapping_end(Event::MappingEnd, None)]
    fn anchor_returns_expected(#[case] event: Event<'_>, #[case] expected: Option<&str>) {
        assert_eq!(event.anchor(), expected);
    }

    // EA-2: anchor_loc() returns Some(Span) for node-typed variants with anchor meta,
    //       and None for all non-node variants and node variants without anchor.
    #[rstest]
    // Node-typed, meta=None → None
    #[case::scalar_no_meta(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: None }, None)]
    #[case::sequence_start_no_meta(Event::SequenceStart { style: CollectionStyle::Block, meta: None }, None)]
    #[case::mapping_start_no_meta(Event::MappingStart { style: CollectionStyle::Block, meta: None }, None)]
    // Node-typed, meta=Some anchor-only → Some(SPAN)
    #[case::scalar_anchor_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_anchor_only() }, Some(SPAN))]
    #[case::sequence_start_anchor_only(Event::SequenceStart { style: CollectionStyle::Block, meta: meta_anchor_only() }, Some(SPAN))]
    #[case::mapping_start_anchor_only(Event::MappingStart { style: CollectionStyle::Block, meta: meta_anchor_only() }, Some(SPAN))]
    // Node-typed, meta=Some tag-only → None (no anchor_loc)
    #[case::scalar_tag_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_tag_only() }, None)]
    // Node-typed, meta=Some both → Some(SPAN)
    #[case::scalar_both(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_both() }, Some(SPAN))]
    // Non-node variants → None
    #[case::stream_start(Event::StreamStart, None)]
    #[case::stream_end(Event::StreamEnd, None)]
    #[case::comment(Event::Comment { text: "hi" }, None)]
    #[case::alias(Event::Alias { name: "x" }, None)]
    #[case::document_start(Event::DocumentStart { explicit: false, version: None, tag_directives: vec![] }, None)]
    #[case::document_end(Event::DocumentEnd { explicit: false }, None)]
    #[case::sequence_end(Event::SequenceEnd, None)]
    #[case::mapping_end(Event::MappingEnd, None)]
    fn anchor_loc_returns_expected(#[case] event: Event<'_>, #[case] expected: Option<Span>) {
        assert_eq!(event.anchor_loc(), expected);
    }

    // EA-3: tag() returns Some for node-typed variants with tag meta,
    //       and None for all non-node variants and node variants without tag.
    #[rstest]
    // Node-typed, meta=None → None
    #[case::scalar_no_meta(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: None }, None)]
    #[case::sequence_start_no_meta(Event::SequenceStart { style: CollectionStyle::Block, meta: None }, None)]
    #[case::mapping_start_no_meta(Event::MappingStart { style: CollectionStyle::Block, meta: None }, None)]
    // Node-typed, meta=Some anchor-only → None (no tag)
    #[case::scalar_anchor_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_anchor_only() }, None)]
    // Node-typed, meta=Some tag-only → Some
    #[case::scalar_tag_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_tag_only() }, Some("!str"))]
    #[case::sequence_start_tag_only(Event::SequenceStart { style: CollectionStyle::Block, meta: meta_tag_only() }, Some("!str"))]
    #[case::mapping_start_tag_only(Event::MappingStart { style: CollectionStyle::Block, meta: meta_tag_only() }, Some("!str"))]
    // Node-typed, meta=Some both → Some
    #[case::scalar_both(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_both() }, Some("!str"))]
    // Non-node variants → None
    #[case::stream_start(Event::StreamStart, None)]
    #[case::stream_end(Event::StreamEnd, None)]
    #[case::comment(Event::Comment { text: "hi" }, None)]
    #[case::alias(Event::Alias { name: "x" }, None)]
    #[case::document_start(Event::DocumentStart { explicit: false, version: None, tag_directives: vec![] }, None)]
    #[case::document_end(Event::DocumentEnd { explicit: false }, None)]
    #[case::sequence_end(Event::SequenceEnd, None)]
    #[case::mapping_end(Event::MappingEnd, None)]
    fn tag_returns_expected(#[case] event: Event<'_>, #[case] expected: Option<&str>) {
        assert_eq!(event.tag(), expected);
    }

    // EA-4: tag_loc() returns Some(Span) for node-typed variants with tag meta,
    //       and None for all non-node variants and node variants without tag.
    #[rstest]
    // Node-typed, meta=None → None
    #[case::scalar_no_meta(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: None }, None)]
    #[case::sequence_start_no_meta(Event::SequenceStart { style: CollectionStyle::Block, meta: None }, None)]
    #[case::mapping_start_no_meta(Event::MappingStart { style: CollectionStyle::Block, meta: None }, None)]
    // Node-typed, meta=Some anchor-only → None (no tag_loc)
    #[case::scalar_anchor_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_anchor_only() }, None)]
    // Node-typed, meta=Some tag-only → Some(SPAN2)
    #[case::scalar_tag_only(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_tag_only() }, Some(SPAN2))]
    #[case::sequence_start_tag_only(Event::SequenceStart { style: CollectionStyle::Block, meta: meta_tag_only() }, Some(SPAN2))]
    #[case::mapping_start_tag_only(Event::MappingStart { style: CollectionStyle::Block, meta: meta_tag_only() }, Some(SPAN2))]
    // Node-typed, meta=Some both → Some(SPAN2)
    #[case::scalar_both(Event::Scalar { value: Cow::Borrowed("v"), style: ScalarStyle::Plain, meta: meta_both() }, Some(SPAN2))]
    // Non-node variants → None
    #[case::stream_start(Event::StreamStart, None)]
    #[case::stream_end(Event::StreamEnd, None)]
    #[case::comment(Event::Comment { text: "hi" }, None)]
    #[case::alias(Event::Alias { name: "x" }, None)]
    #[case::document_start(Event::DocumentStart { explicit: false, version: None, tag_directives: vec![] }, None)]
    #[case::document_end(Event::DocumentEnd { explicit: false }, None)]
    #[case::sequence_end(Event::SequenceEnd, None)]
    #[case::mapping_end(Event::MappingEnd, None)]
    fn tag_loc_returns_expected(#[case] event: Event<'_>, #[case] expected: Option<Span>) {
        assert_eq!(event.tag_loc(), expected);
    }
}