use std::path::Path;
use failure::Fallible as Result;
pub trait Hasher {
const NAME: &'static str;
fn hash<P: AsRef<Path>>(path: P) -> Result<String>;
}
pub mod default {
pub use super::sha1::Sha1Hasher as DefaultHasher;
}
pub mod sha1 {
use std::path::Path;
use failure::Fallible as Result;
use sha1::{Sha1, Digest};
use crate::hasher::Hasher;
pub struct Sha1Hasher;
impl Sha1Hasher {
pub fn sha1_hash(s: &str) -> String {
format!("{:x}", Sha1::digest(s.as_bytes())) }
}
impl Hasher for Sha1Hasher {
const NAME : &'static str = "sha1";
fn hash<P: AsRef<Path>>(path: P) -> Result<String> {
Ok(Sha1Hasher::sha1_hash(&::std::fs::read_to_string(path)?))
}
}
}