From 792e9e695353c94e16f199c2d846e53a178e2e4e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 30 Apr 2019 14:22:14 +0200 Subject: Simplify error handling --- Cargo.toml | 1 + src/main.rs | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 474a983..a6db2ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4" flexi_logger = "0.11" prettytable-rs = "0.8" filters = "0.3" +boolinator = "2" [dependencies.clap] version = ">=2.33" diff --git a/src/main.rs b/src/main.rs index 13b42a0..04c165c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate url; extern crate xdg; extern crate flexi_logger; extern crate filters; +extern crate boolinator; #[cfg(feature = "compare_csv")] extern crate csv; @@ -32,6 +33,7 @@ use failure::ResultExt; use failure::Fallible as Result; use clap::ArgMatches; use filters::filter::Filter; +use boolinator::Boolinator; use config::Configuration; use compare::ComparePackage; @@ -193,21 +195,21 @@ fn app() -> Result<()> { }, (other, _mtch) => { - if app.is_present("input_stdin") { - // Ugly, but works: - // If we have "--stdin" on CLI, we have a CLI/Stdin backend, which means that we can query - // _any_ "project", and get the stdin anyways. This is really not like it should be, but - // works for now - let packages = backend - .project("")? - .into_iter() - .filter(|package| repository_filter.filter(package.repo())) - .collect(); - frontend.list_packages(packages)?; - } else { - error!("Unknown command: '{}'", other); - ::std::process::exit(1) - } + app.is_present("input_stdin") + .as_result((), Error::from(format_err!("Input not from stdin"))) + .and_then(|_| { + // Ugly, but works: + // If we have "--stdin" on CLI, we have a CLI/Stdin backend, which means that we can query + // _any_ "project", and get the stdin anyways. This is really not like it should be, but + // works for now + let packages = backend + .project("")? + .into_iter() + .filter(|package| repository_filter.filter(package.repo())) + .collect(); + frontend.list_packages(packages) + }) + .map_err(|_| format_err!("Unknown command: {}", other)) } } -- cgit v1.2.3