@@ -24,10 +24,32 @@ pub struct FileDiff<'a> {
24
24
file2 : & ' a mut FileData < ' a > ,
25
25
hunks : Hunks ,
26
26
format_options : & ' a FormatOptions ,
27
- pub are_different : bool ,
27
+ are_different : bool ,
28
28
}
29
29
30
30
impl < ' a > FileDiff < ' a > {
31
+ pub fn are_different ( & self ) -> bool {
32
+ self . are_different
33
+ }
34
+
35
+ pub fn set_different ( & mut self , are_different : bool ) {
36
+ self . are_different = are_different;
37
+ }
38
+
39
+ fn new (
40
+ file1 : & ' a mut FileData < ' a > ,
41
+ file2 : & ' a mut FileData < ' a > ,
42
+ format_options : & ' a FormatOptions ,
43
+ ) -> Self {
44
+ Self {
45
+ file1,
46
+ file2,
47
+ hunks : Hunks :: new ( ) ,
48
+ format_options,
49
+ are_different : false ,
50
+ }
51
+ }
52
+
31
53
pub fn file_diff (
32
54
path1 : PathBuf ,
33
55
path2 : PathBuf ,
@@ -63,13 +85,7 @@ impl<'a> FileDiff<'a> {
63
85
let mut file1 = FileData :: get_file ( path1, lines1, ends_with_newline1) ?;
64
86
let mut file2 = FileData :: get_file ( path2, lines2, ends_with_newline2) ?;
65
87
66
- let mut diff = FileDiff {
67
- file1 : & mut file1,
68
- file2 : & mut file2,
69
- hunks : Default :: default ( ) ,
70
- format_options,
71
- are_different : Default :: default ( ) ,
72
- } ;
88
+ let mut diff = FileDiff :: new ( & mut file1, & mut file2, format_options) ;
73
89
74
90
// histogram diff
75
91
let mut lcs_indices: Vec < i32 > = vec ! [ -1 ; diff. file1. lines( ) . len( ) ] ;
@@ -89,10 +105,10 @@ impl<'a> FileDiff<'a> {
89
105
. create_hunks_from_lcs ( & lcs_indices, num_lines1, num_lines2) ;
90
106
91
107
if diff. hunks . hunk_count ( ) > 0 {
92
- diff. are_different = true ;
108
+ diff. set_different ( true ) ;
93
109
}
94
110
95
- if diff. are_different {
111
+ if diff. are_different ( ) {
96
112
if let Some ( show_if_different) = show_if_different {
97
113
println ! ( "{}" , show_if_different) ;
98
114
}
@@ -200,7 +216,7 @@ impl<'a> FileDiff<'a> {
200
216
}
201
217
}
202
218
203
- if self . are_different {
219
+ if self . are_different ( ) {
204
220
Ok ( DiffExitStatus :: Different )
205
221
} else {
206
222
Ok ( DiffExitStatus :: NotDifferent )
@@ -315,7 +331,7 @@ impl<'a> FileDiff<'a> {
315
331
Self :: get_header( self . file2, self . format_options. label2( ) )
316
332
) ;
317
333
318
- let mut diff_disp = ContextDiffDisplay :: default ( ) ;
334
+ let mut diff_disp = ContextDiffDisplay :: new ( ) ;
319
335
320
336
for hunk in self . hunks . hunks ( ) {
321
337
// move cursor to the start of context for first hunk
@@ -442,7 +458,7 @@ impl<'a> FileDiff<'a> {
442
458
Self :: get_header( self . file2, self . format_options. label2( ) )
443
459
) ;
444
460
445
- let mut diff_disp = UnifiedDiffDisplay :: default ( ) ;
461
+ let mut diff_disp = UnifiedDiffDisplay :: new ( ) ;
446
462
447
463
for hunk in self . hunks . hunks ( ) {
448
464
// move cursor to the start of context for first hunk
@@ -510,7 +526,6 @@ impl<'a> FileDiff<'a> {
510
526
}
511
527
}
512
528
513
- #[ derive( Default ) ]
514
529
pub struct UnifiedDiffDisplay {
515
530
curr_pos1 : usize ,
516
531
curr_pos2 : usize ,
@@ -525,6 +540,18 @@ pub struct UnifiedDiffDisplay {
525
540
}
526
541
527
542
impl UnifiedDiffDisplay {
543
+ pub fn new ( ) -> Self {
544
+ Self {
545
+ curr_pos1 : 0 ,
546
+ curr_pos2 : 0 ,
547
+ context_start1 : 0 ,
548
+ context_start2 : 0 ,
549
+ hunk1_len : 0 ,
550
+ hunk2_len : 0 ,
551
+ hunk_lines : String :: new ( ) ,
552
+ }
553
+ }
554
+
528
555
pub fn write_line (
529
556
& mut self ,
530
557
file : & FileData ,
@@ -580,7 +607,6 @@ impl UnifiedDiffDisplay {
580
607
}
581
608
}
582
609
583
- #[ derive( Default ) ]
584
610
pub struct ContextDiffDisplay {
585
611
curr_pos1 : usize ,
586
612
curr_pos2 : usize ,
@@ -595,6 +621,18 @@ pub struct ContextDiffDisplay {
595
621
}
596
622
597
623
impl ContextDiffDisplay {
624
+ pub fn new ( ) -> Self {
625
+ Self {
626
+ curr_pos1 : 0 ,
627
+ curr_pos2 : 0 ,
628
+ context_start1 : 0 ,
629
+ context_start2 : 0 ,
630
+ hunk1_len : 0 ,
631
+ hunk2_len : 0 ,
632
+ hunk_lines : [ String :: new ( ) , String :: new ( ) ] ,
633
+ }
634
+ }
635
+
598
636
pub fn set_context_start ( & mut self ) {
599
637
self . context_start1 = self . curr_pos1 + 1 ;
600
638
self . context_start2 = self . curr_pos2 + 1 ;
0 commit comments