Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight diffs #42

Merged
merged 12 commits into from
Dec 25, 2019
35 changes: 20 additions & 15 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -10,17 +10,24 @@ mod r#impl {
use super::Render;
use dissimilar::Chunk;
use std::cmp;
use std::panic;

pub struct Diff<'a> {
pub worth_printing: bool,
expected: &'a str,
actual: &'a str,
diff: Vec<Chunk<'a>>,
}

impl<'a> Diff<'a> {
pub fn compute(expected: &'a str, actual: &'a str) -> Self {
let diff = dissimilar::diff(expected, actual);
pub fn compute(expected: &'a str, actual: &'a str) -> Option<Self> {
if expected.len() + actual.len() > 2048 {
// We don't yet trust the dissimilar crate to work well on large
// inputs.
return None;
}

// Nor on non-ascii inputs.
let diff = panic::catch_unwind(|| dissimilar::diff(expected, actual)).ok()?;

let mut common_len = 0;
for chunk in &diff {
@@ -31,13 +38,15 @@ mod r#impl {

let bigger_len = cmp::max(expected.len(), actual.len());
let worth_printing = 5 * common_len >= 4 * bigger_len;
if !worth_printing {
return None;
}

Diff {
worth_printing,
Some(Diff {
expected,
actual,
diff,
}
})
}

pub fn iter<'i>(&'i self, input: &str) -> impl Iterator<Item = Render<'a>> + 'i {
@@ -57,21 +66,17 @@ mod r#impl {
mod r#impl {
use super::Render;

pub struct Diff {
pub worth_printing: bool,
}
pub enum Diff {}

impl Diff {
pub fn compute(_expected: &str, _actual: &str) -> Self {
Diff {
worth_printing: false,
}
pub fn compute(_expected: &str, _actual: &str) -> Option<Self> {
None
}

pub fn iter(&self, _input: &str) -> impl Iterator<Item = Render<'static>> {
pub fn iter(&self, _input: &str) -> Box<dyn Iterator<Item = Render>> {
let _ = Render::Common;
let _ = Render::Unique;
[].iter()
match *self {}
}
}
}
13 changes: 3 additions & 10 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use crate::error::Error;
use crate::normalize;
use crate::term;

use std::panic;
use std::path::Path;
use std::process::Output;

@@ -131,13 +130,7 @@ pub(crate) fn mismatch(expected: &str, actual: &str) {
println!("mismatch");
term::reset();
println!();
let diff = if expected.len() + actual.len() <= 2048 {
// We don't yet trust the dissimilar crate to work well on large inputs
// or non-ascii inputs.
panic::catch_unwind(|| Diff::compute(expected, actual)).ok()
} else {
None
};
let diff = Diff::compute(expected, actual);
term::bold_color(Blue);
println!("EXPECTED:");
snippet_diff(Blue, expected, diff.as_ref());
@@ -224,7 +217,7 @@ fn snippet_diff(color: Color, content: &str, diff: Option<&Diff>) {
dotted_line();

match diff {
Some(diff) if diff.worth_printing => {
Some(diff) => {
for chunk in diff.iter(content) {
match chunk {
Render::Common(s) => {
@@ -238,7 +231,7 @@ fn snippet_diff(color: Color, content: &str, diff: Option<&Diff>) {
}
}
}
_ => print!("{}", content),
None => print!("{}", content),
}

term::color(color);