From 3b93c5750c0ea506f07ab4ce6e09cba69689874d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 28 Jan 2017 16:55:18 +0300 Subject: [PATCH] Simplify cli-handling functions --- src/bin/cargo.rs | 28 +++++++++++++++++++++++++--- src/cargo/lib.rs | 40 ++++------------------------------------ tests/lockfile-compat.rs | 1 - 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 8b9d1a23573..3fc95ffe4d8 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -12,8 +12,7 @@ use std::env; use std::fs; use std::path::{Path,PathBuf}; -use cargo::core::shell::Verbosity; -use cargo::execute_main_without_stdin; +use cargo::core::shell::{Verbosity, ColorConfig}; use cargo::util::{self, CliResult, lev_distance, Config, human, CargoResult}; use cargo::util::CliError; @@ -69,7 +68,30 @@ See 'cargo help ' for more information on a specific command. fn main() { env_logger::init().unwrap(); - execute_main_without_stdin(execute, true, USAGE) + + let config = match Config::default() { + Ok(cfg) => cfg, + Err(e) => { + let mut shell = cargo::shell(Verbosity::Verbose, ColorConfig::Auto); + cargo::handle_cli_error(e.into(), &mut shell) + } + }; + + let result = (|| { + let args: Vec<_> = try!(env::args_os().map(|s| { + s.into_string().map_err(|s| { + human(format!("invalid unicode in argument: {:?}", s)) + }) + }).collect()); + let rest = &args; + cargo::call_main_without_stdin(execute, &config, USAGE, rest, true) + })(); + + match result { + Err(e) => cargo::handle_cli_error(e, &mut *config.shell()), + Ok(None) => {}, + Ok(Some(())) => unreachable!(), + } } macro_rules! each_subcommand{ diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index fbcb62c1e0f..920fb4185ed 100755 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -25,16 +25,14 @@ extern crate term; extern crate toml; extern crate url; -use std::env; -use std::fmt; use std::io; +use std::fmt; use rustc_serialize::{Decodable, Encodable}; use rustc_serialize::json; use docopt::Docopt; use core::{Shell, MultiShell, ShellConfig, Verbosity, ColorConfig}; use core::shell::Verbosity::{Verbose}; -use core::shell::ColorConfig::{Auto}; use term::color::{BLACK}; pub use util::{CargoError, CargoResult, CliError, CliResult, human, Config, ChainError}; @@ -106,17 +104,6 @@ impl fmt::Display for VersionInfo { } } -pub fn execute_main_without_stdin( - exec: fn(T, &Config) -> CliResult>, - options_first: bool, - usage: &str) - where V: Encodable, T: Decodable -{ - process::(|rest, config| { - call_main_without_stdin(exec, config, usage, rest, options_first) - }); -} - pub fn call_main_without_stdin( exec: fn(T, &Config) -> CliResult>, config: &Config, @@ -129,26 +116,7 @@ pub fn call_main_without_stdin( exec(flags, config) } -fn process(mut callback: F) - where F: FnMut(&[String], &Config) -> CliResult>, - V: Encodable -{ - let mut config = None; - let result = (|| { - config = Some(Config::default()?); - let args: Vec<_> = try!(env::args_os().map(|s| { - s.into_string().map_err(|s| { - human(format!("invalid unicode in argument: {:?}", s)) - }) - }).collect()); - callback(&args, config.as_ref().unwrap()) - })(); - let mut verbose_shell = shell(Verbose, Auto); - let mut shell = config.as_ref().map(|s| s.shell()); - let shell = shell.as_mut().map(|s| &mut **s).unwrap_or(&mut verbose_shell); - process_executed(result, shell) -} - +// This will diverge if `result` is an `Err` and return otherwise. pub fn process_executed(result: CliResult>, shell: &mut MultiShell) where T: Encodable { @@ -207,7 +175,7 @@ pub fn shell(verbosity: Verbosity, color_config: ColorConfig) -> MultiShell { } } -pub fn handle_cli_error(err: CliError, shell: &mut MultiShell) { +pub fn handle_cli_error(err: CliError, shell: &mut MultiShell) -> ! { debug!("handle_cli_error; err={:?}", err); let CliError { error, exit_code, unknown } = err; @@ -231,7 +199,7 @@ pub fn handle_cli_error(err: CliError, shell: &mut MultiShell) { } } - std::process::exit(exit_code); + std::process::exit(exit_code) } pub fn handle_error(err: &CargoError, shell: &mut MultiShell) { diff --git a/tests/lockfile-compat.rs b/tests/lockfile-compat.rs index f2296d9027b..81561e28eff 100644 --- a/tests/lockfile-compat.rs +++ b/tests/lockfile-compat.rs @@ -1,4 +1,3 @@ -#[macro_use] extern crate cargotest; extern crate hamcrest;