serde-saphyr 0.0.23

YAML (de)serializer for Serde, emphasizing panic-free parsing and good error reporting
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
use std::cell::RefCell;
use std::io::{self, Read};
use std::rc::Rc;

/// Assumed line length to estimate the size of buffers.
/// This is more than YAML typically contains.
const ASSUMED_LINE_LENGTH: usize = 512;

/// How many most-recent bytes to retain for diagnostics/snippet rendering.
///
/// Pick this >> MAX_READ_AHEAD so read-ahead doesn't evict all "history".
pub(crate) const RING_BUFFER_SIZE: usize = 6 * ASSUMED_LINE_LENGTH;

/// Maximum number of bytes we are allowed to read-ahead beyond what the consumer has read.
/// This read-ahead happens ONLY inside `get_recent()`.
pub(crate) const MAX_READ_AHEAD: usize = 2 * ASSUMED_LINE_LENGTH;

// -------------------------
// Fixed-size ring buffer
// -------------------------

/// A fixed-size ring buffer backed by an array.
///
/// This replaces `VecDeque<u8>` for cases where the maximum size is known at compile time.
/// Uses a circular buffer with head and tail indices.
struct FixedRingBuffer<const N: usize> {
    /// The underlying storage array.
    data: [u8; N],
    /// Index of the first (oldest) element. Valid when count > 0.
    head: usize,
    /// Number of elements currently in the buffer.
    count: usize,
}

impl<const N: usize> FixedRingBuffer<N> {
    /// Create a new empty ring buffer.
    const fn new() -> Self {
        Self {
            data: [0u8; N],
            head: 0,
            count: 0,
        }
    }

    /// Returns true if the buffer is empty.
    #[inline]
    fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Returns the number of elements in the buffer.
    #[inline]
    fn len(&self) -> usize {
        self.count
    }

    /// Push a byte to the back of the buffer.
    /// If the buffer is full, the oldest element is overwritten.
    #[inline]
    fn push_back(&mut self, value: u8) {
        let tail = (self.head + self.count) % N;
        self.data[tail] = value;
        if self.count == N {
            // Buffer is full, advance head (overwrite oldest)
            self.head = (self.head + 1) % N;
        } else {
            self.count += 1;
        }
    }

    /// Remove and return the front (oldest) element, or None if empty.
    #[inline]
    fn pop_front(&mut self) -> Option<u8> {
        if self.count == 0 {
            None
        } else {
            let value = self.data[self.head];
            self.head = (self.head + 1) % N;
            self.count -= 1;
            Some(value)
        }
    }

    /// Returns an iterator over the elements from oldest to newest.
    fn iter(&self) -> FixedRingBufferIter<'_, N> {
        FixedRingBufferIter {
            buffer: self,
            pos: 0,
        }
    }
}

impl<const N: usize> std::fmt::Debug for FixedRingBuffer<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FixedRingBuffer")
            .field("head", &self.head)
            .field("count", &self.count)
            .field("capacity", &N)
            .finish()
    }
}

/// Iterator over a `FixedRingBuffer`.
struct FixedRingBufferIter<'a, const N: usize> {
    buffer: &'a FixedRingBuffer<N>,
    pos: usize,
}

impl<'a, const N: usize> Iterator for FixedRingBufferIter<'a, N> {
    type Item = u8;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.buffer.count {
            None
        } else {
            let idx = (self.buffer.head + self.pos) % N;
            self.pos += 1;
            Some(self.buffer.data[idx])
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.buffer.count - self.pos;
        (remaining, Some(remaining))
    }
}

impl<'a, const N: usize> ExactSizeIterator for FixedRingBufferIter<'a, N> {}

/// A snapshot of bytes currently retained by the reader wrapper.
///
/// Offsets are absolute byte offsets from the start of the underlying stream.
#[derive(Debug, Clone)]
#[allow(dead_code)] // Fields used in tests
pub(crate) struct RecentSnapshot {
    /// Absolute byte offset of `bytes[0]`.
    pub start_offset: u64,
    /// Absolute byte offset one-past-the-end of `bytes`.
    pub end_offset: u64,
    /// 1-based line number at `bytes[0]}`.
    pub start_line: usize,
    /// Snapshot bytes (oldest -> newest).
    pub bytes: Vec<u8>,
}

