Skip to content

Commit

Permalink
Format using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Limeth committed Feb 25, 2018
1 parent 050f601 commit e66744d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
56 changes: 29 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,21 @@ patterns as regex patterns, which replaces the basic string comparison.")
};
}

fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet: bool, buffer_writer: Arc<Mutex<BufferWriter>>, patterns: Arc<P>) {
fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches,
quiet: bool,
buffer_writer: Arc<Mutex<BufferWriter>>,
patterns: Arc<P>) {
if patterns.len() <= 0 {
let mut stdout = buffer_writer.lock().unwrap().buffer();
cprintln!(false,
stdout,
Color::Red,
"Please, provide at least one valid pattern.");
buffer_writer.lock().unwrap().print(&stdout).expect("Could not write to stdout.");
buffer_writer
.lock()
.unwrap()
.print(&stdout)
.expect("Could not write to stdout.");
std::process::exit(1);
}

Expand All @@ -158,11 +165,7 @@ fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet:
"Looking for an address matching any of ");
}

cprint!(quiet,
stdout,
Color::Cyan,
"{}",
patterns.len());
cprint!(quiet, stdout, Color::Cyan, "{}", patterns.len());

if patterns.len() <= 1 {
cprint!(quiet, stdout, Color::White, " pattern");
Expand All @@ -175,7 +178,11 @@ fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet:
stdout,
Color::White,
"---------------------------------------------------------------------------------------");
buffer_writer.lock().unwrap().print(&stdout).expect("Could not write to stdout.");
buffer_writer
.lock()
.unwrap()
.print(&stdout)
.expect("Could not write to stdout.");
}

let thread_count = num_cpus::get();
Expand Down Expand Up @@ -251,25 +258,24 @@ fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet:
let mut buffer = buffer_writer.lock().unwrap().buffer();
let mut iterations_per_second =
iterations_this_second.lock().unwrap();
cprint!(quiet,
buffer,
Color::Cyan,
"{}",
*iterations_per_second);
cprint!(quiet, buffer, Color::Cyan, "{}", *iterations_per_second);
cprintln!(quiet, buffer, Color::White, " addresses / second");
*sync_buffer.lock().unwrap() = Some(buffer);
*iterations_per_second = 0;
});
}

'dance:
loop {
'dance: loop {
if *working_threads.lock().unwrap() <= 0 {
break 'dance;
}

if let Some(ref buffer) = *sync_buffer.lock().unwrap() {
buffer_writer.lock().unwrap().print(buffer).expect("Could not write to stdout.");
buffer_writer
.lock()
.unwrap()
.print(buffer)
.expect("Could not write to stdout.");
}

*sync_buffer.lock().unwrap() = None;
Expand All @@ -291,17 +297,9 @@ fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet:
Color::White,
"---------------------------------------------------------------------------------------");
cprint!(quiet, stdout, Color::White, "Found address: ");
cprintln!(quiet,
stdout,
Color::Yellow,
"0x{}",
result.address);
cprintln!(quiet, stdout, Color::Yellow, "0x{}", result.address);
cprint!(quiet, stdout, Color::White, "Generated private key: ");
cprintln!(quiet,
stdout,
Color::Red,
"{}",
result.private_key);
cprintln!(quiet, stdout, Color::Red, "{}", result.private_key);
cprintln!(quiet,
stdout,
Color::White,
Expand All @@ -314,7 +312,11 @@ fn main_pattern_type_selected<P: Patterns + 'static>(matches: ArgMatches, quiet:
stdout,
Color::White,
"---------------------------------------------------------------------------------------");
buffer_writer.lock().unwrap().print(&stdout).expect("Could not write to stdout.");
buffer_writer
.lock()
.unwrap()
.print(&stdout)
.expect("Could not write to stdout.");
}

