From e3da546e23f6eeda84fbc81ba287920390df32e0 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 21 Dec 2020 16:02:39 +1300 Subject: [PATCH] Simplifies 'if' to work on the available scope rather than a stream (#2805) --- crates/nu-cli/src/commands/if_.rs | 70 +++++++++---------------------- crates/nu-cli/src/examples.rs | 5 ++- tests/shell/mod.rs | 2 +- 3 files changed, 25 insertions(+), 52 deletions(-) diff --git a/crates/nu-cli/src/commands/if_.rs b/crates/nu-cli/src/commands/if_.rs index f63e83708..dddd2bee3 100644 --- a/crates/nu-cli/src/commands/if_.rs +++ b/crates/nu-cli/src/commands/if_.rs @@ -53,12 +53,12 @@ impl WholeStreamCommand for If { vec![ Example { description: "Run a block if a condition is true", - example: "echo 10 | if $it > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }", + example: "set x = 10; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }", result: Some(vec![UntaggedValue::string("greater than 5").into()]), }, Example { description: "Run a block if a condition is false", - example: "echo 1 | if $it > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }", + example: "set x = 1; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }", result: Some(vec![UntaggedValue::string("less than or equal to 5").into()]), }, ] @@ -105,57 +105,27 @@ async fn if_command(raw_args: CommandArgs) -> Result { } }; - Ok(input - .then(move |input| { - let cond = cond.clone(); - let then_case = then_case.clone(); - let else_case = else_case.clone(); - let context = context.clone(); - context.scope.enter_scope(); - context.scope.add_vars(&condition.captured.entries); - context.scope.add_var("$it", input); + context.scope.enter_scope(); + context.scope.add_vars(&condition.captured.entries); - async move { - //FIXME: should we use the scope that's brought in as well? - let condition = evaluate_baseline_expr(&cond, &*context).await; + //FIXME: should we use the scope that's brought in as well? + let condition = evaluate_baseline_expr(&cond, &*context).await; + match condition { + Ok(condition) => match condition.as_bool() { + Ok(b) => { + let result = if b { + run_block(&then_case.block, &*context, input).await + } else { + run_block(&else_case.block, &*context, input).await + }; + context.scope.exit_scope(); - match condition { - Ok(condition) => match condition.as_bool() { - Ok(b) => { - if b { - let result = - run_block(&then_case.block, &*context, InputStream::empty()) - .await; - context.scope.exit_scope(); - - match result { - Ok(stream) => stream.to_output_stream(), - Err(e) => futures::stream::iter(vec![Err(e)].into_iter()) - .to_output_stream(), - } - } else { - let result = - run_block(&else_case.block, &*context, InputStream::empty()) - .await; - context.scope.exit_scope(); - - match result { - Ok(stream) => stream.to_output_stream(), - Err(e) => futures::stream::iter(vec![Err(e)].into_iter()) - .to_output_stream(), - } - } - } - Err(e) => { - futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream() - } - }, - Err(e) => futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream(), - } + result.map(|x| x.to_output_stream()) } - }) - .flatten() - .to_output_stream()) + Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()), + }, + Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()), + } } #[cfg(test)] diff --git a/crates/nu-cli/src/examples.rs b/crates/nu-cli/src/examples.rs index 28ce79378..8c734d709 100644 --- a/crates/nu-cli/src/examples.rs +++ b/crates/nu-cli/src/examples.rs @@ -13,7 +13,7 @@ use num_bigint::BigInt; use crate::commands::classified::block::run_block; use crate::commands::command::CommandArgs; use crate::commands::{ - whole_stream_command, BuildString, Command, Each, Echo, First, Get, Keep, Last, Nth, + whole_stream_command, BuildString, Command, Each, Echo, First, Get, Keep, Last, Nth, Set, StrCollect, WholeStreamCommand, Wrap, }; use crate::evaluation_context::EvaluationContext; @@ -40,6 +40,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> { whole_stream_command(Each {}), whole_stream_command(Last {}), whole_stream_command(Nth {}), + whole_stream_command(Set {}), whole_stream_command(StrCollect), whole_stream_command(Wrap), cmd, @@ -97,6 +98,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> { whole_stream_command(Get {}), whole_stream_command(Keep {}), whole_stream_command(Each {}), + whole_stream_command(Set {}), whole_stream_command(cmd), whole_stream_command(StrCollect), whole_stream_command(Wrap), @@ -158,6 +160,7 @@ pub fn test_anchors(cmd: Command) -> Result<(), ShellError> { whole_stream_command(Each {}), whole_stream_command(Last {}), whole_stream_command(Nth {}), + whole_stream_command(Set {}), whole_stream_command(StrCollect), whole_stream_command(Wrap), cmd, diff --git a/tests/shell/mod.rs b/tests/shell/mod.rs index 1e2d60142..87fd0abed 100644 --- a/tests/shell/mod.rs +++ b/tests/shell/mod.rs @@ -27,7 +27,7 @@ fn plugins_are_declared_with_wix() { | wrap wix } | default wix _ - | if $it.wix != $it.cargo { = 1 } { = 0 } + | each { if $it.wix != $it.cargo { = 1 } { = 0 } } | math sum "# ));