2020-07-04 21:40:04 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::evaluate_baseline_expr;
|
|
|
|
use nu_engine::run_block;
|
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-07-04 21:40:04 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-09-26 01:40:02 +02:00
|
|
|
use nu_protocol::{
|
2020-12-18 08:53:49 +01:00
|
|
|
hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue,
|
2020-09-26 01:40:02 +02:00
|
|
|
};
|
2020-07-04 21:40:04 +02:00
|
|
|
|
|
|
|
pub struct If;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct IfArgs {
|
2020-12-18 08:53:49 +01:00
|
|
|
condition: CapturedBlock,
|
|
|
|
then_case: CapturedBlock,
|
|
|
|
else_case: CapturedBlock,
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl WholeStreamCommand for If {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"if"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("if")
|
|
|
|
.required(
|
|
|
|
"condition",
|
2020-12-21 05:32:06 +01:00
|
|
|
SyntaxShape::MathExpression,
|
2020-07-04 21:40:04 +02:00
|
|
|
"the condition that must match",
|
|
|
|
)
|
|
|
|
.required(
|
|
|
|
"then_case",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"block to run if condition is true",
|
|
|
|
)
|
|
|
|
.required(
|
|
|
|
"else_case",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"block to run if condition is false",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-07-18 19:16:35 +02:00
|
|
|
"Run blocks if a condition is true or false."
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
if_command(args).await
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Run a block if a condition is true",
|
2021-01-05 00:30:55 +01:00
|
|
|
example: "let x = 10; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
|
2020-07-04 21:40:04 +02:00
|
|
|
result: Some(vec![UntaggedValue::string("greater than 5").into()]),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Run a block if a condition is false",
|
2021-01-05 00:30:55 +01:00
|
|
|
example: "let x = 1; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
|
2020-07-04 21:40:04 +02:00
|
|
|
result: Some(vec![UntaggedValue::string("less than or equal to 5").into()]),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn if_command(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-07-04 21:40:04 +02:00
|
|
|
let tag = raw_args.call_info.name_tag.clone();
|
2021-02-06 01:34:26 +01:00
|
|
|
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
2020-07-04 21:40:04 +02:00
|
|
|
|
|
|
|
let (
|
|
|
|
IfArgs {
|
|
|
|
condition,
|
|
|
|
then_case,
|
|
|
|
else_case,
|
|
|
|
},
|
|
|
|
input,
|
2020-12-18 08:53:49 +01:00
|
|
|
) = raw_args.process().await?;
|
|
|
|
let cond = {
|
|
|
|
if condition.block.block.len() != 1 {
|
2020-07-04 21:40:04 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a condition",
|
|
|
|
"expected a condition",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
2020-12-18 08:53:49 +01:00
|
|
|
match condition.block.block[0].pipelines.get(0) {
|
|
|
|
Some(item) => match item.list.get(0) {
|
|
|
|
Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
|
2020-07-04 21:40:04 +02:00
|
|
|
_ => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a condition",
|
|
|
|
"expected a condition",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a condition",
|
|
|
|
"expected a condition",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-21 04:02:39 +01:00
|
|
|
context.scope.enter_scope();
|
|
|
|
context.scope.add_vars(&condition.captured.entries);
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
|
|
|
result.map(|x| x.to_output_stream())
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
2020-12-21 04:02:39 +01:00
|
|
|
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()),
|
|
|
|
}
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::If;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-07-04 21:40:04 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-07-04 21:40:04 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(If {})
|
2020-07-04 21:40:04 +02:00
|
|
|
}
|
|
|
|
}
|