if quiet {
Expand Down
9 changes: 7 additions & 2 deletions src/patterns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ fn read_patterns(matches: &ArgMatches) -> Vec<String> {
}

fn parse_patterns<P: Pattern>(buffer_writer: Arc<Mutex<BufferWriter>>,
matches: &ArgMatches) -> Vec<P> {
matches: &ArgMatches)
-> Vec<P> {
// TODO: Use rayon (everywhere)
let mut vec: Vec<P> = Vec::new();
let raw_patterns = read_patterns(matches);
Expand All @@ -64,7 +65,11 @@ fn parse_patterns<P: Pattern>(buffer_writer: Arc<Mutex<BufferWriter>>,
Color::White,
"{}",
error);
buffer_writer.lock().unwrap().print(&stdout).expect("Could not write to stdout.");
buffer_writer
.lock()
.unwrap()
.print(&stdout)
.expect("Could not write to stdout.");
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/patterns/regex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ::patterns::{Pattern, Patterns, parse_patterns};
use patterns::{Pattern, Patterns, parse_patterns};
use std::sync::{Arc, Mutex};
use regex::{Regex, RegexBuilder};
use clap::ArgMatches;
Expand Down Expand Up @@ -28,11 +28,8 @@ pub struct RegexPatterns {
}

impl RegexPatterns {
pub fn new(buffer_writer: Arc<Mutex<BufferWriter>>,
matches: &ArgMatches) -> RegexPatterns {
RegexPatterns {
vec: parse_patterns(buffer_writer, matches)
}
pub fn new(buffer_writer: Arc<Mutex<BufferWriter>>, matches: &ArgMatches) -> RegexPatterns {
RegexPatterns { vec: parse_patterns(buffer_writer, matches) }
}
}

Expand Down
30 changes: 16 additions & 14 deletions src/patterns/string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ::{ADDRESS_PATTERN, AddressLengthType};
use ::patterns::{Pattern, Patterns, parse_patterns};
use {ADDRESS_PATTERN, AddressLengthType};
use patterns::{Pattern, Patterns, parse_patterns};
use std::borrow::Borrow;
use std::sync::{Arc, Mutex};
use clap::ArgMatches;
Expand Down Expand Up @@ -29,8 +29,7 @@ pub struct StringPatterns {
}

impl StringPatterns {
pub fn new(buffer_writer: Arc<Mutex<BufferWriter>>,
matches: &ArgMatches) -> StringPatterns {
pub fn new(buffer_writer: Arc<Mutex<BufferWriter>>, matches: &ArgMatches) -> StringPatterns {
let patterns = parse_patterns::<String>(buffer_writer, matches);
let patterns_by_len: Arc<GenericArray<Mutex<Option<Vec<String>>>, AddressLengthType>> = Arc::new(arr![Mutex<Option<Vec<String>>>; Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None), Mutex::new(None)]);

Expand All @@ -44,20 +43,21 @@ impl StringPatterns {
});


let patterns_by_len_borrowed: GenericArray<Mutex<Option<Vec<String>>>, AddressLengthType> = Arc::try_unwrap(patterns_by_len).unwrap_or_else(|_| panic!("Couldn't unwrap petterns."));
let patterns_by_len_borrowed: GenericArray<Mutex<Option<Vec<String>>>,
AddressLengthType> =
Arc::try_unwrap(patterns_by_len)
.unwrap_or_else(|_| panic!("Couldn't unwrap petterns."));
let sorted_vecs = patterns_by_len_borrowed.map(|item| {
let item: Option<Vec<String>> = item.into_inner().unwrap();

item.map(|mut vec| {
vec.sort();
vec.dedup();
vec
})
vec.sort();
vec.dedup();
vec
})
});

StringPatterns {
sorted_vecs,
}
StringPatterns { sorted_vecs }
}
}

Expand All @@ -69,7 +69,8 @@ impl Patterns for StringPatterns {
let pattern_len = index + 1;
let target_address_slice = &address[0..pattern_len];

if vec.binary_search_by(|item| item.as_str().cmp(target_address_slice)).is_ok() {
if vec.binary_search_by(|item| item.as_str().cmp(target_address_slice))
.is_ok() {
return true;
}
}
Expand All @@ -79,7 +80,8 @@ impl Patterns for StringPatterns {
}

fn len(&self) -> usize {
self.sorted_vecs.par_iter()
self.sorted_vecs
.par_iter()
.filter(|opt| opt.is_some())
.map(|opt| opt.as_ref().unwrap().len())
.sum()
Expand Down

0 comments on commit e66744d

Please sign in to comment.