Skip to content

Commit 6eb1f5c

Browse files
committed
Add more comments to code
1 parent f43cc97 commit 6eb1f5c

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/lib.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
3838
let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect();
3939
let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect();
4040

41-
let expected_lines_count = (expected_lines.len() as u32).wrapping_sub(1);
42-
let actual_lines_count = (actual_lines.len() as u32).wrapping_sub(1);
41+
debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1);
42+
// ^ means that underflow here is impossible
43+
let expected_lines_count = expected_lines.len() as u32 - 1;
44+
let actual_lines_count = actual_lines.len() as u32 - 1;
4345

4446
if expected_lines.last() == Some(&&b""[..]) {
4547
expected_lines.pop();
@@ -68,14 +70,16 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
6870
mismatch.lines.pop();
6971
match mismatch.lines.pop() {
7072
Some(DiffLine::Resulting(res)) => {
73+
// We have to make sure that Resulting (the + lines)
74+
// always come after Expected (the - lines)
7175
mismatch.lines.push(DiffLine::Expected(str.to_vec()));
7276
if line_number > expected_lines_count {
7377
mismatch.lines.push(DiffLine::MissingNL)
7478
}
7579
mismatch.lines.push(DiffLine::Resulting(res));
7680
mismatch.lines.push(DiffLine::MissingNL);
7781
}
78-
_ => unreachable!(),
82+
_ => unreachable!("unterminated Left and Common lines shouldn't be followed by more Left lines"),
7983
}
8084
} else {
8185
mismatch.lines.push(DiffLine::Expected(str.to_vec()));
@@ -100,19 +104,7 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
100104
mismatch.lines.push(DiffLine::Context(line.to_vec()));
101105
}
102106

103-
if mismatch.lines.last() == Some(&DiffLine::MissingNL) {
104-
mismatch.lines.pop();
105-
match mismatch.lines.pop() {
106-
Some(DiffLine::Expected(exp)) => {
107-
mismatch.lines.push(DiffLine::Expected(exp));
108-
mismatch.lines.push(DiffLine::MissingNL);
109-
mismatch.lines.push(DiffLine::Resulting(str.to_vec()));
110-
}
111-
_ => unreachable!(),
112-
}
113-
} else {
114-
mismatch.lines.push(DiffLine::Resulting(str.to_vec()));
115-
}
107+
mismatch.lines.push(DiffLine::Resulting(str.to_vec()));
116108
if line_number_resulting > actual_lines_count {
117109
mismatch.lines.push(DiffLine::MissingNL)
118110
}
@@ -181,19 +173,25 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
181173
if expected_lines_count != expected_lines.len() as u32 {
182174
mismatch
183175
.lines
184-
.push(DiffLine::Expected(expected_lines.pop().unwrap().to_vec()));
176+
.push(DiffLine::Expected(expected_lines.pop().expect("if expected is totally empty, then `expected_lines_count == expected_lines.len()`").to_vec()));
185177
mismatch.lines.push(DiffLine::MissingNL);
186-
mismatch
187-
.lines
188-
.push(DiffLine::Resulting(actual_lines.pop().unwrap().to_vec()));
178+
mismatch.lines.push(DiffLine::Resulting(
179+
actual_lines
180+
.pop()
181+
.expect("if expect is not empty and actual is, then `results.len() != 0`")
182+
.to_vec(),
183+
));
189184
results.push(mismatch);
190185
} else if actual_lines_count != actual_lines.len() as u32 {
186+
mismatch.lines.push(DiffLine::Expected(
187+
expected_lines
188+
.pop()
189+
.expect("if expect is not empty and actual is, then `results.len() != 0`")
190+
.to_vec(),
191+
));
191192
mismatch
192193
.lines
193-
.push(DiffLine::Expected(expected_lines.pop().unwrap().to_vec()));
194-
mismatch
195-
.lines
196-
.push(DiffLine::Resulting(actual_lines.pop().unwrap().to_vec()));
194+
.push(DiffLine::Resulting(actual_lines.pop().expect("if actual is totally empty, then `actual_lines_count == actual_lines.len()`").to_vec()));
197195
mismatch.lines.push(DiffLine::MissingNL);
198196
results.push(mismatch);
199197
}
@@ -240,26 +238,27 @@ pub fn diff(
240238
"@@ -{},{} +{},{} @@",
241239
line_number, expected_count, line_number_resulting, resulting_count
242240
)
243-
.unwrap();
241+
.expect("write to Vec is infallible");
244242
for line in result.lines {
245243
match line {
246244
DiffLine::Expected(e) => {
247-
write!(output, "-").unwrap();
248-
output.write_all(&e).unwrap();
245+
write!(output, "-").expect("write to Vec is infallible");
246+
output.write_all(&e).expect("write to Vec is infallible");
249247
writeln!(output).unwrap();
250248
}
251249
DiffLine::Context(c) => {
252-
write!(output, " ").unwrap();
253-
output.write_all(&c).unwrap();
250+
write!(output, " ").expect("write to Vec is infallible");
251+
output.write_all(&c).expect("write to Vec is infallible");
254252
writeln!(output).unwrap();
255253
}
256254
DiffLine::Resulting(r) => {
257-
write!(output, "+",).unwrap();
258-
output.write_all(&r).unwrap();
255+
write!(output, "+",).expect("write to Vec is infallible");
256+
output.write_all(&r).expect("write to Vec is infallible");
259257
writeln!(output).unwrap();
260258
}
261259
DiffLine::MissingNL => {
262-
writeln!(output, r"\ No newline at end of file").unwrap();
260+
writeln!(output, r"\ No newline at end of file")
261+
.expect("write to Vec is infallible");
263262
}
264263
}
265264
}

0 commit comments

Comments
 (0)