switch from prettydiff to diff

prettydiff has a weird chain of dependencies starting with
prettytable-rs and ending up with (among other things) argon2rs.
This commit is contained in:
Scott Lamb
2021-10-27 10:17:41 -07:00
parent 884c3333cc
commit 4f22cf66e3
4 changed files with 61 additions and 171 deletions

View File

@@ -7,7 +7,6 @@
//! and for tests of `moonfire-nvr upgrade`.
use failure::Error;
use prettydiff::diff_slice;
use rusqlite::params;
use std::fmt::Write;
@@ -55,6 +54,35 @@ impl std::fmt::Display for IndexColumn {
}
}
/// If `slice1` and `slice2` differ, return differences in roughly unified diff form.
fn diff_slices<T: std::fmt::Display + PartialEq>(
name1: &str,
slice1: &[T],
name2: &str,
slice2: &[T],
) -> Option<String> {
let mut diff = format!("--- {}\n+++ {}\n", name1, name2);
let mut changed = false;
for item in diff::slice(slice1, slice2) {
match item {
diff::Result::Left(i) => {
changed = true;
write!(&mut diff, "-{}\n", i)
}
diff::Result::Both(i, _) => write!(&mut diff, " {}\n", i),
diff::Result::Right(i) => {
changed = true;
write!(&mut diff, "+{}\n", i)
}
}
.unwrap();
}
if !changed {
return None;
}
Some(diff)
}
/// Returns a sorted vec of table names in the given connection.
fn get_tables(c: &rusqlite::Connection) -> Result<Vec<String>, rusqlite::Error> {
c.prepare(
@@ -139,13 +167,11 @@ pub fn get_diffs(
// Compare table list.
let tables1 = get_tables(c1)?;
let tables2 = get_tables(c2)?;
if tables1 != tables2 {
if let Some(diff) = diff_slices(n1, &tables1[..], n2, &tables2[..]) {
write!(
&mut diffs,
"table list mismatch, {} vs {}:\n{}",
n1,
n2,
diff_slice(&tables1, &tables2)
n1, n2, diff
)?;
}
@@ -153,14 +179,11 @@ pub fn get_diffs(
for t in &tables1 {
let columns1 = get_table_columns(c1, &t)?;
let columns2 = get_table_columns(c2, &t)?;
if columns1 != columns2 {
if let Some(diff) = diff_slices(n1, &columns1[..], n2, &columns2[..]) {
write!(
&mut diffs,
"table {:?} column, {} vs {}:\n{}",
t,
n1,
n2,
diff_slice(&columns1, &columns2)
t, n1, n2, diff
)?;
}
@@ -168,29 +191,22 @@ pub fn get_diffs(
let mut indices2 = get_indices(c2, &t)?;
indices1.sort_by(|a, b| a.name.cmp(&b.name));
indices2.sort_by(|a, b| a.name.cmp(&b.name));
if indices1 != indices2 {
if let Some(diff) = diff_slices(n1, &indices1[..], n2, &indices2[..]) {
write!(
&mut diffs,
"table {:?} indices, {} vs {}:\n{}",
t,
n1,
n2,
diff_slice(&indices1, &indices2)
t, n1, n2, diff
)?;
}
for i in &indices1 {
let ic1 = get_index_columns(c1, &i.name)?;
let ic2 = get_index_columns(c2, &i.name)?;
if ic1 != ic2 {
if let Some(diff) = diff_slices(n1, &ic1[..], n2, &ic2[..]) {
write!(
&mut diffs,
"table {:?} index {:?} columns {} vs {}:\n{}",
t,
i,
n1,
n2,
diff_slice(&ic1, &ic2)
t, i, n1, n2, diff
)?;
}
}