From 583d7f1b9c48f36a69e9ef227f78e9421f03266e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Mauduit?= Date: Wed, 2 May 2018 22:57:39 +0200 Subject: [PATCH] Better ! --- src/main.rs | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 382c6df..f281822 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ extern crate clap; extern crate term; extern crate sha2; extern crate walkdir; -extern crate generic_array; use sha2::{Sha256, Digest}; @@ -11,6 +10,7 @@ use std::process; use std::fs; use std::io::Read; use walkdir::WalkDir; +use std::fmt; const BUFFER_SIZE: usize = 1024; @@ -91,17 +91,30 @@ impl Args { } } -struct FileToProcess> { - hash: GenericArray, +struct FileToProcess { + hash: Vec, name: String, realpath: String, } +impl fmt::Display for FileToProcess { + + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[").unwrap(); + for byte in &self.hash { + write!(f, "{:02x}", byte).unwrap(); + } + write!(f, "] - {} ({})", + self.name, + self.realpath) + } +} + impl FileToProcess { /// From https://github.com/RustCrypto/hashes/blob/master/sha2/examples/sha256sum.rs /// Compute digest value for given `Reader` and print it /// On any error simply return without doing anything - fn hash(&self, reader: &mut R, name: &str) { + fn hash(&mut self, reader: &mut R) { let mut sh = D::default(); let mut buffer = [0u8; BUFFER_SIZE]; @@ -115,20 +128,15 @@ impl FileToProcess { break; } } - self.hash = sh.result(); - self.print_result(&sh.result(), name); - } - /// Print digest result as hex string and name pair - fn print_result(&self, sum: &[u8], name: &str) { - for byte in sum { - print!("{:02x}", byte); + for i in sh.result() { + self.hash.push(i); } - println!("\t{}", name); } } fn main() { + let mut files_candidate = vec![]; let mut t = term::stdout().unwrap(); let matches = App::new("rdupe") .version("0.1.0") @@ -196,7 +204,6 @@ fn main() { } // Output - let mut i = 0; for entry in WalkDir::new(&args.output) .into_iter() .filter_map(|e| e.ok()) @@ -207,20 +214,24 @@ fn main() { if ft.is_file() { if let Ok(mut file) = fs::File::open(&entry.path()) { - let a = FileToProcess<32> { - name: format!("[{}] - {}", - i, + let mut a = FileToProcess { + name: format!("{}", entry.path().display()), + hash: vec![], + realpath: String::from("TODO"), }; - a.hash::(&mut file, - &format!("[{}] - {}", - i, - entry.path().display())); - i += 1; + a.hash::(&mut file); + println!("{}", a); + files_candidate.push(a); } } } + for i in files_candidate { + println!("{}", i); + } + + // let inputs = fs::read_dir(&args.input).unwrap(); // for path in inputs { // let path_str = path.unwrap().path().into_os_string().into_string().unwrap();