-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up code into modules; use reintroduce static dispatch for `Patt…
…erns`
- Loading branch information
Showing
5 changed files
with
257 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
macro_rules! cprintln { | ||
($surpress:expr, $stdout:expr, $fg:expr, $($rest:tt)+) => { | ||
if !$surpress { | ||
use std::io::Write; | ||
use termcolor::{ColorSpec, WriteColor}; | ||
|
||
$stdout.set_color(ColorSpec::new().set_fg(Some($fg))) | ||
.expect("Could not set the text formatting."); | ||
writeln!($stdout, $($rest)+).expect("Could not output text."); | ||
} | ||
} | ||
} | ||
|
||
macro_rules! cprint { | ||
($surpress:expr, $stdout:expr, $fg:expr, $($rest:tt)+) => { | ||
if !$surpress { | ||
use std::io::Write; | ||
use termcolor::{ColorSpec, WriteColor}; | ||
|
||
$stdout.set_color(ColorSpec::new().set_fg(Some($fg))) | ||
.expect("Could not set the text formatting."); | ||
write!($stdout, $($rest)+).expect("Could not output text."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
mod string; | ||
mod regex; | ||
|
||
pub use self::string::*; | ||
pub use self::regex::*; | ||
|
||
use std; | ||
use std::fmt::Display; | ||
use std::io::BufRead; | ||
use std::sync::{Arc, Mutex}; | ||
use clap::ArgMatches; | ||
use termcolor::{BufferWriter, Color}; | ||
|
||
trait Pattern: Display + Send + Sync + Sized { | ||
fn matches(&self, string: &str) -> bool; | ||
fn parse<T: AsRef<str>>(string: T) -> Result<Self, String>; | ||
} | ||
|
||
pub trait Patterns: Sync + Send { | ||
fn contains(&self, address: &String) -> bool; | ||
fn len(&self) -> usize; | ||
} | ||
|
||
fn read_patterns(matches: &ArgMatches) -> Vec<String> { | ||
if let Some(args) = matches.values_of("PATTERN") { | ||
args.map(str::to_string).collect() | ||
} else { | ||
let mut result = Vec::new(); | ||
let stdin = std::io::stdin(); | ||
|
||
for line in stdin.lock().lines() { | ||
match line { | ||
Ok(line) => result.push(line), | ||
Err(error) => panic!("{}", error), | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
fn parse_patterns<P: Pattern>(buffer_writer: Arc<Mutex<BufferWriter>>, | ||
matches: &ArgMatches) -> Vec<P> { | ||
// TODO: Use rayon (everywhere) | ||
let mut vec: Vec<P> = Vec::new(); | ||
let raw_patterns = read_patterns(matches); | ||
|
||
for raw_pattern in raw_patterns { | ||
if raw_pattern.is_empty() { | ||
continue; | ||
} | ||
|
||
match <P as Pattern>::parse(&raw_pattern) { | ||
Ok(pattern) => vec.push(pattern), | ||
Err(error) => { | ||
let mut stdout = buffer_writer.lock().unwrap().buffer(); | ||
cprint!(matches.is_present("quiet"), | ||
stdout, | ||
Color::Yellow, | ||
"Skipping pattern '{}': ", | ||
&raw_pattern); | ||
cprintln!(matches.is_present("quiet"), | ||
stdout, | ||
Color::White, | ||
"{}", | ||
error); | ||
buffer_writer.lock().unwrap().print(&stdout).expect("Could not write to stdout."); | ||
} | ||
} | ||
} | ||
|
||
vec | ||
} |
Oops, something went wrong.