@@ -24,10 +24,32 @@ pub struct FileDiff<'a> {
2424 file2 : & ' a mut FileData < ' a > ,
2525 hunks : Hunks ,
2626 format_options : & ' a FormatOptions ,
27- pub are_different : bool ,
27+ are_different : bool ,
2828}
2929
3030impl < ' 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+
3153 pub fn file_diff (
3254 path1 : PathBuf ,
3355 path2 : PathBuf ,
@@ -63,13 +85,7 @@ impl<'a> FileDiff<'a> {
6385 let mut file1 = FileData :: get_file ( path1, lines1, ends_with_newline1) ?;
6486 let mut file2 = FileData :: get_file ( path2, lines2, ends_with_newline2) ?;
6587
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) ;
7389
7490 // histogram diff
7591 let mut lcs_indices: Vec < i32 > = vec ! [ -1 ; diff. file1. lines( ) . len( ) ] ;
@@ -89,10 +105,10 @@ impl<'a> FileDiff<'a> {
89105 . create_hunks_from_lcs ( & lcs_indices, num_lines1, num_lines2) ;
90106
91107 if diff. hunks . hunk_count ( ) > 0 {
92- diff. are_different = true ;
108+ diff. set_different ( true ) ;
93109 }
94110
95- if diff. are_different {
111+ if diff. are_different ( ) {
96112 if let Some ( show_if_different) = show_if_different {
97113 println ! ( "{}" , show_if_different) ;
98114 }
@@ -200,7 +216,7 @@ impl<'a> FileDiff<'a> {
200216 }
201217 }
202218
203- if self . are_different {
219+ if self . are_different ( ) {
204220 Ok ( DiffExitStatus :: Different )
205221 } else {
206222 Ok ( DiffExitStatus :: NotDifferent )
@@ -315,7 +331,7 @@ impl<'a> FileDiff<'a> {
315331 Self :: get_header( self . file2, self . format_options. label2( ) )
316332 ) ;
317333
318- let mut diff_disp = ContextDiffDisplay :: default ( ) ;
334+ let mut diff_disp = ContextDiffDisplay :: new ( ) ;
319335
320336 for hunk in self . hunks . hunks ( ) {
321337 // move cursor to the start of context for first hunk
@@ -442,7 +458,7 @@ impl<'a> FileDiff<'a> {
442458 Self :: get_header( self . file2, self . format_options. label2( ) )
443459 ) ;
444460
445- let mut diff_disp = UnifiedDiffDisplay :: default ( ) ;
461+ let mut diff_disp = UnifiedDiffDisplay :: new ( ) ;
446462
447463 for hunk in self . hunks . hunks ( ) {
448464 // move cursor to the start of context for first hunk
@@ -510,7 +526,6 @@ impl<'a> FileDiff<'a> {
510526 }
511527}
512528
513- #[ derive( Default ) ]
514529pub struct UnifiedDiffDisplay {
515530 curr_pos1 : usize ,
516531 curr_pos2 : usize ,
@@ -525,6 +540,18 @@ pub struct UnifiedDiffDisplay {
525540}
526541
527542impl 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+
528555 pub fn write_line (
529556 & mut self ,
530557 file : & FileData ,
@@ -580,7 +607,6 @@ impl UnifiedDiffDisplay {
580607 }
581608}
582609
583- #[ derive( Default ) ]
584610pub struct ContextDiffDisplay {
585611 curr_pos1 : usize ,
586612 curr_pos2 : usize ,
@@ -595,6 +621,18 @@ pub struct ContextDiffDisplay {
595621}
596622
597623impl 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+
598636 pub fn set_context_start ( & mut self ) {
599637 self . context_start1 = self . curr_pos1 + 1 ;
600638 self . context_start2 = self . curr_pos2 + 1 ;
0 commit comments