use std::error::Error;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::process;
use structopt::StructOpt;
#[structopt(name = "hashgood")]
pub struct Opt {
/// Read the hash from the clipboard
+ #[cfg(feature = "paste")]
#[structopt(short = "p", long = "paste")]
paste: bool,
hash: Option<String>,
}
+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 {
}
/// The method by which one or more hashes were supplied to verify the calculated digest
+#[derive(Debug, PartialEq)]
pub enum VerificationSource {
CommandArgument,
Clipboard,
- RawFile(PathBuf),
- DigestsFile(PathBuf),
+ RawFile(String),
+ DigestsFile(String),
}
/// A complete standalone hash result
}
impl Hash {
- pub fn new(alg: Algorithm, bytes: Vec<u8>, path: &PathBuf) -> Self {
+ pub fn new(alg: Algorithm, bytes: Vec<u8>, path: &Path) -> Self {
// Taking the filename component should always work?
// If not, just fall back to the full path
let filename = match path.file_name() {
}
/// A possible hash to match against. The algorithm is assumed.
+#[derive(Debug, PartialEq)]
pub struct CandidateHash {
bytes: Vec<u8>,
filename: Option<String>,
/// A list of candidate hashes that our input could potentially match. At this point it is
/// assumed that we will be verifying a digest of a particular, single algorithm.
+#[derive(Debug, PartialEq)]
pub struct CandidateHashes {
alg: Algorithm,
hashes: Vec<CandidateHash>,
fn hashgood() -> Result<(), Box<dyn Error>> {
let opt = get_verified_options()?;
let candidates = verify::get_candidate_hashes(&opt)?;
- let input = calculate::get_input_reader(&opt.input)?;
+ let input = calculate::get_input_reader(opt.input.as_path())?;
if let Some(c) = candidates {
// If we have a candidate hash of a particular type, use that specific algorithm
let hashes = calculate::create_digests(&[c.alg], input)?;
fn get_verified_options() -> Result<Opt, String> {
let opt = Opt::from_args();
let hash_methods =
- opt.hash.is_some() as i32 + opt.paste as i32 + opt.hash_file.is_some() as i32;
+ opt.hash.is_some() as i32 + opt.get_paste() 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.paste {
+ if opt.get_paste() {
eprintln!("* paste from clipboard (-p)")
}
if opt.hash_file.is_some() {