Skip to content

Commit e6a0ba2

Browse files
committed
Pass a Params reference to the various diff() functions, instead of a long list of arguments
1 parent 0ab824a commit e6a0ba2

File tree

6 files changed

+203
-226
lines changed

6 files changed

+203
-226
lines changed

src/context_diff.rs

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use std::collections::VecDeque;
77
use std::io::Write;
88

9+
use crate::params::Params;
910
use crate::utils::do_write_line;
1011

1112
#[derive(Debug, PartialEq)]
@@ -265,23 +266,18 @@ fn make_diff(
265266
}
266267

267268
#[must_use]
268-
#[allow(clippy::too_many_arguments)]
269-
pub fn diff(
270-
expected: &[u8],
271-
expected_filename: &str,
272-
actual: &[u8],
273-
actual_filename: &str,
274-
context_size: usize,
275-
stop_early: bool,
276-
expand_tabs: bool,
277-
tabsize: usize,
278-
) -> Vec<u8> {
279-
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
280-
let diff_results = make_diff(expected, actual, context_size, stop_early);
269+
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
270+
let mut output = format!(
271+
"*** {0}\t\n--- {1}\t\n",
272+
params.from.to_string_lossy(),
273+
params.to.to_string_lossy()
274+
)
275+
.into_bytes();
276+
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
281277
if diff_results.is_empty() {
282278
return Vec::new();
283279
}
284-
if stop_early {
280+
if params.brief {
285281
return output;
286282
}
287283
for result in diff_results {
@@ -319,19 +315,19 @@ pub fn diff(
319315
match line {
320316
DiffLine::Context(e) => {
321317
write!(output, " ").expect("write to Vec is infallible");
322-
do_write_line(&mut output, &e, expand_tabs, tabsize)
318+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
323319
.expect("write to Vec is infallible");
324320
writeln!(output).unwrap();
325321
}
326322
DiffLine::Change(e) => {
327323
write!(output, "! ").expect("write to Vec is infallible");
328-
do_write_line(&mut output, &e, expand_tabs, tabsize)
324+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
329325
.expect("write to Vec is infallible");
330326
writeln!(output).unwrap();
331327
}
332328
DiffLine::Add(e) => {
333329
write!(output, "- ").expect("write to Vec is infallible");
334-
do_write_line(&mut output, &e, expand_tabs, tabsize)
330+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
335331
.expect("write to Vec is infallible");
336332
writeln!(output).unwrap();
337333
}
@@ -349,19 +345,19 @@ pub fn diff(
349345
match line {
350346
DiffLine::Context(e) => {
351347
write!(output, " ").expect("write to Vec is infallible");
352-
do_write_line(&mut output, &e, expand_tabs, tabsize)
348+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
353349
.expect("write to Vec is infallible");
354350
writeln!(output).unwrap();
355351
}
356352
DiffLine::Change(e) => {
357353
write!(output, "! ").expect("write to Vec is infallible");
358-
do_write_line(&mut output, &e, expand_tabs, tabsize)
354+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
359355
.expect("write to Vec is infallible");
360356
writeln!(output).unwrap();
361357
}
362358
DiffLine::Add(e) => {
363359
write!(output, "+ ").expect("write to Vec is infallible");
364-
do_write_line(&mut output, &e, expand_tabs, tabsize)
360+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
365361
.expect("write to Vec is infallible");
366362
writeln!(output).unwrap();
367363
}
@@ -430,13 +426,13 @@ mod tests {
430426
// We want it to turn the alef into bet.
431427
let diff = diff(
432428
&alef,
433-
"a/alef",
434429
&bet,
435-
&format!("{target}/alef"),
436-
2,
437-
false,
438-
false,
439-
8,
430+
&Params {
431+
from: "a/alef".into(),
432+
to: (&format!("{target}/alef")).into(),
433+
context_count: 2,
434+
..Default::default()
435+
},
440436
);
441437
File::create(&format!("{target}/ab.diff"))
442438
.unwrap()
@@ -511,13 +507,13 @@ mod tests {
511507
// We want it to turn the alef into bet.
512508
let diff = diff(
513509
&alef,
514-
"a/alef_",
515510
&bet,
516-
&format!("{target}/alef_"),
517-
2,
518-
false,
519-
false,
520-
8,
511+
&Params {
512+
from: "a/alef_".into(),
513+
to: (&format!("{target}/alef_")).into(),
514+
context_count: 2,
515+
..Default::default()
516+
},
521517
);
522518
File::create(&format!("{target}/ab_.diff"))
523519
.unwrap()
@@ -595,13 +591,13 @@ mod tests {
595591
// We want it to turn the alef into bet.
596592
let diff = diff(
597593
&alef,
598-
"a/alefx",
599594
&bet,
600-
&format!("{target}/alefx"),
601-
2,
602-
false,
603-
false,
604-
8,
595+
&Params {
596+
from: "a/alefx".into(),
597+
to: (&format!("{target}/alefx")).into(),
598+
context_count: 2,
599+
..Default::default()
600+
},
605601
);
606602
File::create(&format!("{target}/abx.diff"))
607603
.unwrap()
@@ -682,13 +678,13 @@ mod tests {
682678
// We want it to turn the alef into bet.
683679
let diff = diff(
684680
&alef,
685-
"a/alefr",
686681
&bet,
687-
&format!("{target}/alefr"),
688-
2,
689-
false,
690-
false,
691-
8,
682+
&Params {
683+
from: "a/alefr".into(),
684+
to: (&format!("{target}/alefr")).into(),
685+
context_count: 2,
686+
..Default::default()
687+
},
692688
);
693689
File::create(&format!("{target}/abr.diff"))
694690
.unwrap()
@@ -725,17 +721,15 @@ mod tests {
725721
let from = ["a", "b", "c", ""].join("\n");
726722
let to_filename = "bar";
727723
let to = ["a", "d", "c", ""].join("\n");
728-
let context_size: usize = 3;
729724

730725
let diff_full = diff(
731726
from.as_bytes(),
732-
from_filename,
733727
to.as_bytes(),
734-
to_filename,
735-
context_size,
736-
false,
737-
false,
738-
8,
728+
&Params {
729+
from: from_filename.into(),
730+
to: to_filename.into(),
731+
..Default::default()
732+
},
739733
);
740734
let expected_full = [
741735
"*** foo\t",
@@ -756,38 +750,37 @@ mod tests {
756750

757751
let diff_brief = diff(
758752
from.as_bytes(),
759-
from_filename,
760753
to.as_bytes(),
761-
to_filename,
762-
context_size,
763-
true,
764-
false,
765-
8,
754+
&Params {
755+
from: from_filename.into(),
756+
to: to_filename.into(),
757+
brief: true,
758+
..Default::default()
759+
},
766760
);
767761
let expected_brief = ["*** foo\t", "--- bar\t", ""].join("\n");
768762
assert_eq!(diff_brief, expected_brief.as_bytes());
769763

770764
let nodiff_full = diff(
771765
from.as_bytes(),
772-
from_filename,
773766
from.as_bytes(),
774-
to_filename,
775-
context_size,
776-
false,
777-
false,
778-
8,
767+
&Params {
768+
from: from_filename.into(),
769+
to: to_filename.into(),
770+
..Default::default()
771+
},
779772
);
780773
assert!(nodiff_full.is_empty());
781774

782775
let nodiff_brief = diff(
783776
from.as_bytes(),
784-
from_filename,
785777
from.as_bytes(),
786-
to_filename,
787-
context_size,
788-
true,
789-
false,
790-
8,
778+
&Params {
779+
from: from_filename.into(),
780+
to: to_filename.into(),
781+
brief: true,
782+
..Default::default()
783+
},
791784
);
792785
assert!(nodiff_brief.is_empty());
793786
}

src/ed_diff.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use std::io::Write;
77

8+
use crate::params::Params;
89
use crate::utils::do_write_line;
910

1011
#[derive(Debug, PartialEq)]
@@ -109,16 +110,10 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<Mis
109110
Ok(results)
110111
}
111112

112-
pub fn diff(
113-
expected: &[u8],
114-
actual: &[u8],
115-
stop_early: bool,
116-
expand_tabs: bool,
117-
tabsize: usize,
118-
) -> Result<Vec<u8>, DiffError> {
113+
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result<Vec<u8>, DiffError> {
119114
let mut output = Vec::new();
120-
let diff_results = make_diff(expected, actual, stop_early)?;
121-
if stop_early && !diff_results.is_empty() {
115+
let diff_results = make_diff(expected, actual, params.brief)?;
116+
if params.brief && !diff_results.is_empty() {
122117
write!(&mut output, "\0").unwrap();
123118
return Ok(output);
124119
}
@@ -153,7 +148,7 @@ pub fn diff(
153148
if actual == b"." {
154149
writeln!(&mut output, "..\n.\ns/.//\na").unwrap();
155150
} else {
156-
do_write_line(&mut output, actual, expand_tabs, tabsize).unwrap();
151+
do_write_line(&mut output, actual, params.expand_tabs, params.tabsize).unwrap();
157152
writeln!(&mut output).unwrap();
158153
}
159154
}
@@ -168,7 +163,7 @@ mod tests {
168163
use super::*;
169164
use pretty_assertions::assert_eq;
170165
pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
171-
let mut output = diff(expected, actual, false, false, 8)?;
166+
let mut output = diff(expected, actual, &Params::default())?;
172167
writeln!(&mut output, "w {filename}").unwrap();
173168
Ok(output)
174169
}
@@ -177,7 +172,7 @@ mod tests {
177172
fn test_basic() {
178173
let from = b"a\n";
179174
let to = b"b\n";
180-
let diff = diff(from, to, false, false, 8).unwrap();
175+
let diff = diff(from, to, &Params::default()).unwrap();
181176
let expected = ["1c", "b", ".", ""].join("\n");
182177
assert_eq!(diff, expected.as_bytes());
183178
}
@@ -412,18 +407,34 @@ mod tests {
412407
let from = ["a", "b", "c", ""].join("\n");
413408
let to = ["a", "d", "c", ""].join("\n");
414409

415-
let diff_full = diff(from.as_bytes(), to.as_bytes(), false, false, 8).unwrap();
410+
let diff_full = diff(from.as_bytes(), to.as_bytes(), &Params::default()).unwrap();
416411
let expected_full = ["2c", "d", ".", ""].join("\n");
417412
assert_eq!(diff_full, expected_full.as_bytes());
418413

419-
let diff_brief = diff(from.as_bytes(), to.as_bytes(), true, false, 8).unwrap();
414+
let diff_brief = diff(
415+
from.as_bytes(),
416+
to.as_bytes(),
417+
&Params {
418+
brief: true,
419+
..Default::default()
420+
},
421+
)
422+
.unwrap();
420423
let expected_brief = "\0".as_bytes();
421424
assert_eq!(diff_brief, expected_brief);
422425

423-
let nodiff_full = diff(from.as_bytes(), from.as_bytes(), false, false, 8).unwrap();
426+
let nodiff_full = diff(from.as_bytes(), from.as_bytes(), &Params::default()).unwrap();
424427
assert!(nodiff_full.is_empty());
425428

426-
let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true, false, 8).unwrap();
429+
let nodiff_brief = diff(
430+
from.as_bytes(),
431+
from.as_bytes(),
432+
&Params {
433+
brief: true,
434+
..Default::default()
435+
},
436+
)
437+
.unwrap();
427438
assert!(nodiff_brief.is_empty());
428439
}
429440
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod context_diff;
22
pub mod ed_diff;
33
pub mod normal_diff;
4+
pub mod params;
45
pub mod unified_diff;
56
pub mod utils;
67

0 commit comments

Comments
 (0)