Skip to content
Myr

Code cleanup, error handling, closes #1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent 23e51da · Browse files · 1 file+24 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+24 added−22 removed

  • other:+24 added−22 removed

1 file+24 added−22 removed

modified:src/main.rs+24 added-22 removed
28 unmodified lines
29 if entry.path() == root {30 continue;31 }3233 let path_relative = entry.path().strip_prefix(root).unwrap();34 let path_str = match path_relative.to_str() {35 Some(s) => s,33 let path_str_r = entry34 .path()35 .strip_prefix(root) // create relative path36 .ok()37 .and_then(Path::to_str);3839 let path_str = match path_str_r {40 Some(p) => p,41 None => continue42 };38 match pattern.find(path_str) {39 Some(_) =>40 println!("{}", path_str),41 None =>42 continue43 }4344 pattern.find(path_str)45 .map(|_| println!("{}", path_str));46 }47}4849/// Print error message to stderr and exit with status `1`.48fn error<T: Error>(err: &T) -> ! {49 writeln!(&mut std::io::stderr(), "{}", err.description())50fn error(message: &str) -> ! {51 writeln!(&mut std::io::stderr(), "{}", message)52 .expect("Failed writing to stderr");53 process::exit(1);54}55
3 unmodified lines
59 let mut opts = Options::new();60 opts.optflag("h", "help", "print this help message");61 opts.optflag("s", "sensitive", "case-sensitive search");62 let matches = match opts.parse(&args[1..]) {61 Ok(m) => { m }62 Err(f) => { error(&f) }63 Ok(m) => m,64 Err(e) => error(e.description())65 };6667 if matches.opt_present("h") {68 let brief = "Usage: fd [PATTERN]";
5 unmodified lines
7475 let empty = String::new();76 let pattern = matches.free.get(0).unwrap_or(&empty);7776 let current_dir_buf = env::current_dir()77 .expect("Could not get current directory!");78 let current_dir_buf = match env::current_dir() {79 Ok(cd) => cd,80 Err(_) => error("Could not get current directory!")81 };82 let current_dir = current_dir_buf.as_path();8380 match81 RegexBuilder::new(pattern)82 .case_insensitive(case_insensitive)83 .build() {84 Ok(re) =>85 scan(current_dir, &re),84 match RegexBuilder::new(pattern)85 .case_insensitive(case_insensitive)86 .build() {87 Ok(re) => scan(current_dir, &re),88 Err(err) => error(err.description())86 Err(err) => error(&err)89 }90}