use crate::{Hasher, Id};
const BUFFER_LEN: usize = 65_536;
pub async fn identify_input(input: impl futures_io::AsyncRead) -> std::io::Result<Id> {
use futures_util::AsyncReadExt;
futures_util::pin_mut!(input);
let mut hasher = Hasher::new();
let mut buffer = [0u8; BUFFER_LEN];
loop {
match input.read(&mut buffer).await? {
0 => break,
n => hasher.update(&buffer[..n]),
};
}
Ok(Id(hasher.finalize()))
}
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub async fn identify_file(input_path: impl AsRef<std::path::Path>) -> std::io::Result<Id> {
use tokio::io::AsyncReadExt;
let mut input = tokio::fs::File::open(input_path).await?;
let mut hasher = Hasher::new();
let mut buffer = [0u8; BUFFER_LEN];
loop {
match input.read(&mut buffer).await? {
0 => break,
n => hasher.update(&buffer[..n]),
};
}
Ok(Id(hasher.finalize()))
}