forked from extern/nushell
Simplifies 'if' to work on the available scope rather than a stream (#2805)
This commit is contained in:
parent
e5b136f70d
commit
e3da546e23
@ -53,12 +53,12 @@ impl WholeStreamCommand for If {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Run a block if a condition is true",
|
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()]),
|
result: Some(vec![UntaggedValue::string("greater than 5").into()]),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Run a block if a condition is false",
|
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()]),
|
result: Some(vec![UntaggedValue::string("less than or equal to 5").into()]),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@ -105,57 +105,27 @@ async fn if_command(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(input
|
context.scope.enter_scope();
|
||||||
.then(move |input| {
|
context.scope.add_vars(&condition.captured.entries);
|
||||||
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);
|
|
||||||
|
|
||||||
async move {
|
//FIXME: should we use the scope that's brought in as well?
|
||||||
//FIXME: should we use the scope that's brought in as well?
|
let condition = evaluate_baseline_expr(&cond, &*context).await;
|
||||||
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 {
|
result.map(|x| x.to_output_stream())
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()),
|
||||||
.flatten()
|
},
|
||||||
.to_output_stream())
|
Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -13,7 +13,7 @@ use num_bigint::BigInt;
|
|||||||
use crate::commands::classified::block::run_block;
|
use crate::commands::classified::block::run_block;
|
||||||
use crate::commands::command::CommandArgs;
|
use crate::commands::command::CommandArgs;
|
||||||
use crate::commands::{
|
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,
|
StrCollect, WholeStreamCommand, Wrap,
|
||||||
};
|
};
|
||||||
use crate::evaluation_context::EvaluationContext;
|
use crate::evaluation_context::EvaluationContext;
|
||||||
@ -40,6 +40,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
|
|||||||
whole_stream_command(Each {}),
|
whole_stream_command(Each {}),
|
||||||
whole_stream_command(Last {}),
|
whole_stream_command(Last {}),
|
||||||
whole_stream_command(Nth {}),
|
whole_stream_command(Nth {}),
|
||||||
|
whole_stream_command(Set {}),
|
||||||
whole_stream_command(StrCollect),
|
whole_stream_command(StrCollect),
|
||||||
whole_stream_command(Wrap),
|
whole_stream_command(Wrap),
|
||||||
cmd,
|
cmd,
|
||||||
@ -97,6 +98,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
|
|||||||
whole_stream_command(Get {}),
|
whole_stream_command(Get {}),
|
||||||
whole_stream_command(Keep {}),
|
whole_stream_command(Keep {}),
|
||||||
whole_stream_command(Each {}),
|
whole_stream_command(Each {}),
|
||||||
|
whole_stream_command(Set {}),
|
||||||
whole_stream_command(cmd),
|
whole_stream_command(cmd),
|
||||||
whole_stream_command(StrCollect),
|
whole_stream_command(StrCollect),
|
||||||
whole_stream_command(Wrap),
|
whole_stream_command(Wrap),
|
||||||
@ -158,6 +160,7 @@ pub fn test_anchors(cmd: Command) -> Result<(), ShellError> {
|
|||||||
whole_stream_command(Each {}),
|
whole_stream_command(Each {}),
|
||||||
whole_stream_command(Last {}),
|
whole_stream_command(Last {}),
|
||||||
whole_stream_command(Nth {}),
|
whole_stream_command(Nth {}),
|
||||||
|
whole_stream_command(Set {}),
|
||||||
whole_stream_command(StrCollect),
|
whole_stream_command(StrCollect),
|
||||||
whole_stream_command(Wrap),
|
whole_stream_command(Wrap),
|
||||||
cmd,
|
cmd,
|
||||||
|
@ -27,7 +27,7 @@ fn plugins_are_declared_with_wix() {
|
|||||||
| wrap wix
|
| wrap wix
|
||||||
}
|
}
|
||||||
| default wix _
|
| default wix _
|
||||||
| if $it.wix != $it.cargo { = 1 } { = 0 }
|
| each { if $it.wix != $it.cargo { = 1 } { = 0 } }
|
||||||
| math sum
|
| math sum
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
Loading…
Reference in New Issue
Block a user