@@ -68,6 +68,25 @@ pub enum NewlineSeparator<'a> {
68
68
AfterHeaderAndWhenNeeded ( & ' a str ) ,
69
69
}
70
70
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
+
71
90
/// A utility trait for use in [`UnifiedDiffSink`](super::UnifiedDiffSink).
72
91
pub trait ConsumeTypedHunk {
73
92
/// TODO:
@@ -79,15 +98,7 @@ pub trait ConsumeTypedHunk {
79
98
/// How do we want to pass the header to `consume_hunk`? We can add an additional parameter
80
99
/// similar to `ConsumeHunk::consume_hunk` or add `DiffLineType::Header` in which case we
81
100
/// 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 < ( ) > ;
91
102
92
103
/// Called when processing is complete.
93
104
fn finish ( self ) -> Self :: Out ;
@@ -129,7 +140,7 @@ pub(super) mod _impl {
129
140
use imara_diff:: { intern, Sink } ;
130
141
use intern:: { InternedInput , Interner , Token } ;
131
142
132
- use super :: { ConsumeHunk , ConsumeTypedHunk , ContextSize , DiffLineType , NewlineSeparator } ;
143
+ use super :: { ConsumeHunk , ConsumeTypedHunk , ContextSize , DiffLineType , HunkHeader , NewlineSeparator } ;
133
144
134
145
/// A [`Sink`] that creates a unified diff and processes it hunk-by-hunk with structured type information.
135
146
pub struct UnifiedDiffSink < ' a , T , D >
@@ -254,14 +265,14 @@ pub(super) mod _impl {
254
265
. map ( |( line_type, content) | ( * line_type, content. as_slice ( ) ) )
255
266
. collect ( ) ;
256
267
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 ) ?;
265
276
266
277
self . reset_hunks ( ) ;
267
278
Ok ( ( ) )
@@ -405,27 +416,26 @@ pub(super) mod _impl {
405
416
impl < D : ConsumeHunk > ConsumeTypedHunk for UnifiedDiff < ' _ , D > {
406
417
type Out = D :: Out ;
407
418
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 < ( ) > {
417
420
self . buffer . clear ( ) ;
418
421
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
+
419
429
for & ( line_type, content) in lines {
420
430
self . format_line ( line_type, content) ;
421
431
}
422
432
423
433
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 ,
429
439
& self . buffer ,
430
440
)
431
441
}
@@ -468,19 +478,15 @@ pub(super) mod _impl {
468
478
}
469
479
}
470
480
481
+ // TODO:
482
+ // This is not configurable with respect to how newlines are printed.
471
483
impl ConsumeTypedHunk for String {
472
484
type Out = Self ;
473
485
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
+
484
490
for & ( line_type, content) in lines {
485
491
self . push ( line_type. to_prefix ( ) ) ;
486
492
// TODO:
@@ -496,19 +502,15 @@ pub(super) mod _impl {
496
502
}
497
503
}
498
504
505
+ // TODO:
506
+ // This is not configurable with respect to how newlines are printed.
499
507
impl ConsumeTypedHunk for Vec < u8 > {
500
508
type Out = Self ;
501
509
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
+
512
514
for & ( line_type, content) in lines {
513
515
self . push ( line_type. to_byte_prefix ( ) ) ;
514
516
self . extend_from_slice ( content) ;
0 commit comments