From df2c5faf7303cf3b4a1d75a5a0315854438718cd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 19 Oct 2019 08:54:57 +0200 Subject: [PATCH] Remove calls to exit() and replace them with error propagation up to main() Signed-off-by: Matthias Beyer --- bin/core/imag-grep/Cargo.toml | 1 + bin/core/imag-grep/src/lib.rs | 58 ++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/bin/core/imag-grep/Cargo.toml b/bin/core/imag-grep/Cargo.toml index a923a902..d87ffde3 100644 --- a/bin/core/imag-grep/Cargo.toml +++ b/bin/core/imag-grep/Cargo.toml @@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" } log = "0.4.6" regex = "1.1.7" failure = "0.1.5" +resiter = "0.3.0" libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } diff --git a/bin/core/imag-grep/src/lib.rs b/bin/core/imag-grep/src/lib.rs index f7a4a2d1..aadbcda5 100644 --- a/bin/core/imag-grep/src/lib.rs +++ b/bin/core/imag-grep/src/lib.rs @@ -35,9 +35,10 @@ )] #[macro_use] extern crate log; +#[macro_use] extern crate failure; extern crate clap; extern crate regex; -extern crate failure; +extern crate resiter; extern crate libimagstore; extern crate libimagrt; @@ -46,15 +47,17 @@ extern crate libimagerror; use std::io::Write; use regex::Regex; -use failure::Fallible as Result; use clap::App; +use failure::Error; +use failure::Fallible as Result; +use failure::err_msg; +use resiter::AndThen; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; use libimagstore::store::Entry; -use libimagerror::trace::MapErrTrace; -use libimagerror::exit::ExitUnwrap; -use libimagerror::io::ToExitCode; +use libimagerror::iter::IterInnerOkOrElse; + mod ui; @@ -82,34 +85,32 @@ impl ImagApplication for ImagGrep { .value_of("pattern") .map(Regex::new) .unwrap() // ensured by clap - .unwrap_or_else(|e| { - error!("Regex building error: {:?}", e); - ::std::process::exit(1) - }); + .map_err(|e| format_err!("Regex building error: {:?}", e))?; let overall_count = rt .store() - .entries() - .map_err_trace_exit_unwrap() + .entries()? .into_get_iter() - .filter_map(|res| res.map_err_trace_exit_unwrap()) - .filter_map(|entry| if pattern.is_match(entry.get_content()) { - show(&rt, &entry, &pattern, &opts, &mut count); - Some(()) - } else { - None + .map_inner_ok_or_else(|| err_msg("Entry from entries missing")) + .and_then_ok(|entry| { + if pattern.is_match(entry.get_content()) { + debug!("Matched: {}", entry.get_location()); + show(&rt, &entry, &pattern, &opts, &mut count) + } else { + debug!("Not matched: {}", entry.get_location()); + Ok(()) + } }) - .count(); + .collect::>>()? + .len(); if opts.count { - writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit(); + writeln!(rt.stdout(), "{}", count)?; } else if !opts.files_with_matches { writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches", overall_count, count, - overall_count - count) - .to_exit_code() - .unwrap_or_exit(); + overall_count - count)?; } Ok(()) @@ -130,27 +131,28 @@ impl ImagApplication for ImagGrep { fn version() -> &'static str { env!("CARGO_PKG_VERSION") } + } -fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) { +fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) -> Result<()> { if opts.files_with_matches { - writeln!(rt.stdout(), "{}", e.get_location()).to_exit_code().unwrap_or_exit(); + writeln!(rt.stdout(), "{}", e.get_location())?; } else if opts.count { *count += 1; } else { - writeln!(rt.stdout(), "{}:", e.get_location()).to_exit_code().unwrap_or_exit(); + writeln!(rt.stdout(), "{}:", e.get_location())?; for capture in re.captures_iter(e.get_content()) { for mtch in capture.iter() { if let Some(m) = mtch { - writeln!(rt.stdout(), " '{}'", m.as_str()).to_exit_code().unwrap_or_exit(); + writeln!(rt.stdout(), " '{}'", m.as_str())?; } } } - writeln!(rt.stdout()).to_exit_code().unwrap_or_exit(); + writeln!(rt.stdout())?; *count += 1; } - rt.report_touched(e.get_location()).unwrap_or_exit(); + rt.report_touched(e.get_location()).map_err(Error::from) }