/// A `Read` wrapper that:
/// - retains the last `RING_BUFFER_SIZE` bytes in a ring buffer
/// - tracks the absolute byte offset returned to the consumer
/// - allows bounded read-ahead ONLY via `get_recent()`
///
/// Key invariants:
/// - `offset()` is the absolute offset after bytes returned to the consumer.
/// - `stash` contains bytes read from `inner` but not yet returned.
/// - Therefore the next `inner.read()` begins at `offset() + stash.len()`.
pub(crate) struct RingReader<R> {
    inner: R,

    // Ring buffer of the most-recent bytes (includes both returned bytes and read-ahead bytes).
    ring: FixedRingBuffer<RING_BUFFER_SIZE>,
    // Absolute offset of ring[0] (valid only when ring is non-empty).
    ring_start_offset: u64,
    // 1-based line number at ring[0] (valid only when ring is non-empty).
    ring_start_line: usize,

    // Read-ahead bytes (only filled by get_recent()).
    //
    // Why is `stash` a struct field rather than a local variable in `get_recent()`?
    //
    // The stash holds bytes that have been read from `inner` during `get_recent()` but have
    // NOT yet been returned to the consumer. This design enables **continued use of the reader
    // after calling `get_recent()`**: the consumer can inspect the snapshot for error diagnostics
    // and then resume reading from where they left off without losing any bytes.
    //
    // When `read()` is called, it first drains bytes from the stash before reading from `inner`
    // again. This preserves stream integrity - the consumer sees the exact same byte sequence
    // as if no read-ahead had occurred.
    //
    // If `stash` were a local variable in `get_recent()`, the read-ahead bytes would be lost
    // when the method returns, and subsequent `read()` calls would skip those bytes entirely,
    // corrupting the byte stream from the consumer's perspective.
    stash: FixedRingBuffer<MAX_READ_AHEAD>,

    // Absolute offset after bytes returned to the consumer.
    returned_total: u64,
}

impl<R> RingReader<R> {
    pub(crate) fn new(inner: R) -> Self {
        Self {
            inner,
            ring: FixedRingBuffer::new(),
            ring_start_offset: 0,
            ring_start_line: 1,
            stash: FixedRingBuffer::new(),
            returned_total: 0,
        }
    }

    /// Absolute offset after bytes already returned to the consumer.
    #[allow(dead_code)] // Used in tests
    pub(crate) fn offset(&self) -> u64 {
        self.returned_total
    }

    /// How many bytes are currently read-ahead (not yet returned to the consumer).
    #[allow(dead_code)] // Used in tests
    pub(crate) fn read_ahead_len(&self) -> usize {
        self.stash.len()
    }

    #[allow(dead_code)] // Used in tests
    pub(crate) fn inner(&self) -> &R {
        &self.inner
    }

    #[allow(dead_code)] // Used in tests
    pub(crate) fn inner_mut(&mut self) -> &mut R {
        &mut self.inner
    }

    #[allow(dead_code)] // Used in tests
    pub(crate) fn into_inner(self) -> R {
        self.inner
    }

    /// Get a snapshot of the most recently retained bytes and their absolute offset range.
    ///
    /// Behavior:
    /// - This is the ONLY method that may read-ahead from `inner`.
    /// - It will read at most `MAX_READ_AHEAD - stash.len()` additional bytes (never more).
    /// - The returned snapshot is trimmed to "reasonable UTF-8 boundaries" at the edges
    ///   (leading continuation bytes are dropped; incomplete trailing sequence is dropped).
    ///
    /// Returns:
    /// - `Ok(RecentSnapshot)` on success (even if EOF => snapshot may be empty)
    /// - `Err(io::Error)` if the underlying reader errors during read-ahead
    pub(crate) fn get_recent(&mut self) -> io::Result<RecentSnapshot>
    where
        R: Read,
    {
        // Enforce the GLOBAL read-ahead cap: total unread-ahead bytes must not exceed MAX_READ_AHEAD.
        let already_ahead = self.stash.len();
        let can_read_more = MAX_READ_AHEAD.saturating_sub(already_ahead);

        if can_read_more > 0 {
            let _ = self.read_ahead_at_most(can_read_more)?;
        }

        let (mut start_offset, mut start_line, mut bytes) = self.ring_snapshot();
        if !bytes.is_empty() {
            (start_offset, start_line, bytes) =
                trim_to_utf8_boundaries_with_line(bytes, start_offset, start_line);
        }

        let end_offset = start_offset.saturating_add(bytes.len() as u64);

        Ok(RecentSnapshot {
            start_offset,
            end_offset,
            start_line,
            bytes,
        })
    }

