use std::fs;
use std::io;
use std::io::{BufReader, Read};
use std::path::Path;
use similar_asserts;
pub type Differ = Box<dyn Fn(&Path, &Path) + Send + Sync>;
pub fn text_diff(old: &Path, new: &Path) {
let old_str = String::from_utf8(check_io(fs::read(old), "reading file", old));
let new_str = String::from_utf8(check_io(fs::read(new), "reading file", new));
match (old_str, new_str) {
(Ok(old_str), Ok(new_str)) => {
similar_asserts::assert_eq!(&old_str, &new_str, "{}", old.display())
}
_ => binary_diff(old, new),
}
}
pub fn binary_diff(old: &Path, new: &Path) {
let old_len = file_len(old);
let new_len = file_len(new);
if old_len != new_len {
panic!(
"File sizes differ: Old file is {} bytes, new file is {} bytes",
old_len, new_len
);
}
let first_difference = file_byte_iter(old)
.zip(file_byte_iter(new))
.position(|(old_byte, new_byte)| old_byte != new_byte);
if let Some(position) = first_difference {
panic!("{}: Files differ at byte {}", old.display(), position + 1);
}
}
fn open_file(path: &Path) -> fs::File {
check_io(fs::File::open(path), "opening file", path)
}
fn file_byte_iter(path: &Path) -> impl Iterator<Item = u8> + '_ {
BufReader::new(open_file(path))
.bytes()
.map(move |b| check_io(b, "reading file", path))
}
fn file_len(path: &Path) -> u64 {
check_io(fs::metadata(path), "getting file length", path).len()
}
fn check_io<T>(x: Result<T, io::Error>, message: &str, path: &Path) -> T {
x.unwrap_or_else(|err| panic!("Error {} {:?}: {:?}", message, path, err))
}