From: Aleksey Kladov Date: Mon, 28 Mar 2016 23:37:43 +0000 (+0300) Subject: minor: use enum instead of pair of booleans X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=c7e51d3994f3fc885bfb94b32232084ad6f94834;p=rust.git minor: use enum instead of pair of booleans This is a bit more typing, but statically forbids using both verbose and quiet --- diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index d267dc5e35d..c033851f98a 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -49,19 +49,22 @@ fn execute() -> i32 { } }; - let (verbose, quiet) = (matches.opt_present("v"), matches.opt_present("q")); - - if verbose && quiet { - print_usage(&opts, "quiet mode and verbose mode are not compatible"); - return failure; - } + let verbosity = match (matches.opt_present("v"), matches.opt_present("q")) { + (false, false) => Verbosity::Normal, + (false, true) => Verbosity::Quiet, + (true, false) => Verbosity::Verbose, + (true, true) => { + print_usage(&opts, "quiet mode and verbose mode are not compatible"); + return failure; + } + }; if matches.opt_present("h") { print_usage(&opts, ""); return success; } - match format_crate(verbose, quiet) { + match format_crate(verbosity) { Err(e) => { print_usage(&opts, &e.to_string()); failure @@ -83,21 +86,28 @@ fn print_usage(opts: &Options, reason: &str) { opts.usage(&msg)); } -fn format_crate(verbose: bool, quiet: bool) -> Result { +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Verbosity { + Verbose, + Normal, + Quiet, +} + +fn format_crate(verbosity: Verbosity) -> Result { let targets = try!(get_targets()); // Currently only bin and lib files get formatted let files: Vec<_> = targets.into_iter() .filter(|t| t.kind.is_lib() | t.kind.is_bin()) .inspect(|t| { - if verbose { + if verbosity == Verbosity::Verbose { println!("[{:?}] {:?}", t.kind, t.path) } }) .map(|t| t.path) .collect(); - format_files(&files, &get_fmt_args(), verbose, quiet) + format_files(&files, &get_fmt_args(), verbosity) } fn get_fmt_args() -> Vec { @@ -173,15 +183,14 @@ fn target_from_json(jtarget: &Json) -> Target { fn format_files(files: &Vec, fmt_args: &Vec, - verbose: bool, - quiet: bool) + verbosity: Verbosity) -> Result { - let stdout = if quiet { + let stdout = if verbosity == Verbosity::Quiet { std::process::Stdio::null() } else { std::process::Stdio::inherit() }; - if verbose { + if verbosity == Verbosity::Verbose { print!("rustfmt"); for a in fmt_args.iter() { print!(" {}", a);