    fn next_inner_offset(&self) -> u64 {
        // Next unread position in the underlying stream is after returned bytes + unread stash bytes.
        self.returned_total.saturating_add(self.stash.len() as u64)
    }

    /// Returns (start_offset, start_line, bytes).
    fn ring_snapshot(&self) -> (u64, usize, Vec<u8>) {
        if self.ring.is_empty() {
            // No bytes retained; represent an empty range at the current consumer offset.
            return (self.returned_total, self.ring_start_line, Vec::new());
        }
        let start_offset = self.ring_start_offset;
        let start_line = self.ring_start_line;
        let bytes: Vec<u8> = self.ring.iter().collect();
        (start_offset, start_line, bytes)
    }

    pub(crate) fn push_ring_bytes(&mut self, bytes: &[u8], abs_start: u64) {
        let mut off = abs_start;

        for &b in bytes {
            if self.ring.is_empty() {
                self.ring_start_offset = off;
            }

            if self.ring.len() == RING_BUFFER_SIZE {
                let evicted = self.ring.pop_front();
                self.ring_start_offset = self.ring_start_offset.saturating_add(1);
                // Track newlines: if we evict a newline, increment the start line
                if evicted == Some(b'\n') {
                    self.ring_start_line = self.ring_start_line.saturating_add(1);
                }
            }

            self.ring.push_back(b);
            off = off.saturating_add(1);
        }
    }

    fn read_ahead_at_most(&mut self, max_additional: usize) -> io::Result<usize>
    where
        R: Read,
    {
        if max_additional == 0 {
            return Ok(0);
        }

        // Small fixed scratch buffer to avoid heap churn.
        const SCRATCH: usize = 8 * 1024;
        let mut scratch = [0u8; SCRATCH];

        let mut remaining = max_additional;
        let mut total = 0usize;

        while remaining > 0 {
            let want = remaining.min(SCRATCH);
            let n = self.inner.read(&mut scratch[..want])?;
            if n == 0 {
                break; // EOF
            }

            // Absolute offset of the first newly read byte.
            let abs_start = self.next_inner_offset();
            let chunk = &scratch[..n];

            // Stash it so the consumer still sees the same byte stream.
            for &b in chunk {
                self.stash.push_back(b);
            }

            // Also record into ring for snapshot context.
            self.push_ring_bytes(chunk, abs_start);

            total = total.saturating_add(n);
            remaining = remaining.saturating_sub(n);
        }

        Ok(total)
    }

    fn drain_stash_into(&mut self, out: &mut [u8]) -> usize {
        let mut n = 0usize;

        while n < out.len() {
            let b = match self.stash.pop_front() {
                Some(x) => x,
                None => break,
            };
            out[n] = b;
            n = n.saturating_add(1);
        }

        n
    }
}

impl<R: Read> Read for RingReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }

        // Always serve read-ahead bytes first.
        if !self.stash.is_empty() {
            let n = self.drain_stash_into(buf);
            self.returned_total = self.returned_total.saturating_add(n as u64);
            return Ok(n);
        }

        // No read-ahead pending: read exactly what the consumer asks for.
        let n = self.inner.read(buf)?;
        if n == 0 {
            return Ok(0);
        }

        let chunk = match buf.get(..n) {
            Some(s) => s,
            None => return Ok(0), // defensive: should be impossible
        };

        let abs_start = self.returned_total; // stash empty => next_inner_offset == returned_total
        self.push_ring_bytes(chunk, abs_start);

        self.returned_total = self.returned_total.saturating_add(n as u64);
        Ok(n)
    }
}

// -------------------------
// Shared RingReader for use in lib.rs
// -------------------------

