From dbe0effd67b0c1efa32046401bda18eb8d470e3b Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Fri, 17 Jul 2020 21:12:06 -0400 Subject: [PATCH] User error propagation operator (#2201) --- crates/nu-cli/src/commands/group_by_date.rs | 26 +++++++-------------- crates/nu-cli/src/commands/insert.rs | 7 +++--- crates/nu-cli/src/commands/math/utils.rs | 7 +----- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/crates/nu-cli/src/commands/group_by_date.rs b/crates/nu-cli/src/commands/group_by_date.rs index 29f9def7a7..3e3e5cc495 100644 --- a/crates/nu-cli/src/commands/group_by_date.rs +++ b/crates/nu-cli/src/commands/group_by_date.rs @@ -98,14 +98,11 @@ pub async fn group_by_date( Grouper::ByDate(None) }; - match (grouper_date, grouper_column) { + let value_result = match (grouper_date, grouper_column) { (Grouper::ByDate(None), GroupByColumn::Name(None)) => { let block = Box::new(move |_, row: &Value| row.format("%Y-%b-%d")); - match crate::utils::data::group(&values, &Some(block), &name) { - Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), - Err(err) => Err(err), - } + crate::utils::data::group(&values, &Some(block), &name) } (Grouper::ByDate(None), GroupByColumn::Name(Some(column_name))) => { let block = Box::new(move |_, row: &Value| { @@ -117,18 +114,12 @@ pub async fn group_by_date( group_key?.format("%Y-%b-%d") }); - match crate::utils::data::group(&values, &Some(block), &name) { - Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), - Err(err) => Err(err), - } + crate::utils::data::group(&values, &Some(block), &name) } (Grouper::ByDate(Some(fmt)), GroupByColumn::Name(None)) => { let block = Box::new(move |_, row: &Value| row.format(&fmt)); - match crate::utils::data::group(&values, &Some(block), &name) { - Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), - Err(err) => Err(err), - } + crate::utils::data::group(&values, &Some(block), &name) } (Grouper::ByDate(Some(fmt)), GroupByColumn::Name(Some(column_name))) => { let block = Box::new(move |_, row: &Value| { @@ -140,12 +131,11 @@ pub async fn group_by_date( group_key?.format(&fmt) }); - match crate::utils::data::group(&values, &Some(block), &name) { - Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))), - Err(err) => Err(err), - } + crate::utils::data::group(&values, &Some(block), &name) } - } + }; + + Ok(OutputStream::one(ReturnSuccess::value(value_result?))) } } diff --git a/crates/nu-cli/src/commands/insert.rs b/crates/nu-cli/src/commands/insert.rs index 51f895b4ed..3039a8c15a 100644 --- a/crates/nu-cli/src/commands/insert.rs +++ b/crates/nu-cli/src/commands/insert.rs @@ -56,10 +56,9 @@ async fn insert(args: CommandArgs, registry: &CommandRegistry) -> Result match row.insert_data_at_column_path(&column, value.clone()) { - Ok(v) => Ok(ReturnSuccess::Value(v)), - Err(err) => Err(err), - }, + } => Ok(ReturnSuccess::Value( + row.insert_data_at_column_path(&column, value.clone())?, + )), Value { tag, .. } => Err(ShellError::labeled_error( "Unrecognized type in stream", diff --git a/crates/nu-cli/src/commands/math/utils.rs b/crates/nu-cli/src/commands/math/utils.rs index 68f2fdd985..6004e2d49f 100644 --- a/crates/nu-cli/src/commands/math/utils.rs +++ b/crates/nu-cli/src/commands/math/utils.rs @@ -50,12 +50,7 @@ pub fn calculate(values: &[Value], name: &Tag, mf: MathFunction) -> Result { - column_totals.insert(col_name, result); - } - Err(err) => return Err(err), - } + column_totals.insert(col_name, mf(&col_vals, &name)?); } Ok(UntaggedValue::Row(Dictionary {