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

1
Cargo.lock generated
View File

@@ -846,6 +846,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"quick-error",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",

View File

@@ -17,3 +17,4 @@ serde_json = "1.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
hex = "0.4.2" hex = "0.4.2"
sha2 = "0.9.1" sha2 = "0.9.1"
quick-error = "1.2.3"

View File

@@ -11,8 +11,10 @@ impl CommandDefault {
} }
pub fn run(_arg_matches: &ArgMatches) -> CommandError { 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 status: {}", crate::git::check_git_status());
information!("Git hash : {}", crate::git::get_git_hash().unwrap_or_else(|| "<none>".to_owned())); information!("Git hash : {}", crate::git::get_git_hash().unwrap_or_else(|| "<none>".to_owned()));
information!("SHA256 R : {}", hex::encode(crate::util::digest_file_sha256("README.md").unwrap()));
error!("Default command is not implemented!"); error!("Default command is not implemented!");
Ok(()) Ok(())
} }

View File

@@ -82,7 +82,7 @@ pub fn get_git_hash() -> Option<String> {
warn!("Error in get utf8 git rev-parse HEAD: {}", e); warn!("Error in get utf8 git rev-parse HEAD: {}", e);
None None
}, },
Ok(stdout) => Some(stdout), Ok(stdout) => Some(stdout.trim().into()),
} }
} }
} }

View File

@@ -1,4 +1,5 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate quick_error;
//#[macro_use] extern crate lazy_static; //#[macro_use] extern crate lazy_static;
use log::LevelFilter; use log::LevelFilter;
use clap::{ App, ArgMatches }; use clap::{ App, ArgMatches };

View File

@@ -1,5 +1,24 @@
use std::fs;
use sha2::{ Digest, Sha256 }; 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> { pub fn digest_sha256(bs: &[u8]) -> Vec<u8> {
let mut sha256 = Sha256::new(); let mut sha256 = Sha256::new();
sha256.update(bs); sha256.update(bs);