/// A shared wrapper around `RingReader` that allows multiple references.
///
/// This is used in `lib.rs` to wrap the reader before passing to `LiveEvents`,
/// while retaining access to get snapshots when errors occur.
pub(crate) struct SharedRingReader<R> {
    inner: Rc<RefCell<RingReader<R>>>,
}

impl<R> SharedRingReader<R> {
    /// Create a new shared ring reader.
    pub(crate) fn new(reader: R) -> Self {
        Self {
            inner: Rc::new(RefCell::new(RingReader::new(reader))),
        }
    }

    /// Get a snapshot of the recent bytes for error reporting.
    pub(crate) fn get_recent(&self) -> io::Result<RecentSnapshot>
    where
        R: Read,
    {
        self.inner.borrow_mut().get_recent()
    }

    /// Clone the inner Rc for sharing.
    pub(crate) fn clone_inner(&self) -> Rc<RefCell<RingReader<R>>> {
        Rc::clone(&self.inner)
    }
}

/// A reader handle that delegates to a shared `RingReader`.
///
/// This implements `Read` and can be passed to functions that consume readers,
/// while the original `SharedRingReader` retains access for snapshots.
pub(crate) struct SharedRingReaderHandle<R> {
    inner: Rc<RefCell<RingReader<R>>>,
}

impl<R> SharedRingReaderHandle<R> {
    pub(crate) fn new(shared: &SharedRingReader<R>) -> Self {
        Self {
            inner: shared.clone_inner(),
        }
    }
}

impl<R: Read> Read for SharedRingReaderHandle<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.borrow_mut().read(buf)
    }
}

// -------------------------
// UTF-8 edge trimming helpers
// -------------------------

fn is_utf8_continuation(b: u8) -> bool {
    (b & 0b1100_0000) == 0b1000_0000
}

/// Return expected UTF-8 sequence length for valid lead bytes.
/// Returns None if `b` is not a valid lead byte.
fn utf8_expected_len(lead: u8) -> Option<usize> {
    if lead <= 0x7F {
        Some(1)
    } else if (0xC2..=0xDF).contains(&lead) {
        Some(2)
    } else if (0xE0..=0xEF).contains(&lead) {
        Some(3)
    } else if (0xF0..=0xF4).contains(&lead) {
        Some(4)
    } else {
        None
    }
}

/// Trim to "reasonable UTF-8 boundaries" and also track line numbers:
/// - drop leading continuation bytes (and adjust start_offset and start_line accordingly)
/// - drop a trailing incomplete UTF-8 sequence (if snapshot ends mid-codepoint)
fn trim_to_utf8_boundaries_with_line(
    mut bytes: Vec<u8>,
    mut start_offset: u64,
    mut start_line: usize,
) -> (u64, usize, Vec<u8>) {
    if bytes.is_empty() {
        return (start_offset, start_line, bytes);
    }

    // 1) Trim leading continuation bytes, counting newlines in the trimmed portion.
    let mut cut = 0usize;
    while cut < bytes.len() && is_utf8_continuation(bytes[cut]) {
        // Note: '\n' (0x0A) is not a continuation byte, so we won't see it here.
        // But for safety, check anyway.
        if bytes[cut] == b'\n' {
            start_line = start_line.saturating_add(1);
        }
        cut = cut.saturating_add(1);
    }
    if cut > 0 {
        bytes.drain(..cut);
        start_offset = start_offset.saturating_add(cut as u64);
    }

    // 2) Trim trailing incomplete sequence.
    trim_incomplete_utf8_tail(&mut bytes);

    (start_offset, start_line, bytes)
}

