From 03fb895197ce07360254939393a606f5c62bcf52 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 28 Dec 2018 14:15:09 +0100 Subject: [PATCH] getopt: condense nested match to reduce indentation The `match value` statement is the only thing covering the entire RawArgument::Option case. `rustfmt` suggests this more condensed way of writing this case. See the `git diff -w` of this patch: |diff --git a/src/getopts.rs b/src/getopts.rs |index 9755af2..4db4579 100644 |--- a/src/getopts.rs |+++ b/src/getopts.rs |@@ -72,8 +72,7 @@ pub fn parse_arguments>( | RawArgument::Separator => { | break; | } |- RawArgument::Option { name, value } => { |- match value { |+ RawArgument::Option { name, value } => match value { | None => { | let mut want_bool = false; | let mut can_default = false; |@@ -125,7 +124,6 @@ pub fn parse_arguments>( | data.push((name, v)); | } | } |- } | RawArgument::Argument { value } => { | rest.push(value); | } Signed-off-by: Wolfgang Bumiller --- src/getopts.rs | 88 ++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/src/getopts.rs b/src/getopts.rs index 9755af27..4db45790 100644 --- a/src/getopts.rs +++ b/src/getopts.rs @@ -72,58 +72,56 @@ pub fn parse_arguments>( RawArgument::Separator => { break; } - RawArgument::Option { name, value } => { - match value { - None => { - let mut want_bool = false; - let mut can_default = false; - if let Some((_optional, param_schema)) = properties.get::(&name) { - if let Schema::Boolean(boolean_schema) = param_schema.as_ref() { - want_bool = true; - if let Some(default) = boolean_schema.default { - if default == false { can_default = true; } - } else { - can_default = true; - } - } - } - - let mut next_is_argument = false; - let mut next_is_bool = false; - - if (pos + 1) < args.len() { - let next = args[pos+1].as_ref(); - if let RawArgument::Argument { value: _} = parse_argument(next) { - next_is_argument = true; - if let Ok(_) = parse_boolean(next) { next_is_bool = true; } - } - } - - if want_bool { - if next_is_bool { - pos += 1; - data.push((name, args[pos].as_ref().to_string())); - } else if can_default { - data.push((name, "true".to_string())); + RawArgument::Option { name, value } => match value { + None => { + let mut want_bool = false; + let mut can_default = false; + if let Some((_optional, param_schema)) = properties.get::(&name) { + if let Schema::Boolean(boolean_schema) = param_schema.as_ref() { + want_bool = true; + if let Some(default) = boolean_schema.default { + if default == false { can_default = true; } } else { - errors.push(format_err!("parameter '{}': {}", name, - "missing boolean value.")); + can_default = true; } + } + } + let mut next_is_argument = false; + let mut next_is_bool = false; + + if (pos + 1) < args.len() { + let next = args[pos+1].as_ref(); + if let RawArgument::Argument { value: _} = parse_argument(next) { + next_is_argument = true; + if let Ok(_) = parse_boolean(next) { next_is_bool = true; } + } + } + + if want_bool { + if next_is_bool { + pos += 1; + data.push((name, args[pos].as_ref().to_string())); + } else if can_default { + data.push((name, "true".to_string())); } else { + errors.push(format_err!("parameter '{}': {}", name, + "missing boolean value.")); + } - if next_is_argument { - pos += 1; - data.push((name, args[pos].as_ref().to_string())); - } else { - errors.push(format_err!("parameter '{}': {}", name, - "missing parameter value.")); - } + } else { + + if next_is_argument { + pos += 1; + data.push((name, args[pos].as_ref().to_string())); + } else { + errors.push(format_err!("parameter '{}': {}", name, + "missing parameter value.")); } } - Some(v) => { - data.push((name, v)); - } + } + Some(v) => { + data.push((name, v)); } } RawArgument::Argument { value } => {