feat: add custom erorr

This commit is contained in:
2020-08-02 01:26:29 +08:00
parent 67b8311400
commit aad710138f
6 changed files with 25 additions and 1 deletions

View File

@@ -1,5 +1,24 @@
use std::fs;
use sha2::{ Digest, Sha256 };
quick_error! {
#[derive(Debug)]
pub enum DigestError {
FileOpenError(f: String, m: String) {
display("Read file: {}, failed: {}", f, m)
}
}
}
// pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
pub fn digest_file_sha256(file: &str) -> Result<Vec<u8>, DigestError> {
let bs = match fs::read(file) {
Ok(bs) => bs, Err(e) => return Err(DigestError::FileOpenError(file.into(), e.to_string())),
};
Ok(digest_sha256(&bs))
}
pub fn digest_sha256(bs: &[u8]) -> Vec<u8> {
let mut sha256 = Sha256::new();
sha256.update(bs);