/// Trim to "reasonable UTF-8 boundaries" without panicking and without mutating interior bytes:
/// - drop leading continuation bytes (and adjust start_offset accordingly)
/// - drop a trailing incomplete UTF-8 sequence (if snapshot ends mid-codepoint)
fn trim_incomplete_utf8_tail(bytes: &mut Vec<u8>) {
    loop {
        if bytes.is_empty() {
            return;
        }

        // Count trailing continuation bytes (0..=3).
        let mut cont = 0usize;
        let mut i = bytes.len();
        while i > 0 && cont < 3 {
            let b = bytes[i - 1];
            if is_utf8_continuation(b) {
                cont = cont.saturating_add(1);
                i = i.saturating_sub(1);
            } else {
                break;
            }
        }

        if i == 0 {
            // All bytes are continuation bytes (invalid boundary); drop all.
            bytes.clear();
            return;
        }

        let lead_idx = i - 1;
        let lead = bytes[lead_idx];

        let expected = match utf8_expected_len(lead) {
            Some(n) => n,
            None => {
                // Not a valid lead byte => we won't try to "fix" it here.
                // Consider it "reasonable enough" (caller can decode lossy).
                return;
            }
        };

        let actual = bytes.len().saturating_sub(lead_idx);

        if actual < expected {
            // Incomplete codepoint at end: drop from lead byte onwards.
            bytes.truncate(lead_idx);
            // New end might still be incomplete (rare), so loop.
            continue;
        }

        // End is not "truncated mid-codepoint".
        return;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{self, Read};

    /// A deterministic reader for tests:
    /// - serves bytes from an internal buffer
    /// - counts how many bytes have been read from it
    /// - can limit max chunk size per read() call (to force partial reads)
    #[derive(Debug)]
    struct CountingReader {
        data: Vec<u8>,
        pos: usize,
        bytes_read_total: usize,
        max_chunk: usize,
    }

    impl CountingReader {
        fn new(data: Vec<u8>) -> Self {
            Self::with_max_chunk(data, usize::MAX)
        }

        fn with_max_chunk(data: Vec<u8>, max_chunk: usize) -> Self {
            Self {
                data,
                pos: 0,
                bytes_read_total: 0,
                max_chunk: max_chunk.max(1),
            }
        }

        fn bytes_read(&self) -> usize {
            self.bytes_read_total
        }
    }

    impl Read for CountingReader {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            if buf.is_empty() {
                return Ok(0);
            }
            if self.pos >= self.data.len() {
                return Ok(0);
            }

            let remaining = self.data.len().saturating_sub(self.pos);
            let n = buf.len().min(self.max_chunk).min(remaining);

            // Defensive (should never be 0 here unless buf.len()==0 or remaining==0).
            if n == 0 {
                return Ok(0);
            }

            buf[..n].copy_from_slice(&self.data[self.pos..self.pos + n]);
            self.pos += n;
            self.bytes_read_total += n;
            Ok(n)
        }
    }

    fn make_ascii_data(len: usize) -> Vec<u8> {
        (0..len).map(|i| b'a' + (i % 26) as u8).collect()
    }

    fn assert_snapshot_offsets_consistent(s: &RecentSnapshot) {
        assert_eq!(s.end_offset, s.start_offset + s.bytes.len() as u64);
    }

    #[test]
    fn empty_input_snapshot_is_empty_and_offsets_zero() {
        let inner = CountingReader::new(Vec::new());
        let mut rr = RingReader::new(inner);

        assert_eq!(rr.offset(), 0);
        assert_eq!(rr.read_ahead_len(), 0);

        let snap = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap);

        assert_eq!(snap.start_offset, 0);
        assert_eq!(snap.end_offset, 0);
        assert!(snap.bytes.is_empty());

        assert_eq!(rr.offset(), 0);
        assert_eq!(rr.read_ahead_len(), 0);
        assert_eq!(rr.inner().bytes_read(), 0);
    }

    #[test]
    fn read_with_empty_buffer_returns_0_and_does_not_touch_inner() {
        let data = make_ascii_data(128);
        let inner = CountingReader::new(data);
        let mut rr = RingReader::new(inner);

        let mut buf = [0u8; 0];
        let n = rr.read(&mut buf).unwrap();
        assert_eq!(n, 0);

        assert_eq!(rr.offset(), 0);
        assert_eq!(rr.read_ahead_len(), 0);
        assert_eq!(rr.inner().bytes_read(), 0);
    }

    #[test]
    fn get_recent_reads_ahead_up_to_max_and_does_not_advance_offset() {
        let data = make_ascii_data(MAX_READ_AHEAD * 3 + 123);
        let inner = CountingReader::with_max_chunk(data.clone(), 7);
        let mut rr = RingReader::new(inner);

        assert_eq!(rr.offset(), 0);
        assert_eq!(rr.read_ahead_len(), 0);

        let snap = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap);

        // Read-ahead happens, but consumer offset must not change.
        assert_eq!(rr.offset(), 0);

        // Stash should be filled to the cap (data is long enough).
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_eq!(rr.inner().bytes_read(), MAX_READ_AHEAD);

        // Ring should contain the read-ahead bytes (ASCII => no UTF trimming).
        assert_eq!(snap.start_offset, 0);
        assert_eq!(snap.bytes.len(), MAX_READ_AHEAD);
        assert_eq!(&snap.bytes[..], &data[..MAX_READ_AHEAD]);
    }

    #[test]
    fn get_recent_does_not_read_more_when_stash_already_full() {
        let data = make_ascii_data(MAX_READ_AHEAD * 2 + 5);
        let inner = CountingReader::with_max_chunk(data.clone(), 5);
        let mut rr = RingReader::new(inner);

        let snap1 = rr.get_recent().unwrap();
        let inner_read1 = rr.inner().bytes_read();
        assert_eq!(inner_read1, MAX_READ_AHEAD);
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_snapshot_offsets_consistent(&snap1);

        let snap2 = rr.get_recent().unwrap();
        let inner_read2 = rr.inner().bytes_read();
        assert_eq!(inner_read2, inner_read1, "inner must not be read again");
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_snapshot_offsets_consistent(&snap2);

        assert_eq!(snap2.bytes, snap1.bytes);
        assert_eq!(snap2.start_offset, snap1.start_offset);
        assert_eq!(snap2.end_offset, snap1.end_offset);
    }

    #[test]
    fn read_drains_stash_before_touching_inner_again() {
        let data = make_ascii_data(MAX_READ_AHEAD * 2 + 77);
        let inner = CountingReader::with_max_chunk(data.clone(), 11);
        let mut rr = RingReader::new(inner);

        rr.get_recent().unwrap();
        assert_eq!(rr.offset(), 0);
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);

        let inner_before = rr.inner().bytes_read();

        let mut first = vec![0u8; 17];
        rr.read_exact(&mut first).unwrap();

        assert_eq!(&first[..], &data[..17]);
        assert_eq!(rr.offset(), 17);
        assert_eq!(
            rr.inner().bytes_read(),
            inner_before,
            "must not read inner while draining stash"
        );
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD - 17);
    }

    #[test]
    fn get_recent_refills_only_the_missing_amount_to_reach_cap() {
        let data = make_ascii_data(MAX_READ_AHEAD * 3 + 10);
        let inner = CountingReader::with_max_chunk(data.clone(), 3);
        let mut rr = RingReader::new(inner);

        rr.get_recent().unwrap();
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_eq!(rr.inner().bytes_read(), MAX_READ_AHEAD);

        let drain = 100usize.min(MAX_READ_AHEAD);
        let mut tmp = vec![0u8; drain];
        rr.read_exact(&mut tmp).unwrap();
        assert_eq!(&tmp[..], &data[..drain]);

        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD - drain);

        let inner_before = rr.inner().bytes_read();
        rr.get_recent().unwrap();
        let inner_after = rr.inner().bytes_read();

        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_eq!(
            inner_after - inner_before,
            drain,
            "must read only the missing bytes"
        );
    }

    #[test]
    fn stream_integrity_with_read_ahead_matches_original_input() {
        let data = make_ascii_data(MAX_READ_AHEAD * 2 + 123);
        let inner = CountingReader::with_max_chunk(data.clone(), 9);
        let mut rr = RingReader::new(inner);

        // Trigger read-ahead
        rr.get_recent().unwrap();
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_eq!(rr.offset(), 0);

        // Read entire stream and compare
        let mut out = Vec::new();
        rr.read_to_end(&mut out).unwrap();

        assert_eq!(out, data);
        assert_eq!(rr.offset(), data.len() as u64);
        assert_eq!(rr.read_ahead_len(), 0);
        assert_eq!(rr.inner().bytes_read(), data.len());
    }

    #[test]
    fn stream_integrity_with_periodic_snapshots_matches_original_input() {
        let data = make_ascii_data(MAX_READ_AHEAD * 3 + 2000);
        let inner = CountingReader::with_max_chunk(data.clone(), 17);
        let mut rr = RingReader::new(inner);

        let mut out = Vec::new();
        let mut buf = vec![0u8; 57];

        // Keep reading; occasionally request snapshots (which may read-ahead).
        while out.len() < data.len() {
            // Snapshot every ~400 bytes produced (best-effort; does not need to align).
            if out.len() % 400 == 0 {
                let snap = rr.get_recent().unwrap();
                assert_snapshot_offsets_consistent(&snap);
                // At any moment, unread read-ahead must never exceed cap.
                assert!(rr.read_ahead_len() <= MAX_READ_AHEAD);
            }

            let n = rr.read(&mut buf).unwrap();
            if n == 0 {
                break;
            }
            out.extend_from_slice(&buf[..n]);
        }

        assert_eq!(out, data);
        assert_eq!(rr.offset(), data.len() as u64);
        assert_eq!(rr.inner().bytes_read(), data.len());
    }

    #[test]
    fn ring_eviction_reports_correct_window_and_offset() {
        let extra = 123usize;
        let total = RING_BUFFER_SIZE + extra;
        let data = make_ascii_data(total);

        let inner = std::io::Cursor::new(data.clone());
        let mut rr = RingReader::new(inner);

        let mut out = Vec::new();
        rr.read_to_end(&mut out).unwrap();
        assert_eq!(out, data);
        assert_eq!(rr.offset(), total as u64);

        let snap = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap);

        assert_eq!(snap.bytes.len(), RING_BUFFER_SIZE);
        assert_eq!(snap.start_offset, extra as u64);
        assert_eq!(snap.end_offset, total as u64);
        assert_eq!(&snap.bytes[..], &data[extra..]);
    }

    #[test]
    fn get_recent_trims_leading_utf8_continuations_and_adjusts_start_offset() {
        let inner = std::io::Cursor::new(Vec::<u8>::new());
        let mut rr = RingReader::new(inner);

        // Leading continuation bytes (0x82, 0xAC) without the preceding lead byte.
        // Then ASCII bytes.
        rr.push_ring_bytes(&[0x82, 0xAC, b'a', b'b'], 100);

        let snap = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap);

        assert_eq!(snap.start_offset, 102);
        assert_eq!(snap.bytes, vec![b'a', b'b']);
        assert_eq!(snap.end_offset, 104);
    }

    #[test]
    fn get_recent_trims_trailing_incomplete_utf8_sequence() {
        let inner = std::io::Cursor::new(Vec::<u8>::new());
        let mut rr = RingReader::new(inner);

        // 'abc' + 0xE2 (lead byte of 3-byte UTF-8 sequence) => incomplete at end.
        rr.push_ring_bytes(&[b'a', b'b', b'c', 0xE2], 5);

        let snap = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap);

        assert_eq!(snap.start_offset, 5);
        assert_eq!(snap.bytes, b"abc".to_vec());
        assert_eq!(snap.end_offset, 8);
    }

    #[test]
    fn snapshot_contains_read_ahead_bytes_and_grows_when_refilled() {
        let data = make_ascii_data(MAX_READ_AHEAD + 10);
        let inner = CountingReader::with_max_chunk(data.clone(), 13);
        let mut rr = RingReader::new(inner);

        // First snapshot reads ahead to cap (or data length if smaller).
        let snap1 = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap1);
        assert_eq!(snap1.bytes.len(), MAX_READ_AHEAD);
        assert_eq!(&snap1.bytes[..], &data[..MAX_READ_AHEAD]);

        // Consume 1 byte from stash.
        let mut one = [0u8; 1];
        rr.read_exact(&mut one).unwrap();
        assert_eq!(one[0], data[0]);
        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD - 1);

        // Next snapshot should read exactly 1 byte to restore read-ahead to cap,
        // and ring should now include that new byte too.
        let snap2 = rr.get_recent().unwrap();
        assert_snapshot_offsets_consistent(&snap2);

        assert_eq!(rr.read_ahead_len(), MAX_READ_AHEAD);
        assert_eq!(snap2.start_offset, 0);
        assert_eq!(snap2.bytes.len(), MAX_READ_AHEAD + 1);
        assert_eq!(&snap2.bytes[..], &data[..MAX_READ_AHEAD + 1]);
    }
}