use std::path::Path;
use digest::Digest;
use tokio::{fs::File, io::AsyncReadExt};
#[macro_export]
macro_rules! hash {
($($await:ident)? : $Hasher:ty, $reader:expr $(,$write:ident)?) => {{
let mut hasher = <$Hasher>::new();
let mut reader = $reader;
let mut buf = [0; 16384];
loop {
let n = reader.read(&mut buf)$(.$await)??;
if n == 0 {
break;
}
let bin = &buf[..n];
hasher.update(bin);
$(let _ = $write.write(bin)?;)?
}
hasher
}};
}
pub async fn hash<H: Digest>(path: impl AsRef<Path>) -> Result<H, std::io::Error> {
let file = File::open(path).await?;
Ok(hash!(await: H, file))
}