Skip to main content

rustik_diff/
decode.rs

1//! Unified diff decoder.
2//!
3//! Streaming [`Decode`] implementations for [`HunkHeader`], [`DiffLine`],
4//! [`Hunk`], [`FileDiff`], and [`DiffDocument`]. Hunks are read by their
5//! header counts (matching radicle-cli's `unified_diff` shape) so callers
6//! can pull a hunk at a time without arbitrary peek-ahead. File and
7//! document boundaries are detected with [`BufRead::fill_buf`] since
8//! rustik-diff has no libgit2 layer to do it for us.
9//!
10//! [`Decode::decode_str`] is a thin string entry point — it just wraps the
11//! input in [`io::Cursor`] and runs the streaming decoder.
12
13use std::io::{self, BufRead};
14use std::path::PathBuf;
15use std::str::FromStr;
16
17use crate::document::{
18    DecodeError, DiffDocument, DiffLine, DiffLineKind, FileDiff, FileHeader, FileId, FileKind,
19    FileMode, Hunk, HunkHeader, HunkId, Oid, RowId,
20};
21
22/// Decodes a value from unified diff text.
23///
24/// The default `decode_str`/`decode_bytes` adapters route through `decode`
25/// so a single [`BufRead`]-based implementation covers all input shapes.
26pub trait Decode: Sized {
27    /// Reads and decodes a value from a buffered input stream.
28    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError>;
29
30    /// Reads and decodes a value, returning `Ok(None)` at clean end-of-input.
31    /// Anything else, including a partial value followed by EOF surfaces as
32    /// `Err`.
33    fn try_decode(input: &mut impl BufRead) -> Result<Option<Self>, DecodeError> {
34        match Self::decode(input) {
35            Ok(value) => Ok(Some(value)),
36            Err(error) if error.is_eof() => Ok(None),
37            Err(error) => Err(error),
38        }
39    }
40
41    /// Decodes a value from a UTF-8 string.
42    fn decode_str(input: &str) -> Result<Self, DecodeError> {
43        Self::decode_bytes(input.as_bytes())
44    }
45
46    /// Decodes a value from raw UTF-8 bytes.
47    fn decode_bytes(input: &[u8]) -> Result<Self, DecodeError> {
48        let mut reader = io::BufReader::new(input);
49        Self::decode(&mut reader)
50    }
51}
52
53impl Decode for HunkHeader {
54    /// Reads a hunk header line from the input stream.
55    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
56        let mut stream = Stream::new(input);
57        let line = stream.read_line()?.ok_or_else(DecodeError::eof)?;
58        HunkHeader::decode_line(&line.text).map_err(|error| error.at_line(line.line_no))
59    }
60}
61
62impl Decode for DiffLine {
63    /// Reads one hunk-body line from the input stream.
64    ///
65    /// The returned [`DiffLine`] uses a zero row id; callers should
66    /// assign a stable ID.
67    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
68        let mut stream = Stream::new(input);
69        let line = stream.read_line()?.ok_or_else(DecodeError::eof)?;
70        line.decode_diff_line(RowId::new(0))
71    }
72}
73
74impl Decode for Hunk {
75    /// Reads a complete hunk from the input stream.
76    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
77        Stream::new(input).decode_hunk()
78    }
79}
80
81impl Decode for FileDiff {
82    /// Reads one file diff from the input stream.
83    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
84        let mut stream = Stream::new(input);
85        if !stream.has_more()? {
86            return Err(DecodeError::eof());
87        }
88        stream.decode_file(FileId::new(0))
89    }
90}
91
92impl Decode for DiffDocument {
93    /// Reads a full diff document from the input stream.
94    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
95        Stream::new(input).decode_document()
96    }
97
98    /// Decodes a diff document from raw bytes.
99    ///
100    /// Non-UTF-8 bytes are replaced with U+FFFD and ANSI escape sequences are
101    /// stripped before parsing, so output from colored Git pipes round-trips
102    /// without caller-side preprocessing.
103    fn decode_bytes(input: &[u8]) -> Result<Self, DecodeError> {
104        if !input.contains(&b'\x1b')
105            && let Ok(text) = std::str::from_utf8(input)
106        {
107            let mut reader = io::BufReader::new(text.as_bytes());
108            return Self::decode(&mut reader);
109        }
110
111        let text = String::from_utf8_lossy(input);
112        let stripped = rustik_tui::ansi::strip(&text);
113        let mut reader = io::BufReader::new(stripped.as_bytes());
114        Self::decode(&mut reader)
115    }
116}
117
118/// Streaming decoder over a buffered input stream.
119///
120/// Stores the reader, a line counter, and per-document ID counters used while
121/// building a [`DiffDocument`]. Each decode call (`Hunk` or `FileDiff`) starts
122/// from a fresh stream, so IDs begin at zero and callers that combine pieces
123/// must renumber as needed.
124struct Stream<R> {
125    /// Buffered source of unified diff text.
126    reader: R,
127    /// Number of physical input lines already consumed.
128    line_no: usize,
129    /// Hunk ID to assign to the next decoded hunk.
130    next_hunk_id: usize,
131    /// Row ID to assign to the next decoded hunk-body line.
132    next_row_id: usize,
133}
134
135/// One physical line read from the stream, with its 1-based line number.
136struct ReadLine {
137    /// 1-based physical input line number.
138    line_no: usize,
139    /// Line contents without trailing line endings.
140    text: String,
141}
142
143impl<R: BufRead> Stream<R> {
144    /// Creates a new streaming decoder.
145    fn new(reader: R) -> Self {
146        Self {
147            reader,
148            line_no: 0,
149            next_hunk_id: 0,
150            next_row_id: 0,
151        }
152    }
153
154    /// Decodes a full document from the remaining input.
155    fn decode_document(&mut self) -> Result<DiffDocument, DecodeError> {
156        let mut commit = Vec::new();
157        while self.has_more()? && !self.peek_starts_with(b"diff --git ")? {
158            let Some(line) = self.read_line()? else {
159                break;
160            };
161            commit.push(line.text);
162        }
163        let commit = commit.join("\n").parse().ok();
164        let mut files = Vec::new();
165
166        while self.has_more()? {
167            let id = FileId::new(files.len());
168            files.push(self.decode_file(id)?);
169        }
170        Ok(DiffDocument { commit, files })
171    }
172
173    /// Decodes one file diff from the remaining input.
174    fn decode_file(&mut self, id: FileId) -> Result<FileDiff, DecodeError> {
175        let line = self.read_line()?.ok_or_else(DecodeError::eof)?;
176        let rest = line
177            .text
178            .strip_prefix("diff --git ")
179            .ok_or_else(|| DecodeError::new(line.line_no, "missing 'diff --git' prefix"))?;
180        let (old_path, new_path) = if let Some((old, new)) = rest.split_once(" b/") {
181            (
182                old.strip_prefix("a/").map(PathBuf::from),
183                new.strip_prefix("b/")
184                    .map(PathBuf::from)
185                    .or_else(|| Some(PathBuf::from(new))),
186            )
187        } else {
188            let (old, new) = rest
189                .split_once(' ')
190                .ok_or_else(|| DecodeError::new(line.line_no, "diff header is missing paths"))?;
191            (
192                Some(PathBuf::from(old.strip_prefix("a/").unwrap_or(old))),
193                Some(PathBuf::from(new.strip_prefix("b/").unwrap_or(new))),
194            )
195        };
196        let mut header = FileHeader {
197            kind: FileKind::Modified,
198            old_path,
199            new_path,
200            old_mode: None,
201            new_mode: None,
202            old_oid: None,
203            new_oid: None,
204            raw: vec![line.text.clone()],
205        };
206        let mut hunks = Vec::new();
207
208        loop {
209            match self.peek_byte()? {
210                None => break,
211                Some(b'@') => hunks.push(self.decode_hunk()?),
212                Some(b'd') if self.peek_starts_with(b"diff --git ")? => break,
213                Some(_) => {
214                    let Some(line) = self.read_line()? else {
215                        return Err(DecodeError::eof());
216                    };
217                    if let Some(mode) = line.text.strip_prefix("new file mode ") {
218                        header.kind = FileKind::Added;
219                        header.new_mode = FileMode::parse(mode);
220                    } else if let Some(mode) = line.text.strip_prefix("deleted file mode ") {
221                        header.kind = FileKind::Deleted;
222                        header.old_mode = FileMode::parse(mode);
223                    } else if let Some(mode) = line.text.strip_prefix("old mode ") {
224                        header.old_mode = FileMode::parse(mode);
225                    } else if let Some(mode) = line.text.strip_prefix("new mode ") {
226                        header.new_mode = FileMode::parse(mode);
227                        if header.kind == FileKind::Modified {
228                            header.kind = FileKind::ModeChanged;
229                        }
230                    } else if let Some(path) = line.text.strip_prefix("rename from ") {
231                        header.kind = FileKind::Renamed;
232                        header.old_path = Some(PathBuf::from(path));
233                    } else if let Some(path) = line.text.strip_prefix("rename to ") {
234                        header.kind = FileKind::Renamed;
235                        header.new_path = Some(PathBuf::from(path));
236                    } else if let Some(path) = line.text.strip_prefix("copy from ") {
237                        header.kind = FileKind::Copied;
238                        header.old_path = Some(PathBuf::from(path));
239                    } else if let Some(path) = line.text.strip_prefix("copy to ") {
240                        header.kind = FileKind::Copied;
241                        header.new_path = Some(PathBuf::from(path));
242                    } else if line.text.starts_with("Binary files ")
243                        || line.text == "GIT binary patch"
244                    {
245                        header.kind = FileKind::Binary;
246                    } else if let Some(rest) = line.text.strip_prefix("index ") {
247                        let mut parts = rest.split_whitespace();
248                        if let Some(oids) = parts.next()
249                            && let Some((old, new)) = oids.split_once("..")
250                        {
251                            let old_oid = Oid::from_str(old).map_err(|_| {
252                                DecodeError::new(line.line_no, "invalid old object id")
253                            })?;
254                            let new_oid = Oid::from_str(new).map_err(|_| {
255                                DecodeError::new(line.line_no, "invalid new object id")
256                            })?;
257                            header.old_oid = Some(old_oid);
258                            header.new_oid = Some(new_oid);
259                        }
260                        if let Some(mode) = parts.next()
261                            && let Some(parsed) = FileMode::parse(mode)
262                        {
263                            header.old_mode.get_or_insert(parsed);
264                            header.new_mode.get_or_insert(parsed);
265                        }
266                    }
267                    header.raw.push(line.text);
268                }
269            }
270        }
271        Ok(FileDiff { id, header, hunks })
272    }
273
274    /// Decodes one hunk from the remaining input.
275    fn decode_hunk(&mut self) -> Result<Hunk, DecodeError> {
276        let line = self.read_line()?.ok_or_else(DecodeError::eof)?;
277        let header =
278            HunkHeader::decode_line(&line.text).map_err(|error| error.at_line(line.line_no))?;
279        let id = HunkId::new(self.next_hunk_id);
280        self.next_hunk_id += 1;
281
282        let mut lines = Vec::new();
283        let mut old_consumed: u32 = 0;
284        let mut new_consumed: u32 = 0;
285
286        loop {
287            if old_consumed >= header.old_size && new_consumed >= header.new_size {
288                while self.peek_byte()? == Some(b'\\') {
289                    let Some(raw) = self.read_line()? else {
290                        return Err(DecodeError::eof());
291                    };
292                    lines.push(raw.decode_diff_line(self.take_row_id())?);
293                }
294                break;
295            }
296            let Some(raw) = self.read_line()? else {
297                return Err(DecodeError::new(
298                    0,
299                    format!(
300                        "expected '{}' old lines and '{}' new lines, but found '{}' and '{}'",
301                        header.old_size, header.new_size, old_consumed, new_consumed
302                    ),
303                ));
304            };
305            let row_id = self.take_row_id();
306            let parsed = raw.decode_diff_line(row_id)?;
307
308            match parsed.kind {
309                DiffLineKind::Context => {
310                    old_consumed += 1;
311                    new_consumed += 1;
312                }
313                DiffLineKind::Addition => new_consumed += 1,
314                DiffLineKind::Deletion => old_consumed += 1,
315                DiffLineKind::NoNewline => {}
316            }
317            lines.push(parsed);
318        }
319        Ok(Hunk { id, header, lines })
320    }
321
322    /// Returns whether unread input remains in the stream.
323    fn has_more(&mut self) -> Result<bool, DecodeError> {
324        Ok(!self.reader.fill_buf()?.is_empty())
325    }
326
327    /// Returns the next byte without consuming it.
328    fn peek_byte(&mut self) -> Result<Option<u8>, DecodeError> {
329        Ok(self.reader.fill_buf()?.first().copied())
330    }
331
332    /// Checks whether the next bytes match a required prefix.
333    fn peek_starts_with(&mut self, prefix: &[u8]) -> Result<bool, DecodeError> {
334        let buf = self.reader.fill_buf()?;
335        Ok(buf.len() >= prefix.len() && &buf[..prefix.len()] == prefix)
336    }
337
338    /// Reads the next line, stripping trailing line endings.
339    fn read_line(&mut self) -> Result<Option<ReadLine>, DecodeError> {
340        let mut buf = String::new();
341        let n = self.reader.read_line(&mut buf)?;
342        if n == 0 {
343            return Ok(None);
344        }
345        self.line_no += 1;
346
347        if buf.ends_with('\n') {
348            buf.pop();
349            if buf.ends_with('\r') {
350                buf.pop();
351            }
352        }
353        Ok(Some(ReadLine {
354            line_no: self.line_no,
355            text: buf,
356        }))
357    }
358
359    /// Returns the next row identifier.
360    fn take_row_id(&mut self) -> RowId {
361        let id = RowId::new(self.next_row_id);
362        self.next_row_id += 1;
363        id
364    }
365}
366
367impl HunkHeader {
368    /// Decodes one unified diff hunk header line like `@@ -A,B +C,D @@ optional text`.
369    fn decode_line(line: &str) -> Result<Self, DecodeError> {
370        let rest = line
371            .strip_prefix("@@ -")
372            .ok_or_else(|| DecodeError::new(0, "hunk header must start with '@@ -'"))?;
373        let (old, rest) = rest
374            .split_once(" +")
375            .ok_or_else(|| DecodeError::new(0, "hunk header is missing new-file range"))?;
376        let (new, text) = rest
377            .split_once(" @@")
378            .ok_or_else(|| DecodeError::new(0, "hunk header is missing closing '@@'"))?;
379        let (old_line_no, old_size) = Self::decode_range(old)?;
380        let (new_line_no, new_size) = Self::decode_range(new)?;
381
382        Ok(Self {
383            old_line_no,
384            old_size,
385            new_line_no,
386            new_size,
387            text: text.strip_prefix(' ').unwrap_or(text).to_owned(),
388        })
389    }
390
391    /// Decodes a unified diff range into a start and size pair.
392    fn decode_range(input: &str) -> Result<(u32, u32), DecodeError> {
393        let (line, size) = input.split_once(',').unwrap_or((input, "1"));
394        Ok((line.parse()?, size.parse()?))
395    }
396}
397
398impl ReadLine {
399    /// Decodes one hunk-body line into a [`DiffLine`] with the given id.
400    fn decode_diff_line(&self, id: RowId) -> Result<DiffLine, DecodeError> {
401        if self.text.starts_with('\\') {
402            if !self.text.starts_with("\\ No newline at end of file") {
403                return Err(DecodeError::new(
404                    self.line_no,
405                    format!("invalid hunk line: {}", self.text),
406                ));
407            }
408            return Ok(DiffLine {
409                id,
410                kind: DiffLineKind::NoNewline,
411                text: String::new(),
412            });
413        }
414        let mut chars = self.text.chars();
415        let marker = chars.next().ok_or_else(|| {
416            DecodeError::new(self.line_no, format!("invalid hunk line: {}", self.text))
417        })?;
418        let text = chars.as_str();
419        let kind = match marker {
420            ' ' => DiffLineKind::Context,
421            '+' => DiffLineKind::Addition,
422            '-' => DiffLineKind::Deletion,
423            _ => {
424                return Err(DecodeError::new(
425                    self.line_no,
426                    format!("invalid hunk line: {}", self.text),
427                ));
428            }
429        };
430        Ok(DiffLine {
431            id,
432            kind,
433            text: text.to_owned(),
434        })
435    }
436}
437
438#[cfg(test)]
439mod tests {
440    use super::*;
441
442    #[test]
443    fn parses_preamble_and_modified_hunk() {
444        let doc = DiffDocument::decode_str(
445            "\
446commit abc
447Author: A
448
449diff --git a/a.rs b/a.rs
450index 1111111..2222222 100644
451--- a/a.rs
452+++ b/a.rs
453@@ -1 +1 @@
454-old
455+new
456\\ No newline at end of file
457",
458        )
459        .expect("diff parses");
460
461        let commit = doc.commit.as_ref().expect("preamble parsed");
462        assert_eq!(commit.hash.as_str(), "abc");
463        assert_eq!(commit.header("Author"), Some("A"));
464        assert!(commit.message.is_empty());
465        assert_eq!(doc.files.len(), 1);
466        assert_eq!(doc.files[0].header.kind, FileKind::Modified);
467        assert_eq!(
468            doc.files[0].header.old_oid.as_ref().map(Oid::as_str),
469            Some("1111111")
470        );
471        assert_eq!(
472            doc.files[0].header.new_oid.as_ref().map(Oid::as_str),
473            Some("2222222")
474        );
475        assert_eq!(doc.files[0].hunks[0].header.old_line_no, 1);
476        assert_eq!(doc.files[0].hunks[0].lines[0].kind, DiffLineKind::Deletion);
477        assert_eq!(doc.files[0].hunks[0].lines[1].kind, DiffLineKind::Addition);
478        assert_eq!(doc.files[0].hunks[0].lines[2].kind, DiffLineKind::NoNewline);
479    }
480
481    #[test]
482    fn parses_added_deleted_renamed_copied_mode_and_binary_files() {
483        let doc = DiffDocument::decode_str(
484            "\
485diff --git a/new.rs b/new.rs
486new file mode 100644
487index 0000000..1111111
488--- /dev/null
489+++ b/new.rs
490@@ -0,0 +1 @@
491+new
492diff --git a/old.rs b/old.rs
493deleted file mode 100644
494index 1111111..0000000
495--- a/old.rs
496+++ /dev/null
497@@ -1 +0,0 @@
498-old
499diff --git a/from.rs b/to.rs
500similarity index 100%
501rename from from.rs
502rename to to.rs
503diff --git a/copy.rs b/copy2.rs
504similarity index 100%
505copy from copy.rs
506copy to copy2.rs
507diff --git a/mode.sh b/mode.sh
508old mode 100644
509new mode 100755
510diff --git a/image.png b/image.png
511index 1111111..2222222 100644
512Binary files a/image.png and b/image.png differ
513",
514        )
515        .expect("diff parses");
516
517        let kinds = doc
518            .files
519            .iter()
520            .map(|file| file.header.kind)
521            .collect::<Vec<_>>();
522        assert_eq!(
523            kinds,
524            vec![
525                FileKind::Added,
526                FileKind::Deleted,
527                FileKind::Renamed,
528                FileKind::Copied,
529                FileKind::ModeChanged,
530                FileKind::Binary,
531            ]
532        );
533        assert_eq!(doc.files[2].header.old_path, Some(PathBuf::from("from.rs")));
534        assert_eq!(doc.files[2].header.new_path, Some(PathBuf::from("to.rs")));
535        assert_eq!(doc.files[4].header.old_mode, Some(FileMode::Blob));
536        assert_eq!(doc.files[4].header.new_mode, Some(FileMode::BlobExecutable));
537    }
538
539    #[test]
540    fn parses_hunk_header_default_sizes() {
541        let header = HunkHeader::decode_line("@@ -7 +8 @@ fn main").expect("header parses");
542
543        assert_eq!(header.old_line_no, 7);
544        assert_eq!(header.old_size, 1);
545        assert_eq!(header.new_line_no, 8);
546        assert_eq!(header.new_size, 1);
547        assert_eq!(header.text, "fn main");
548    }
549
550    #[test]
551    fn decode_via_bufread_matches_zero_copy_parse() {
552        let input = "\
553diff --git a/a b/a
554@@ -1 +1 @@
555-old
556+new
557";
558        let from_str = DiffDocument::decode_str(input).expect("zero-copy parse");
559        let from_bytes = DiffDocument::decode_bytes(input.as_bytes()).expect("bufread decode");
560
561        assert_eq!(from_str, from_bytes);
562    }
563
564    #[test]
565    fn reports_invalid_hunk_lines() {
566        let error = DiffDocument::decode_str(
567            "\
568diff --git a/a b/a
569@@ -1 +1 @@
570?bad
571",
572        )
573        .expect_err("invalid hunk line is rejected");
574
575        assert_eq!(error.line, 3);
576        assert!(error.message.contains("invalid hunk line"));
577        assert!(!error.is_eof());
578    }
579
580    #[test]
581    fn reports_invalid_index_object_ids() {
582        let error = DiffDocument::decode_str(
583            "\
584diff --git a/a b/a
585index not-hex..2222222 100644
586@@ -1 +1 @@
587-old
588+new
589",
590        )
591        .expect_err("invalid object id should be rejected");
592
593        assert_eq!(error.line, 2);
594        assert_eq!(error.message, "invalid old object id");
595    }
596
597    #[test]
598    fn try_decode_hunks_one_at_a_time() {
599        let input = "\
600@@ -1 +1 @@
601-old
602+new
603@@ -10,2 +10,2 @@ ctx
604 a
605-b
606+c
607";
608        let mut reader = io::BufReader::new(input.as_bytes());
609
610        let first = Hunk::try_decode(&mut reader)
611            .expect("decodes")
612            .expect("first hunk present");
613        assert_eq!(first.header.old_line_no, 1);
614        assert_eq!(first.lines.len(), 2);
615
616        let second = Hunk::try_decode(&mut reader)
617            .expect("decodes")
618            .expect("second hunk present");
619        assert_eq!(second.header.old_line_no, 10);
620        assert_eq!(second.header.text, "ctx");
621        assert_eq!(second.lines.len(), 3);
622
623        assert!(
624            Hunk::try_decode(&mut reader).expect("decodes").is_none(),
625            "stream should be drained",
626        );
627    }
628
629    #[test]
630    fn try_decode_returns_none_on_empty_input() {
631        let mut empty = io::BufReader::new(&[][..]);
632        assert!(FileDiff::try_decode(&mut empty).expect("eof").is_none());
633        let mut empty = io::BufReader::new(&[][..]);
634        assert!(Hunk::try_decode(&mut empty).expect("eof").is_none());
635        // An empty stream is a valid (empty) document.
636        let mut empty = io::BufReader::new(&[][..]);
637        assert!(
638            DiffDocument::decode(&mut empty)
639                .expect("decodes empty")
640                .is_empty()
641        );
642    }
643
644    #[test]
645    fn try_decode_distinguishes_eof_from_syntax_error() {
646        let mut bad = io::BufReader::new(&b"@@ not a hunk header\n"[..]);
647        let error = Hunk::try_decode(&mut bad).expect_err("syntax error surfaces");
648        assert!(!error.is_eof());
649    }
650
651    #[test]
652    fn streaming_decode_matches_zero_copy_parse_for_full_document() {
653        let input = "\
654commit abc
655
656diff --git a/a.rs b/a.rs
657index 1111111..2222222 100644
658--- a/a.rs
659+++ b/a.rs
660@@ -1 +1 @@
661-old
662+new
663diff --git a/b.rs b/b.rs
664new file mode 100644
665index 0000000..3333333
666--- /dev/null
667+++ b/b.rs
668@@ -0,0 +1 @@
669+only
670";
671        let zero_copy = DiffDocument::decode_str(input).expect("zero-copy parses");
672        let streamed = {
673            let mut reader = io::BufReader::new(input.as_bytes());
674            DiffDocument::decode(&mut reader).expect("stream decodes")
675        };
676
677        assert_eq!(zero_copy, streamed);
678    }
679
680    #[test]
681    fn raw_bytes_strip_ansi_before_decoding_document() {
682        let input = b"\
683\x1b[33mdiff --git a/a.rs b/a.rs\x1b[m
684\x1b[33mindex 1111111..2222222 100644\x1b[m
685\x1b[33m--- a/a.rs\x1b[m
686\x1b[33m+++ b/a.rs\x1b[m
687\x1b[35m@@ -1 +1 @@\x1b[m
688\x1b[31m-old\x1b[m
689\x1b[32m+new\x1b[m
690";
691        let document = DiffDocument::decode_bytes(input).unwrap();
692
693        assert_eq!(document.files.len(), 1);
694        assert_eq!(document.stats().insertions, 1);
695        assert_eq!(document.stats().deletions, 1);
696    }
697
698    #[test]
699    fn hunk_body_uses_header_counts_to_terminate() {
700        // Body is exactly old_size + new_size = 1 + 2 = 3 line-types.
701        // After the body, a `\ No newline...` marker must still be consumed.
702        let input = "\
703@@ -1 +1,2 @@
704-old
705+new
706+extra
707\\ No newline at end of file
708";
709        let mut reader = io::BufReader::new(input.as_bytes());
710        let hunk = Hunk::decode(&mut reader).expect("decodes");
711
712        assert_eq!(hunk.lines.len(), 4);
713        assert_eq!(hunk.lines[3].kind, DiffLineKind::NoNewline);
714        assert!(reader.fill_buf().expect("fills").is_empty());
715    }
716}