Skip to content

Commit 18d5cee

Browse files
authored
Merge pull request #333 from fox0/default-diff
diff: #[derive(Default)] and remove getter/setter
2 parents 28ccf7e + 8de1970 commit 18d5cee

File tree

2 files changed

+9
-51
lines changed

2 files changed

+9
-51
lines changed

text/diff_util/file_diff.rs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ pub struct FileDiff<'a> {
2828
}
2929

3030
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-
3931
fn new(
4032
file1: &'a mut FileData<'a>,
4133
file2: &'a mut FileData<'a>,
@@ -44,7 +36,7 @@ impl<'a> FileDiff<'a> {
4436
Self {
4537
file1,
4638
file2,
47-
hunks: Hunks::new(),
39+
hunks: Default::default(),
4840
format_options,
4941
are_different: false,
5042
}
@@ -105,10 +97,10 @@ impl<'a> FileDiff<'a> {
10597
.create_hunks_from_lcs(&lcs_indices, num_lines1, num_lines2);
10698

10799
if diff.hunks.hunk_count() > 0 {
108-
diff.set_different(true);
100+
diff.are_different = true;
109101
}
110102

111-
if diff.are_different() {
103+
if diff.are_different {
112104
if let Some(show_if_different) = show_if_different {
113105
println!("{}", show_if_different);
114106
}
@@ -216,7 +208,7 @@ impl<'a> FileDiff<'a> {
216208
}
217209
}
218210

219-
if self.are_different() {
211+
if self.are_different {
220212
Ok(DiffExitStatus::Different)
221213
} else {
222214
Ok(DiffExitStatus::NotDifferent)
@@ -331,7 +323,7 @@ impl<'a> FileDiff<'a> {
331323
Self::get_header(self.file2, self.format_options.label2())
332324
);
333325

334-
let mut diff_disp = ContextDiffDisplay::new();
326+
let mut diff_disp = ContextDiffDisplay::default();
335327

336328
for hunk in self.hunks.hunks() {
337329
// move cursor to the start of context for first hunk
@@ -458,7 +450,7 @@ impl<'a> FileDiff<'a> {
458450
Self::get_header(self.file2, self.format_options.label2())
459451
);
460452

461-
let mut diff_disp = UnifiedDiffDisplay::new();
453+
let mut diff_disp = UnifiedDiffDisplay::default();
462454

463455
for hunk in self.hunks.hunks() {
464456
// move cursor to the start of context for first hunk
@@ -526,6 +518,7 @@ impl<'a> FileDiff<'a> {
526518
}
527519
}
528520

521+
#[derive(Default)]
529522
pub struct UnifiedDiffDisplay {
530523
curr_pos1: usize,
531524
curr_pos2: usize,
@@ -540,18 +533,6 @@ pub struct UnifiedDiffDisplay {
540533
}
541534

542535
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-
555536
pub fn write_line(
556537
&mut self,
557538
file: &FileData,
@@ -607,6 +588,7 @@ impl UnifiedDiffDisplay {
607588
}
608589
}
609590

591+
#[derive(Default)]
610592
pub struct ContextDiffDisplay {
611593
curr_pos1: usize,
612594
curr_pos2: usize,
@@ -621,18 +603,6 @@ pub struct ContextDiffDisplay {
621603
}
622604

623605
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-
636606
pub fn set_context_start(&mut self) {
637607
self.context_start1 = self.curr_pos1 + 1;
638608
self.context_start2 = self.curr_pos2 + 1;

text/diff_util/hunks.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ impl Default for Hunk {
3333
}
3434

3535
impl Hunk {
36-
pub fn new() -> Self {
37-
Self::default()
38-
}
39-
4036
pub fn f1_range(&self, is_ed: bool) -> String {
4137
if self.ln1_start == self.ln1_end {
4238
format!("{}", self.ln1_start)
@@ -193,20 +189,12 @@ impl Hunk {
193189
}
194190
}
195191

192+
#[derive(Default)]
196193
pub struct Hunks {
197194
hunks: Vec<Hunk>,
198195
}
199196

200197
impl Hunks {
201-
pub fn new() -> Self {
202-
Self {
203-
hunks: {
204-
Hunk::new();
205-
vec![] as Vec<Hunk>
206-
},
207-
}
208-
}
209-
210198
pub fn hunks(&self) -> &Vec<Hunk> {
211199
&self.hunks
212200
}

0 commit comments

Comments
 (0)