use sha2::{Digest, Sha256};
use std::{fs::File, io};

pub fn file_hash(filename: &str) -> Result<String, std::io::Error> {
    let mut file = File::open(filename)?;
    let mut hasher = Sha256::new();
    io::copy(&mut file, &mut hasher)?;
    Ok(format!("{:x}", hasher.finalize()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let hash = file_hash("Cargo.toml").unwrap();
        assert_eq!(
            hash,
            "02272c4c973d0bf266b571bf596797f46602ae6796d33df4b4057a170483d8f8"
        );
    }
}
