diff --git a/.gitignore b/.gitignore index fccf2be..bdc4116 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk .criterion +sbocco.sh diff --git a/Cargo.lock b/Cargo.lock index 6a43491..52ffeea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -408,6 +408,7 @@ dependencies = [ "crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -519,6 +520,25 @@ name = "strsim" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "structopt" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt-derive 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "structopt-derive" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.14.9" @@ -710,6 +730,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_json 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "d30ec34ac923489285d24688c7a9c0898d16edff27fc1f1bd854edeff6ca3b7f" "checksum simplelog 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "24b615b1a3cc51ffa565d9a1d0cfcc49fe7d64737ada84eca284cddb0292d125" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8e9ad6a11096cbecdcca0cc6aa403fdfdbaeda2fb3323a39c98e6a166a1e45a" +"checksum structopt-derive 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4cbce8ccdc62166bd594c14396a3242bf94c337a51dbfa9be1076dd74b3db2af" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9056ebe7f2d6a38bc63171816fd1d3430da5a43896de21676dc5c0a4b8274a11" "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" diff --git a/Cargo.toml b/Cargo.toml index e3cdb28..2ca17c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ authors = ["bic "] crypto-hash = "0.3.1" rayon = "1.0.2" memmap = "0.7.0" +structopt = "0.2.10" [dev-dependencies] criterion = "0.1.2" diff --git a/benches/sbocco.rs b/benches/sbocco.rs index 22b9d48..7c4459c 100644 --- a/benches/sbocco.rs +++ b/benches/sbocco.rs @@ -4,10 +4,17 @@ extern crate rainbowcazzi; use criterion::Criterion; use rainbowcazzi::rainbow; +use std::fs::File; fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("sbocco", |b| b.iter(|| rainbow("assets/sbocco"))); - c.bench_function("sbocchino", |b| b.iter(|| rainbow("assets/sbocchino"))); + let infile = File::open("assets/sbocchino").unwrap(); + c.bench_function("sbocchino", |b| b.iter(|| rainbow(&infile, true))); + + let infile = File::open("assets/sbocco").unwrap(); + c.bench_function("sbocco", |b| b.iter(|| rainbow(&infile, true))); + + let infile = File::open("assets/sboccone").unwrap(); + c.bench_function("sbocchino", |b| b.iter(|| rainbow(&infile, true))); } criterion_group!(benches, criterion_benchmark); diff --git a/src/lib.rs b/src/lib.rs index 727fdac..e460b43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,21 @@ extern crate crypto_hash; -extern crate rayon; extern crate memmap; +extern crate rayon; -use crypto_hash::{Algorithm, hex_digest}; -use std::fs::File; +use crypto_hash::{hex_digest, Algorithm}; use memmap::MmapOptions; use rayon::prelude::*; +use std::fs::File; - -pub fn rainbow(file: &str) { - let file = File::open(file).unwrap(); - let mmap = unsafe { MmapOptions::new().map(&file).unwrap() }; +pub fn rainbow(infile: &File, quiet: bool) { + let mmap = unsafe { MmapOptions::new().map(&infile).unwrap() }; mmap.par_split(|c| *c == '\n' as u8) .map(|coso| { - let _hash = hex_digest(Algorithm::MD5, coso); - // println!("{}", _hash); - }).collect::<()>(); + let hash = hex_digest(Algorithm::MD5, coso); + if !quiet { + println!("{}", hash); + } + }) + .collect::<()>(); } diff --git a/src/main.rs b/src/main.rs index 9813730..932c0d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,22 @@ extern crate rainbowcazzi; +extern crate structopt; use rainbowcazzi::rainbow; +use std::fs::File; +use structopt::StructOpt; -fn main() { - rainbow("assets/sbocco"); +#[derive(StructOpt, Debug)] +struct Args { + #[structopt(short = "q", long = "quiet")] + quiet: bool, + #[structopt(name = "input")] + input: String, } +fn main() { + let args = Args::from_args(); + + let infile = File::open(args.input).unwrap(); + + rainbow(&infile, args.quiet); +}