From aad710138f4ab8dd9eea6a4fd13ce5f539fd123f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 2 Aug 2020 01:26:29 +0800 Subject: [PATCH] feat: add custom erorr --- Cargo.lock | 1 + Cargo.toml | 1 + src/cmd_default.rs | 2 ++ src/git.rs | 2 +- src/main.rs | 1 + src/util.rs | 19 +++++++++++++++++++ 6 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index dad271f..e07a965 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -846,6 +846,7 @@ dependencies = [ "lazy_static", "log", "pretty_env_logger", + "quick-error", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index debf287..cbefa9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ serde_json = "1.0" lazy_static = "1.4.0" hex = "0.4.2" sha2 = "0.9.1" +quick-error = "1.2.3" diff --git a/src/cmd_default.rs b/src/cmd_default.rs index c3b671d..447fbc1 100644 --- a/src/cmd_default.rs +++ b/src/cmd_default.rs @@ -11,8 +11,10 @@ impl CommandDefault { } pub fn run(_arg_matches: &ArgMatches) -> CommandError { + information!("Git path : {:?}", crate::git::get_git_base_path()); information!("Git status: {}", crate::git::check_git_status()); information!("Git hash : {}", crate::git::get_git_hash().unwrap_or_else(|| "".to_owned())); + information!("SHA256 R : {}", hex::encode(crate::util::digest_file_sha256("README.md").unwrap())); error!("Default command is not implemented!"); Ok(()) } diff --git a/src/git.rs b/src/git.rs index f66b230..dba2441 100644 --- a/src/git.rs +++ b/src/git.rs @@ -82,7 +82,7 @@ pub fn get_git_hash() -> Option { warn!("Error in get utf8 git rev-parse HEAD: {}", e); None }, - Ok(stdout) => Some(stdout), + Ok(stdout) => Some(stdout.trim().into()), } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5ba7263..d751454 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate log; +#[macro_use] extern crate quick_error; //#[macro_use] extern crate lazy_static; use log::LevelFilter; use clap::{ App, ArgMatches }; diff --git a/src/util.rs b/src/util.rs index e36975c..7b4f10b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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 = Result>; + +pub fn digest_file_sha256(file: &str) -> Result, 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 { let mut sha256 = Sha256::new(); sha256.update(bs);