cleanup extend.rs

This commit is contained in:
2024-09-18 16:09:31 +02:00
parent 8f0ba691c0
commit 252ac66234

View File

@@ -1,38 +1,42 @@
use std::{ use std::{
fs::File, fs::File,
io::{self, Read}, io::{self, BufReader, Read},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
pub fn file_blake3(file: &Path) -> std::io::Result<String> { pub fn file_blake3(file: &Path) -> io::Result<String> {
let mut blake3 = blake3::Hasher::new(); let file = File::open(file)?;
let mut file = File::open(file)?; let mut reader = BufReader::new(file);
let mut buffer = [0; 1024]; let mut hasher = blake3::Hasher::new();
let mut buffer = [0; 8192];
loop { loop {
let n = file.read(&mut buffer)?; let bytes_read = reader.read(&mut buffer)?;
if n == 0 { if bytes_read == 0 {
break; break;
} }
blake3.update(&buffer[..n]); hasher.update(&buffer[..bytes_read]);
} }
Ok(blake3.finalize().to_string())
Ok(hasher.finalize().to_hex().to_string())
} }
pub trait Blake3Path { pub trait Blake3Path {
fn get_blake3(&self) -> io::Result<String>; fn get_blake3(&self) -> io::Result<String>;
} }
impl Blake3Path for Path { impl Blake3Path for Path {
fn get_blake3(&self) -> io::Result<String> { fn get_blake3(&self) -> io::Result<String> {
if self.is_dir() { if self.is_dir() {
// The default Error (PermissionDenied) is not very helpful when troubleshooting
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
format!("Path is a directory ({})", self.cute_path()), format!("Path is a directory: {}", self.cute_path()),
)); ));
} }
file_blake3(self) file_blake3(self)
} }
} }
impl Blake3Path for PathBuf { impl Blake3Path for PathBuf {
fn get_blake3(&self) -> io::Result<String> { fn get_blake3(&self) -> io::Result<String> {
self.as_path().get_blake3() self.as_path().get_blake3()
@@ -42,11 +46,13 @@ impl Blake3Path for PathBuf {
pub trait CutePath { pub trait CutePath {
fn cute_path(&self) -> String; fn cute_path(&self) -> String;
} }
impl CutePath for Path { impl CutePath for Path {
fn cute_path(&self) -> String { fn cute_path(&self) -> String {
self.to_str().unwrap().replace('\\', "/") self.to_string_lossy().replace('\\', "/")
} }
} }
impl CutePath for PathBuf { impl CutePath for PathBuf {
fn cute_path(&self) -> String { fn cute_path(&self) -> String {
self.as_path().cute_path() self.as_path().cute_path()