Skip to main content

yaml_rt_core/
source.rs

1use crate::{Diagnostic, DiagnosticKind, YamlError};
2
3/// YAML version targeted by this workspace.
4pub const TARGET_YAML_VERSION: &str = "1.2.2";
5
6/// Identifier for a node stored inside a [`crate::YamlDoc`].
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct NodeId(pub u32);
9
10impl NodeId {
11    /// Creates a node ID from a vector index.
12    ///
13    /// # Panics
14    ///
15    /// Panics when `index` cannot fit in the u32-backed node ID.
16    #[must_use]
17    pub fn from_usize(index: usize) -> Self {
18        Self(u32::try_from(index).expect("node arena is too large for u32-based node IDs"))
19    }
20
21    /// Returns this node ID as a vector index.
22    #[must_use]
23    pub const fn as_usize(self) -> usize {
24        self.0 as usize
25    }
26}
27
28/// A byte span inside a [`Source`].
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub struct Span {
31    /// Inclusive start byte offset.
32    pub start: u32,
33    /// Exclusive end byte offset.
34    pub end: u32,
35}
36
37impl Span {
38    /// Creates a new byte span.
39    #[must_use]
40    pub const fn new(start: u32, end: u32) -> Self {
41        Self { start, end }
42    }
43
44    /// Creates a span from usize byte offsets.
45    ///
46    /// # Panics
47    ///
48    /// Panics when either offset cannot fit in the u32-backed span.
49    #[must_use]
50    pub fn from_usize(start: usize, end: usize) -> Self {
51        Self::try_from((start, end)).expect("YAML source is too large for u32-based spans")
52    }
53
54    /// Returns an empty span at `offset`.
55    #[must_use]
56    pub const fn empty(offset: u32) -> Self {
57        Self {
58            start: offset,
59            end: offset,
60        }
61    }
62
63    pub(crate) fn usize_to_u32(offset: usize) -> u32 {
64        u32::try_from(offset).expect("YAML source is too large for u32-based spans")
65    }
66
67    pub(crate) fn offset_from_usize(base: u32, offset: usize) -> u32 {
68        base.checked_add(Self::usize_to_u32(offset))
69            .expect("YAML source is too large for u32-based spans")
70    }
71
72    /// Returns an empty span at `offset`.
73    #[must_use]
74    pub fn empty_from_usize(offset: usize) -> Self {
75        Self::empty(Self::usize_to_u32(offset))
76    }
77
78    /// Returns the span length in bytes.
79    #[must_use]
80    pub const fn len(self) -> u32 {
81        self.end.saturating_sub(self.start)
82    }
83
84    /// Returns true when this span covers no bytes.
85    #[must_use]
86    pub const fn is_empty(self) -> bool {
87        self.start == self.end
88    }
89
90    /// Returns true when `offset` is inside this span.
91    #[must_use]
92    pub const fn contains(self, offset: u32) -> bool {
93        self.start <= offset && offset < self.end
94    }
95}
96
97impl TryFrom<(usize, usize)> for Span {
98    type Error = std::num::TryFromIntError;
99
100    fn try_from((start, end): (usize, usize)) -> Result<Self, Self::Error> {
101        Ok(Self {
102            start: Self::usize_to_u32(start),
103            end: Self::usize_to_u32(end),
104        })
105    }
106}
107/// One-based line and column location.
108#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109pub struct LineCol {
110    /// One-based line number.
111    pub line: usize,
112    /// One-based column number in bytes for the current bootstrap model.
113    pub column: usize,
114}
115
116/// Original YAML input plus line-start metadata.
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct Source {
119    text: String,
120    line_starts: Vec<u32>,
121    line_facts: Vec<LineFacts>,
122}
123
124const NO_LINE_OFFSET: u16 = u16::MAX;
125const NO_LINE_INDEX: u32 = u32::MAX;
126const LINE_BLANK: u16 = 1 << 0;
127const LINE_SIMPLE_MAPPING: u16 = 1 << 1;
128const LINE_OFFSET_OVERFLOW: u16 = 1 << 2;
129const LINE_COMMENT: u16 = 1 << 3;
130const LINE_SCALAR_PLAIN: u16 = 1 << 4;
131const LINE_SCALAR_SINGLE_QUOTED: u16 = 1 << 5;
132const LINE_SCALAR_DOUBLE_QUOTED: u16 = 1 << 6;
133const LINE_FACTS_MIN_SOURCE_BYTES: usize = 1024;
134
135/// Compact facts for the common block-line parser path.
136///
137/// Offsets are relative to the start of the line. Exceptionally long lines use
138/// the existing full scanners instead of retaining wider offsets for every
139/// ordinary line.
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub(crate) struct LineFacts {
142    indent: u16,
143    mapping_colon: u16,
144    value_start: u16,
145    scalar_end: u16,
146    flags: u16,
147    next_significant: u32,
148}
149
150impl LineFacts {
151    const FALLBACK: Self = Self {
152        indent: NO_LINE_OFFSET,
153        mapping_colon: NO_LINE_OFFSET,
154        value_start: NO_LINE_OFFSET,
155        scalar_end: NO_LINE_OFFSET,
156        flags: LINE_OFFSET_OVERFLOW,
157        next_significant: NO_LINE_INDEX,
158    };
159
160    pub(crate) fn indent(self) -> Option<usize> {
161        (!self.has(LINE_OFFSET_OVERFLOW)).then_some(self.indent as usize)
162    }
163
164    pub(crate) fn simple_mapping(self) -> Option<(usize, usize)> {
165        self.has(LINE_SIMPLE_MAPPING)
166            .then_some((self.mapping_colon as usize, self.value_start as usize))
167    }
168
169    pub(crate) fn mapping_colon(self) -> Option<usize> {
170        (self.mapping_colon != NO_LINE_OFFSET).then_some(self.mapping_colon as usize)
171    }
172
173    pub(crate) fn scalar_mapping(self) -> Option<(usize, usize, usize, CachedScalarStyle)> {
174        let style = if self.has(LINE_SCALAR_PLAIN) {
175            CachedScalarStyle::Plain
176        } else if self.has(LINE_SCALAR_SINGLE_QUOTED) {
177            CachedScalarStyle::SingleQuoted
178        } else if self.has(LINE_SCALAR_DOUBLE_QUOTED) {
179            CachedScalarStyle::DoubleQuoted
180        } else {
181            return None;
182        };
183        Some((
184            self.mapping_colon as usize,
185            self.value_start as usize,
186            self.scalar_end as usize,
187            style,
188        ))
189    }
190
191    pub(crate) const fn is_blank(self) -> bool {
192        self.has(LINE_BLANK)
193    }
194
195    fn next_significant(self) -> Option<usize> {
196        (self.next_significant != NO_LINE_INDEX).then_some(self.next_significant as usize)
197    }
198
199    const fn has(self, flag: u16) -> bool {
200        self.flags & flag != 0
201    }
202}
203
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
205pub(crate) enum CachedScalarStyle {
206    Plain,
207    SingleQuoted,
208    DoubleQuoted,
209}
210
211impl Source {
212    /// Builds a source buffer, validates YAML 1.2.2 printable characters, and
213    /// records all line starts.
214    ///
215    /// # Errors
216    ///
217    /// Returns an error when `text` contains characters that are not valid in a
218    /// YAML 1.2.2 stream.
219    pub fn new(text: String) -> Result<Self, YamlError> {
220        let bytes = text.as_bytes();
221        let mut line_starts = Vec::with_capacity(text.len() / 32 + 1);
222        line_starts.push(0);
223        const SOURCE_SCAN_CHUNK: usize = 32;
224        let complete = bytes.len() / SOURCE_SCAN_CHUNK * SOURCE_SCAN_CHUNK;
225        for (chunk_index, chunk) in bytes[..complete]
226            .chunks_exact(SOURCE_SCAN_CHUNK)
227            .enumerate()
228        {
229            validate_source_chunk(
230                &text,
231                bytes,
232                chunk_index * SOURCE_SCAN_CHUNK,
233                chunk,
234                &mut line_starts,
235            )?;
236        }
237        validate_source_chunk(&text, bytes, complete, &bytes[complete..], &mut line_starts)?;
238        let line_facts = if text.len() >= LINE_FACTS_MIN_SOURCE_BYTES {
239            build_line_facts(&text, &line_starts)
240        } else {
241            Vec::new()
242        };
243
244        Ok(Self {
245            text,
246            line_starts,
247            line_facts,
248        })
249    }
250
251    /// Returns the original input text.
252    #[must_use]
253    pub fn as_str(&self) -> &str {
254        &self.text
255    }
256
257    /// Returns the source length in bytes.
258    #[must_use]
259    pub fn len(&self) -> usize {
260        self.text.len()
261    }
262
263    /// Returns true when the source is empty.
264    #[must_use]
265    pub fn is_empty(&self) -> bool {
266        self.text.is_empty()
267    }
268
269    /// Returns the recorded line-start byte offsets.
270    #[must_use]
271    pub fn line_starts(&self) -> &[u32] {
272        &self.line_starts
273    }
274
275    pub(crate) fn line_facts(&self, index: usize) -> LineFacts {
276        self.line_facts
277            .get(index)
278            .copied()
279            .unwrap_or(LineFacts::FALLBACK)
280    }
281
282    pub(crate) fn cached_next_significant_line(&self, index: usize) -> Option<Option<usize>> {
283        self.line_facts
284            .get(index)
285            .copied()
286            .map(LineFacts::next_significant)
287    }
288
289    pub(crate) fn has_line_facts(&self) -> bool {
290        !self.line_facts.is_empty()
291    }
292
293    pub(crate) fn validated_line_slice(&self, start: usize, end: usize) -> &str {
294        debug_assert!(start <= end);
295        debug_assert!(end <= self.text.len());
296        debug_assert!(self.text.is_char_boundary(start));
297        debug_assert!(self.text.is_char_boundary(end));
298        // SAFETY: line offsets are produced while scanning this owned, valid
299        // UTF-8 string. The assertions retain those invariants in debug builds.
300        unsafe { self.text.get_unchecked(start..end) }
301    }
302
303    /// Returns the source slice for `span`.
304    ///
305    /// # Panics
306    ///
307    /// Panics if `span` is outside the source or does not fall on UTF-8
308    /// boundaries. Use [`Source::try_slice`] when handling user-provided spans.
309    #[must_use]
310    pub fn slice(&self, span: Span) -> &str {
311        self.try_slice(span)
312            .expect("span must be in bounds and on UTF-8 boundaries")
313    }
314
315    /// Returns the source slice for `span`, or a span-rich error when invalid.
316    ///
317    /// # Errors
318    ///
319    /// Returns an error when `span` is outside the source text or does not fall
320    /// on UTF-8 boundaries.
321    pub fn try_slice(&self, span: Span) -> Result<&str, YamlError> {
322        let start = span.start as usize;
323        let end = span.end as usize;
324
325        if start > end || end > self.text.len() {
326            return Err(YamlError::new(Diagnostic::new(
327                DiagnosticKind::Source,
328                "span is outside the source text",
329                span,
330            )));
331        }
332
333        self.text.get(start..end).ok_or_else(|| {
334            YamlError::new(Diagnostic::new(
335                DiagnosticKind::Source,
336                "span does not align with UTF-8 character boundaries",
337                span,
338            ))
339        })
340    }
341
342    /// Converts a byte offset into a one-based line/column pair.
343    #[must_use]
344    pub fn line_col(&self, offset: usize) -> LineCol {
345        let offset = Span::usize_to_u32(offset.min(self.text.len()));
346        let line_index = match self.line_starts.binary_search(&offset) {
347            Ok(index) => index,
348            Err(index) => index.saturating_sub(1),
349        };
350        let line_start = self.line_starts[line_index];
351
352        LineCol {
353            line: line_index + 1,
354            column: (offset - line_start) as usize + 1,
355        }
356    }
357
358    /// Returns the line/column pair for a diagnostic's primary span.
359    #[must_use]
360    pub fn diagnostic_position(&self, diagnostic: &Diagnostic) -> LineCol {
361        self.line_col(diagnostic.span.start as usize)
362    }
363}
364
365fn validate_source_chunk(
366    text: &str,
367    bytes: &[u8],
368    base: usize,
369    chunk: &[u8],
370    line_starts: &mut Vec<u32>,
371) -> Result<(), YamlError> {
372    let mut cursor = 0;
373    while let Some(relative) = chunk[cursor..]
374        .iter()
375        .position(|byte| *byte < b' ' || matches!(*byte, 0x7F | 0xC2 | 0xEF))
376    {
377        let relative = cursor + relative;
378        let byte = chunk[relative];
379        let offset = base + relative;
380        if byte < 0x80 {
381            if !matches!(byte, b'\t' | b'\n' | b'\r' | b' '..=b'~') {
382                return Err(invalid_yaml_character(offset, char::from(byte)));
383            }
384            if byte == b'\n' {
385                line_starts.push(Span::usize_to_u32(offset + 1));
386            }
387        } else if unicode_sequence_may_be_non_printable(bytes, offset) {
388            let character = text[offset..]
389                .chars()
390                .next()
391                .expect("offset starts a valid UTF-8 character");
392            if !is_yaml_printable(character) {
393                return Err(invalid_yaml_character(offset, character));
394            }
395        }
396        cursor = relative + 1;
397    }
398    Ok(())
399}
400
401fn unicode_sequence_may_be_non_printable(bytes: &[u8], offset: usize) -> bool {
402    match bytes[offset..] {
403        [0xC2, continuation, ..] => (0x80..=0x9F).contains(&continuation) && continuation != 0x85,
404        [0xEF, 0xBF, 0xBE | 0xBF, ..] => true,
405        _ => false,
406    }
407}
408
409fn build_line_facts(text: &str, line_starts: &[u32]) -> Vec<LineFacts> {
410    let mut facts = Vec::with_capacity(line_starts.len());
411    for (index, &start) in line_starts.iter().enumerate() {
412        let start = start as usize;
413        let mut end = line_starts
414            .get(index + 1)
415            .map_or(text.len(), |next| *next as usize);
416        if end > start && text.as_bytes()[end - 1] == b'\n' {
417            end -= 1;
418            if end > start && text.as_bytes()[end - 1] == b'\r' {
419                end -= 1;
420            }
421        } else if end > start && text.as_bytes()[end - 1] == b'\r' {
422            end -= 1;
423        }
424        facts.push(analyze_line(&text.as_bytes()[start..end]));
425    }
426    populate_next_significant_lines(&mut facts);
427    facts
428}
429
430fn populate_next_significant_lines(facts: &mut [LineFacts]) {
431    let mut next = NO_LINE_INDEX;
432    for (index, fact) in facts.iter_mut().enumerate().rev() {
433        fact.next_significant = next;
434        if !fact.has(LINE_BLANK | LINE_COMMENT) {
435            next = u32::try_from(index).expect("line index exceeds u32 capacity");
436        }
437    }
438}
439
440fn analyze_line(line: &[u8]) -> LineFacts {
441    let indent = line.iter().take_while(|byte| **byte == b' ').count();
442    let mut flags = 0;
443    if line[indent..].is_empty() {
444        flags |= LINE_BLANK;
445    } else if line[indent] == b'#' {
446        flags |= LINE_COMMENT;
447    }
448
449    if line.len() >= NO_LINE_OFFSET as usize {
450        return LineFacts {
451            flags: flags | LINE_OFFSET_OVERFLOW,
452            ..LineFacts::FALLBACK
453        };
454    }
455
456    let Some(indent) = u16::try_from(indent)
457        .ok()
458        .filter(|value| *value != NO_LINE_OFFSET)
459    else {
460        return LineFacts {
461            indent: NO_LINE_OFFSET,
462            mapping_colon: NO_LINE_OFFSET,
463            value_start: NO_LINE_OFFSET,
464            scalar_end: NO_LINE_OFFSET,
465            flags: flags | LINE_OFFSET_OVERFLOW,
466            next_significant: NO_LINE_INDEX,
467        };
468    };
469
470    let body = &line[indent as usize..];
471    if let Some((colon, value_start)) = plain_key_mapping_offsets(body)
472        && let Ok(colon) = u16::try_from(colon)
473        && colon != NO_LINE_OFFSET
474    {
475        let scalar_value_start = value_start;
476        let value_start = scalar_value_start
477            .and_then(|offset| u16::try_from(offset).ok())
478            .filter(|offset| *offset != NO_LINE_OFFSET);
479        let mut scalar_end = NO_LINE_OFFSET;
480        if let Some(start) = scalar_value_start
481            && let Some((end, scalar_flag)) = cached_scalar_offsets(&body[start..])
482            && let Ok(end) = u16::try_from(start + end)
483            && end != NO_LINE_OFFSET
484        {
485            scalar_end = end;
486            flags |= scalar_flag;
487            if scalar_flag == LINE_SCALAR_PLAIN {
488                flags |= LINE_SIMPLE_MAPPING;
489            }
490        }
491        return LineFacts {
492            indent,
493            mapping_colon: colon,
494            value_start: value_start.unwrap_or(NO_LINE_OFFSET),
495            scalar_end,
496            flags,
497            next_significant: NO_LINE_INDEX,
498        };
499    }
500
501    LineFacts {
502        indent,
503        mapping_colon: NO_LINE_OFFSET,
504        value_start: NO_LINE_OFFSET,
505        scalar_end: NO_LINE_OFFSET,
506        flags,
507        next_significant: NO_LINE_INDEX,
508    }
509}
510
511fn plain_key_mapping_offsets(body: &[u8]) -> Option<(usize, Option<usize>)> {
512    let mut colon = 0;
513    while colon < body.len() && body[colon] != b':' {
514        if !is_simple_plain_byte(body[colon]) {
515            return None;
516        }
517        colon += 1;
518    }
519    if colon == 0 || colon == body.len() {
520        return None;
521    }
522
523    let mut value_start = colon + 1;
524    if value_start == body.len() {
525        return Some((colon, None));
526    }
527    if body.get(value_start) != Some(&b' ') {
528        return None;
529    }
530    while body.get(value_start) == Some(&b' ') {
531        value_start += 1;
532    }
533    if value_start == body.len() || &body[value_start..] == b"-" {
534        return Some((colon, None));
535    }
536    Some((colon, Some(value_start)))
537}
538
539fn cached_scalar_offsets(text: &[u8]) -> Option<(usize, u16)> {
540    match text.first().copied()? {
541        b'"' => {
542            let end = text[1..]
543                .iter()
544                .position(|byte| matches!(*byte, b'"' | b'\\'))?
545                + 1;
546            if text[end] == b'\\' || !valid_cached_quoted_trailing_text(&text[end + 1..]) {
547                return None;
548            }
549            Some((end + 1, LINE_SCALAR_DOUBLE_QUOTED))
550        }
551        b'\'' => {
552            let mut position = 1;
553            loop {
554                let quote = text[position..].iter().position(|byte| *byte == b'\'')? + position;
555                if text.get(quote + 1) == Some(&b'\'') {
556                    position = quote + 2;
557                    continue;
558                }
559                if !valid_cached_quoted_trailing_text(&text[quote + 1..]) {
560                    return None;
561                }
562                return Some((quote + 1, LINE_SCALAR_SINGLE_QUOTED));
563            }
564        }
565        _ if text.iter().all(|byte| is_simple_plain_byte(*byte)) => {
566            Some((text.len(), LINE_SCALAR_PLAIN))
567        }
568        _ => None,
569    }
570}
571
572fn valid_cached_quoted_trailing_text(trailing: &[u8]) -> bool {
573    if trailing.iter().all(|byte| *byte == b' ') {
574        return true;
575    }
576    let whitespace = trailing.iter().take_while(|byte| **byte == b' ').count();
577    whitespace > 0 && trailing.get(whitespace) == Some(&b'#')
578}
579
580const fn is_simple_plain_byte(byte: u8) -> bool {
581    byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.' | b'/')
582}
583
584fn invalid_yaml_character(offset: usize, character: char) -> YamlError {
585    let span = Span::from_usize(offset, offset + character.len_utf8());
586    YamlError::new(
587        Diagnostic::new(
588            DiagnosticKind::Source,
589            format!("invalid YAML 1.2.2 character U+{:04X}", character as u32),
590            span,
591        )
592        .with_note(
593            "YAML streams may contain tab, line feeds, carriage returns, printable Unicode characters, and non-breaking spaces",
594        ),
595    )
596}
597
598pub(crate) fn validate_yaml_chars(text: &str) -> Result<(), YamlError> {
599    for (offset, character) in text.char_indices() {
600        if !is_yaml_printable(character) {
601            let span = Span::from_usize(offset, offset + character.len_utf8());
602            return Err(YamlError::new(
603                Diagnostic::new(
604                    DiagnosticKind::Source,
605                    format!(
606                        "invalid YAML 1.2.2 character U+{:04X}",
607                        character as u32
608                    ),
609                    span,
610                )
611                .with_note(
612                    "YAML streams may contain tab, line feeds, carriage returns, printable Unicode characters, and non-breaking spaces",
613                ),
614            ));
615        }
616    }
617
618    Ok(())
619}
620
621const fn is_yaml_printable(character: char) -> bool {
622    matches!(
623        character as u32,
624        0x09 | 0x0A | 0x0D | 0x20..=0x7E | 0x85 | 0xA0..=0xD7FF | 0xE000..=0xFFFD | 0x001_0000..=0x0010_FFFF
625    )
626}
627
628#[cfg(test)]
629mod line_facts_tests {
630    use std::fmt::Write;
631
632    use super::*;
633    use crate::parser::Parser;
634    use crate::semantic::SemanticStore;
635    use crate::{Node, ParsedYaml, YamlDoc, YamlEvent};
636
637    #[derive(Debug, PartialEq, Eq)]
638    struct ParseFingerprint {
639        nodes: Vec<Node>,
640        semantics: SemanticStore,
641        events: Vec<YamlEvent>,
642        rendering: String,
643    }
644
645    fn parse_fingerprint(source: Source, parsed: ParsedYaml) -> ParseFingerprint {
646        let document = YamlDoc {
647            source,
648            nodes: parsed.nodes.clone(),
649            semantics: parsed.semantics.clone(),
650            root_override: None,
651            edits: Vec::new(),
652        };
653        ParseFingerprint {
654            nodes: parsed.nodes,
655            semantics: parsed.semantics,
656            events: document.events().collect(),
657            rendering: document.to_string(),
658        }
659    }
660
661    #[test]
662    fn caches_common_lines_and_leaves_complex_lines_on_the_fallback_path() {
663        let source = Source::new(
664            "alpha: beta\r\nunicode: café\n\tbad: tab\n\"quoted\": value\n&anchor key: value\nflow: [one, two]\nkey: value # comment\n# comment\nliteral: |\n  text\n"
665                .to_owned(),
666        )
667        .expect("fixture is printable YAML");
668
669        let facts = build_line_facts(source.as_str(), source.line_starts());
670        assert_eq!(facts[0].simple_mapping(), Some((5, 7)));
671        assert_eq!(facts[1].mapping_colon(), Some(7));
672        assert_eq!(facts[1].simple_mapping(), None);
673        assert_eq!(facts[2].mapping_colon(), None);
674        assert_eq!(facts[3].mapping_colon(), None);
675        assert_eq!(facts[4].mapping_colon(), None);
676        assert_eq!(facts[5].mapping_colon(), Some(4));
677        assert_eq!(facts[5].simple_mapping(), None);
678        assert_eq!(facts[6].mapping_colon(), Some(3));
679        assert_eq!(facts[6].simple_mapping(), None);
680        assert_eq!(facts[7].mapping_colon(), None);
681        assert_eq!(facts[8].mapping_colon(), Some(7));
682        assert_eq!(facts[9].indent(), Some(2));
683        assert_eq!(facts[5].next_significant(), Some(6));
684        assert_eq!(facts[6].next_significant(), Some(8));
685        assert_eq!(facts[9].next_significant(), None);
686        assert_eq!(std::mem::size_of::<LineFacts>(), 16);
687    }
688
689    #[test]
690    fn cached_scalar_facts_match_the_general_parser() {
691        let mut input = String::from("root:\n");
692        for index in 0..40 {
693            writeln!(input, "  plain_{index}: value_{index}")
694                .expect("writing to a String cannot fail");
695            writeln!(input, "  single_{index}: 'quoted # {index}' # trailing")
696                .expect("writing to a String cannot fail");
697            writeln!(
698                input,
699                "  double_{index}: \"Unicode café {index}\" # trailing"
700            )
701            .expect("writing to a String cannot fail");
702        }
703
704        let optimized = Source::new(input.clone()).expect("fixture is printable YAML");
705        assert!(optimized.line_facts(2).scalar_mapping().is_some());
706        assert!(optimized.line_facts(3).scalar_mapping().is_some());
707        let optimized_parse = Parser::new(&optimized)
708            .parse()
709            .expect("optimized fixture should parse");
710
711        let mut general = Source::new(input).expect("fixture is printable YAML");
712        general.line_facts.clear();
713        let general_parse = Parser::new(&general)
714            .parse()
715            .expect("general fixture should parse");
716        assert_eq!(
717            parse_fingerprint(optimized, optimized_parse),
718            parse_fingerprint(general, general_parse)
719        );
720    }
721
722    #[test]
723    fn cached_line_path_matches_fallback_diagnostics() {
724        let input = format!(
725            "{}---\nquoted: \"a\nb\nc\"\n",
726            "# oracle padding\n".repeat(80)
727        );
728        let optimized = Source::new(input.clone()).expect("fixture is printable YAML");
729        let optimized_error = Parser::new(&optimized)
730            .parse()
731            .expect_err("unindented quoted continuation is invalid")
732            .with_position_from(&optimized);
733
734        let mut general = Source::new(input).expect("fixture is printable YAML");
735        general.line_facts.clear();
736        let general_error = Parser::new(&general)
737            .parse()
738            .expect_err("fallback path must reject the same input")
739            .with_position_from(&general);
740
741        assert_eq!(optimized_error.diagnostic, general_error.diagnostic);
742    }
743
744    #[test]
745    fn cached_common_mapping_path_preserves_the_complete_source() {
746        let mut input = String::new();
747        for index in 0..100 {
748            writeln!(input, "key_{index:04}: value_{index:04}")
749                .expect("writing to a String cannot fail");
750        }
751        let source = Source::new(input.clone()).expect("generated mapping is printable YAML");
752        assert_eq!(source.line_facts(0).simple_mapping(), Some((8, 10)));
753
754        let doc = YamlDoc::parse(&input).expect("cached mapping should parse");
755        assert_eq!(doc.to_string(), input);
756    }
757
758    #[test]
759    fn long_line_offsets_fall_back_without_changing_parse_behavior() {
760        let key = "k".repeat(u16::MAX as usize);
761        let input = format!("{key}: value\n");
762        let source = Source::new(input.clone()).expect("long fixture is printable YAML");
763        assert_eq!(source.line_facts(0).mapping_colon(), None);
764
765        let doc = YamlDoc::parse(&input).expect("long mapping key should use the full scanner");
766        assert_eq!(doc.to_string(), input);
767    }
768}