@@ -68,6 +68,25 @@ pub enum NewlineSeparator<'a> {
6868 AfterHeaderAndWhenNeeded ( & ' a str ) ,
6969}
7070
71+ /// TODO:
72+ /// Document.
73+ pub struct HunkHeader {
74+ before_hunk_start : u32 ,
75+ before_hunk_len : u32 ,
76+ after_hunk_start : u32 ,
77+ after_hunk_len : u32 ,
78+ }
79+
80+ impl std:: fmt:: Display for HunkHeader {
81+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
82+ write ! (
83+ f,
84+ "@@ -{},{} +{},{} @@" ,
85+ self . before_hunk_start, self . before_hunk_len, self . after_hunk_start, self . after_hunk_len
86+ )
87+ }
88+ }
89+
7190/// A utility trait for use in [`UnifiedDiffSink`](super::UnifiedDiffSink).
7291pub trait ConsumeTypedHunk {
7392 /// TODO:
@@ -79,15 +98,7 @@ pub trait ConsumeTypedHunk {
7998 /// How do we want to pass the header to `consume_hunk`? We can add an additional parameter
8099 /// similar to `ConsumeHunk::consume_hunk` or add `DiffLineType::Header` in which case we
81100 /// didn’t have to add an additional parameter.
82- fn consume_hunk (
83- & mut self ,
84- before_hunk_start : u32 ,
85- before_hunk_len : u32 ,
86- after_hunk_start : u32 ,
87- after_hunk_len : u32 ,
88- header : & str ,
89- lines : & [ ( DiffLineType , & [ u8 ] ) ] ,
90- ) -> std:: io:: Result < ( ) > ;
101+ fn consume_hunk ( & mut self , header : HunkHeader , lines : & [ ( DiffLineType , & [ u8 ] ) ] ) -> std:: io:: Result < ( ) > ;
91102
92103 /// Called when processing is complete.
93104 fn finish ( self ) -> Self :: Out ;
@@ -129,7 +140,7 @@ pub(super) mod _impl {
129140 use imara_diff:: { intern, Sink } ;
130141 use intern:: { InternedInput , Interner , Token } ;
131142
132- use super :: { ConsumeHunk , ConsumeTypedHunk , ContextSize , DiffLineType , NewlineSeparator } ;
143+ use super :: { ConsumeHunk , ConsumeTypedHunk , ContextSize , DiffLineType , HunkHeader , NewlineSeparator } ;
133144
134145 /// A [`Sink`] that creates a unified diff and processes it hunk-by-hunk with structured type information.
135146 pub struct UnifiedDiffSink < ' a , T , D >
@@ -254,14 +265,14 @@ pub(super) mod _impl {
254265 . map ( |( line_type, content) | ( * line_type, content. as_slice ( ) ) )
255266 . collect ( ) ;
256267
257- self . delegate . consume_hunk (
258- hunk_start,
259- self . before_hunk_len ,
260- hunk_end,
261- self . after_hunk_len ,
262- & self . header_buf ,
263- & lines ,
264- ) ?;
268+ let header = HunkHeader {
269+ before_hunk_start : hunk_start,
270+ before_hunk_len : self . before_hunk_len ,
271+ after_hunk_start : hunk_end,
272+ after_hunk_len : self . after_hunk_len ,
273+ } ;
274+
275+ self . delegate . consume_hunk ( header , & lines ) ?;
265276
266277 self . reset_hunks ( ) ;
267278 Ok ( ( ) )
@@ -405,27 +416,26 @@ pub(super) mod _impl {
405416 impl < D : ConsumeHunk > ConsumeTypedHunk for UnifiedDiff < ' _ , D > {
406417 type Out = D :: Out ;
407418
408- fn consume_hunk (
409- & mut self ,
410- before_hunk_start : u32 ,
411- before_hunk_len : u32 ,
412- after_hunk_start : u32 ,
413- after_hunk_len : u32 ,
414- header : & str ,
415- lines : & [ ( DiffLineType , & [ u8 ] ) ] ,
416- ) -> std:: io:: Result < ( ) > {
419+ fn consume_hunk ( & mut self , header : HunkHeader , lines : & [ ( DiffLineType , & [ u8 ] ) ] ) -> std:: io:: Result < ( ) > {
417420 self . buffer . clear ( ) ;
418421
422+ // TODO:
423+ // Can we find a better name?
424+ let mut printed_header = header. to_string ( ) ;
425+ printed_header. push_str ( match self . newline {
426+ NewlineSeparator :: AfterHeaderAndLine ( nl) | NewlineSeparator :: AfterHeaderAndWhenNeeded ( nl) => nl,
427+ } ) ;
428+
419429 for & ( line_type, content) in lines {
420430 self . format_line ( line_type, content) ;
421431 }
422432
423433 self . delegate . consume_hunk (
424- before_hunk_start,
425- before_hunk_len,
426- after_hunk_start,
427- after_hunk_len,
428- & header ,
434+ header . before_hunk_start ,
435+ header . before_hunk_len ,
436+ header . after_hunk_start ,
437+ header . after_hunk_len ,
438+ & printed_header ,
429439 & self . buffer ,
430440 )
431441 }
@@ -468,19 +478,15 @@ pub(super) mod _impl {
468478 }
469479 }
470480
481+ // TODO:
482+ // This is not configurable with respect to how newlines are printed.
471483 impl ConsumeTypedHunk for String {
472484 type Out = Self ;
473485
474- fn consume_hunk (
475- & mut self ,
476- _: u32 ,
477- _: u32 ,
478- _: u32 ,
479- _: u32 ,
480- header : & str ,
481- lines : & [ ( DiffLineType , & [ u8 ] ) ] ,
482- ) -> std:: io:: Result < ( ) > {
483- self . push_str ( header) ;
486+ fn consume_hunk ( & mut self , header : HunkHeader , lines : & [ ( DiffLineType , & [ u8 ] ) ] ) -> std:: io:: Result < ( ) > {
487+ self . push_str ( & header. to_string ( ) ) ;
488+ self . push ( '\n' ) ;
489+
484490 for & ( line_type, content) in lines {
485491 self . push ( line_type. to_prefix ( ) ) ;
486492 // TODO:
@@ -496,19 +502,15 @@ pub(super) mod _impl {
496502 }
497503 }
498504
505+ // TODO:
506+ // This is not configurable with respect to how newlines are printed.
499507 impl ConsumeTypedHunk for Vec < u8 > {
500508 type Out = Self ;
501509
502- fn consume_hunk (
503- & mut self ,
504- _: u32 ,
505- _: u32 ,
506- _: u32 ,
507- _: u32 ,
508- header : & str ,
509- lines : & [ ( DiffLineType , & [ u8 ] ) ] ,
510- ) -> std:: io:: Result < ( ) > {
511- self . push_str ( header) ;
510+ fn consume_hunk ( & mut self , header : HunkHeader , lines : & [ ( DiffLineType , & [ u8 ] ) ] ) -> std:: io:: Result < ( ) > {
511+ self . push_str ( header. to_string ( ) ) ;
512+ self . push ( b'\n' ) ;
513+
512514 for & ( line_type, content) in lines {
513515 self . push ( line_type. to_byte_prefix ( ) ) ;
514516 self . extend_from_slice ( content) ;
0 commit comments