X-Git-Url: https://code.octet-stream.net/hashgood/blobdiff_plain/3036c7308170f6c68961e44e61e06727eef78327..9ebc8f2b553b02c2bc5ec7de01196844b1a09b30:/src/main.rs diff --git a/src/main.rs b/src/main.rs index f0fc474..ff5a00c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,14 @@ mod display; /// Collect candidate hashes based on options and match them against a calculated hash mod verify; +/// Problem running the program +const EXIT_ERR: i32 = 1; +/// Verification was performed and was not a match +const EXIT_MISMATCH: i32 = 2; + #[derive(StructOpt)] #[structopt(name = "hashgood")] pub struct Opt { - /// Read the hash from the clipboard - #[cfg(feature = "paste")] - #[structopt(short = "p", long = "paste")] - paste: bool, - /// Disable ANSI colours in output #[structopt(short = "C", long = "no-colour")] no_colour: bool, @@ -37,19 +37,6 @@ pub struct Opt { hash: Option, } -impl Opt { - fn get_paste(&self) -> bool { - #[cfg(feature = "paste")] - { - self.paste - } - #[cfg(not(feature = "paste"))] - { - false - } - } -} - /// Types of supported digest algorithm #[derive(Debug, PartialEq, Copy, Clone)] pub enum Algorithm { @@ -74,7 +61,6 @@ impl Algorithm { #[derive(Debug, PartialEq)] pub enum VerificationSource { CommandArgument, - Clipboard, RawFile(String), DigestsFile(String), } @@ -119,6 +105,7 @@ pub struct CandidateHashes { } /// Summary of an atetmpt to match the calculated digest against candidates +#[derive(PartialEq)] pub enum MatchLevel { Ok, Maybe, @@ -147,7 +134,7 @@ pub struct Verification<'a> { fn main() { hashgood().unwrap_or_else(|e| { eprintln!("Error: {}", e); - process::exit(1); + process::exit(EXIT_ERR); }); } @@ -164,6 +151,7 @@ fn hashgood() -> Result<(), Box> { if c.alg == alg { let hash = Hash::new(alg, bytes, &opt.input); let verification = verify::verify_hash(&hash, &c); + let successful_match = verification.match_level == MatchLevel::Ok; display::print_hash( &hash, verification.comparison_hash, @@ -172,6 +160,9 @@ fn hashgood() -> Result<(), Box> { )?; display::print_messages(verification.messages, opt.no_colour)?; display::print_match_level(verification.match_level, opt.no_colour)?; + if !successful_match { + process::exit(EXIT_MISMATCH); + } } } } else { @@ -195,15 +186,11 @@ fn hashgood() -> Result<(), Box> { /// Parse the command line options and check for ambiguous or inconsistent settings fn get_verified_options() -> Result { let opt = Opt::from_args(); - let hash_methods = - opt.hash.is_some() as i32 + opt.get_paste() as i32 + opt.hash_file.is_some() as i32; + let hash_methods = opt.hash.is_some() as i32 + opt.hash_file.is_some() as i32; if hash_methods > 1 { if opt.hash.is_some() { eprintln!("* specified as command line argument"); } - if opt.get_paste() { - eprintln!("* paste from clipboard (-p)") - } if opt.hash_file.is_some() { eprintln!("* check hash from file (-c)") }