mirror of https://github.com/Wilfred/difftastic/
Define a separate files.rs for reading and content checking
parent
ce2d6dee54
commit
aff0131ecb
@ -0,0 +1,34 @@
|
||||
use std::fs;
|
||||
|
||||
pub fn read_or_die(path: &str) -> Vec<u8> {
|
||||
match fs::read(path) {
|
||||
Ok(src) => src,
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
std::io::ErrorKind::NotFound => {
|
||||
eprintln!("No such file: {}", path);
|
||||
}
|
||||
std::io::ErrorKind::PermissionDenied => {
|
||||
eprintln!("Permission denied when reading file: {}", path);
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Could not read file: {} (error {:?})", path, e.kind());
|
||||
}
|
||||
};
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Do these bytes look like a binary (non-textual) format?
|
||||
pub fn is_probably_binary(bytes: &[u8]) -> bool {
|
||||
// If more than 20 of the first 1,000 characters are not valid
|
||||
// UTF-8, we assume it's binary.
|
||||
let num_replaced = String::from_utf8_lossy(bytes)
|
||||
.to_string()
|
||||
.chars()
|
||||
.take(1000)
|
||||
.filter(|c| *c == std::char::REPLACEMENT_CHARACTER)
|
||||
.count();
|
||||
num_replaced > 20
|
||||
}
|
||||
Loading…
Reference in New Issue