Skip to content

Commit 3797a2a

Browse files
committed
simplegrep: touch up
1 parent 0e2f8f7 commit 3797a2a

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

grep/examples/simplegrep.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ extern crate termcolor;
33
extern crate walkdir;
44

55
use std::env;
6+
use std::error::Error;
67
use std::ffi::OsString;
7-
use std::path::Path;
88
use std::process;
9-
use std::result;
109

1110
use grep::cli;
1211
use grep::printer::{ColorSpecs, StandardBuilder};
@@ -15,52 +14,46 @@ use grep::searcher::{BinaryDetection, SearcherBuilder};
1514
use termcolor::ColorChoice;
1615
use walkdir::WalkDir;
1716

18-
macro_rules! fail {
19-
($($tt:tt)*) => {
20-
return Err(From::from(format!($($tt)*)));
21-
}
22-
}
23-
24-
type Result<T> = result::Result<T, Box<::std::error::Error>>;
25-
2617
fn main() {
2718
if let Err(err) = try_main() {
2819
eprintln!("{}", err);
2920
process::exit(1);
3021
}
3122
}
3223

33-
fn try_main() -> Result<()> {
24+
fn try_main() -> Result<(), Box<Error>> {
3425
let mut args: Vec<OsString> = env::args_os().collect();
3526
if args.len() < 2 {
36-
fail!("Usage: simplegrep <pattern> [<path> ...]");
27+
return Err("Usage: simplegrep <pattern> [<path> ...]".into());
3728
}
3829
if args.len() == 2 {
3930
args.push(OsString::from("./"));
4031
}
4132
search(cli::pattern_from_os(&args[1])?, &args[2..])
4233
}
4334

44-
fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
35+
fn search(pattern: &str, paths: &[OsString]) -> Result<(), Box<Error>> {
4536
let matcher = RegexMatcher::new_line_matcher(&pattern)?;
4637
let mut searcher = SearcherBuilder::new()
4738
.binary_detection(BinaryDetection::quit(b'\x00'))
4839
.line_number(false)
4940
.build();
5041
let mut printer = StandardBuilder::new()
5142
.color_specs(ColorSpecs::default_with_color())
52-
.build(cli::stdout(color_choice()));
43+
.build(cli::stdout(
44+
if cli::is_tty_stdout() {
45+
ColorChoice::Auto
46+
} else {
47+
ColorChoice::Never
48+
}
49+
));
5350

5451
for path in paths {
5552
for result in WalkDir::new(path) {
5653
let dent = match result {
5754
Ok(dent) => dent,
5855
Err(err) => {
59-
eprintln!(
60-
"{}: {}",
61-
err.path().unwrap_or(Path::new("error")).display(),
62-
err,
63-
);
56+
eprintln!("{}", err);
6457
continue;
6558
}
6659
};
@@ -79,11 +72,3 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
7972
}
8073
Ok(())
8174
}
82-
83-
fn color_choice() -> ColorChoice {
84-
if cli::is_tty_stdout() {
85-
ColorChoice::Auto
86-
} else {
87-
ColorChoice::Never
88-
}
89-
}

0 commit comments

Comments
 (0)