Do a bit more cleanup of block params (#3455)

* Do a bit more cleanup of block params

* Do a bit more cleanup of block params
This commit is contained in:
JT
2021-05-21 19:04:27 +12:00
committed by GitHub
parent 28388b4e3a
commit 751de20f93
15 changed files with 157 additions and 186 deletions

View File

@ -1,10 +1,12 @@
use crate::prelude::*;
use log::trace;
use nu_engine::evaluate_baseline_expr;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_parser::ParserScope;
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{
hir::{CapturedBlock, ClassifiedCommand},
Signature, SyntaxShape,
};
pub struct SubCommand;
@ -29,52 +31,38 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let tag = args.call_info.name_tag.clone();
let call_info = args.evaluate_once()?;
let block = call_info.args.expect_nth(0)?.clone();
let (condition, captured) = match block {
Value {
value: UntaggedValue::Block(captured_block),
tag,
} => {
if captured_block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match captured_block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => {
(Arc::new(expr.clone()), captured_block.captured.clone())
}
_ => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
},
None => {
let block: CapturedBlock = call_info.req(0)?;
let condition = {
if block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
_ => {
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,
));
}
}
Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
};
Ok(call_info
@ -83,13 +71,13 @@ impl WholeStreamCommand for SubCommand {
let condition = condition.clone();
let ctx = ctx.clone();
ctx.scope.enter_scope();
ctx.scope.add_vars(&captured.entries);
ctx.scope.add_var("$it", item.clone());
trace!("ITEM = {:?}", item);
ctx.scope.add_vars(&block.captured.entries);
if let Some((arg, _)) = block.block.params.positional.first() {
ctx.scope.add_var(arg.name(), item.clone());
}
let result = evaluate_baseline_expr(&*condition, &*ctx);
ctx.scope.exit_scope();
trace!("RESULT = {:?}", result);
!matches!(result, Ok(ref v) if v.is_true())
})

View File

@ -3,7 +3,10 @@ use log::trace;
use nu_engine::evaluate_baseline_expr;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{
hir::{CapturedBlock, ClassifiedCommand},
Signature, SyntaxShape,
};
pub struct SubCommand;
@ -28,51 +31,37 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let tag = args.call_info.name_tag.clone();
let call_info = args.evaluate_once()?;
let block = call_info.args.expect_nth(0)?.clone();
let (condition, captured) = match block {
Value {
value: UntaggedValue::Block(captured_block),
tag,
} => {
if captured_block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match captured_block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => {
(Arc::new(expr.clone()), captured_block.captured.clone())
}
_ => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
},
None => {
let block: CapturedBlock = call_info.req(0)?;
let condition = {
if block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
_ => {
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,
));
}
}
Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
};
Ok(call_info
@ -82,8 +71,10 @@ impl WholeStreamCommand for SubCommand {
let ctx = ctx.clone();
ctx.scope.enter_scope();
ctx.scope.add_var("$it", item.clone());
ctx.scope.add_vars(&captured.entries);
ctx.scope.add_vars(&block.captured.entries);
if let Some((arg, _)) = block.block.params.positional.first() {
ctx.scope.add_var(arg.name(), item.clone());
}
trace!("ITEM = {:?}", item);
let result = evaluate_baseline_expr(&*condition, &*ctx);