Enginep/all (#3312)

* Add iter-extensions

* Move all to enginep style

* Remove iter extensions

* Fix clippy lints

* Add comment and make ? more visible

* Remove try_all

* Remove all because it cant return err
This commit is contained in:
Leonhard Kipp 2021-04-18 20:39:33 +02:00 committed by GitHub
parent d34068da18
commit d8e105fe34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,13 +5,11 @@ use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{
hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue,
}; };
use nu_stream::ToActionStream;
pub struct Command; pub struct Command;
#[derive(Deserialize)] struct AllArgs {
pub struct Arguments { predicate: CapturedBlock,
block: CapturedBlock,
} }
impl WholeStreamCommand for Command { impl WholeStreamCommand for Command {
@ -31,7 +29,7 @@ impl WholeStreamCommand for Command {
"Find if the table rows matches the condition." "Find if the table rows matches the condition."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
all(args) all(args)
} }
@ -53,72 +51,75 @@ impl WholeStreamCommand for Command {
} }
} }
fn all(args: CommandArgs) -> Result<ActionStream, ShellError> { fn all(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args)); let ctx = EvaluationContext::from_args(&args);
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let (Arguments { block }, input) = args.process()?; let args = args.evaluate_once()?;
let all_args = AllArgs {
predicate: args.req(0)?,
};
let err = Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
args.call_info.name_tag.clone(),
));
//This seems a little odd. Can't we have predicates with pipelines/multiple statements?
let condition = { let condition = {
if block.block.block.len() != 1 { if all_args.predicate.block.block.len() != 1 {
return Err(ShellError::labeled_error( return err;
"Expected a condition",
"expected a condition",
tag,
));
} }
match block.block.block[0].pipelines.get(0) { match all_args.predicate.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) { Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(), Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
_ => { _ => {
return Err(ShellError::labeled_error( return err;
"Expected a condition",
"expected a condition",
tag,
));
} }
}, },
None => { None => {
return Err(ShellError::labeled_error( return err;
"Expected a condition",
"expected a condition",
tag,
));
} }
} }
}; };
let scope = args.scope.clone();
let init = Ok(InputStream::one( let init = Ok(InputStream::one(
UntaggedValue::boolean(true).into_value(&tag), UntaggedValue::boolean(true).into_value(&tag),
)); ));
Ok(input // Variables in nu are immutable. Having the same variable accross invocations
.fold(init, move |acc, row| { // of evaluate_baseline_expr does not mutate the variables and those each
let condition = condition.clone(); // invocations are independent of each other!
let ctx = ctx.clone(); scope.enter_scope();
ctx.scope.enter_scope(); scope.add_vars(&all_args.predicate.captured.entries);
ctx.scope.add_vars(&block.captured.entries); let result = args.input.fold(init, move |acc, row| {
ctx.scope.add_var("$it", row); let condition = condition.clone();
let ctx = ctx.clone();
ctx.scope.add_var("$it", row);
let condition = evaluate_baseline_expr(&condition, &*ctx); let condition = evaluate_baseline_expr(&condition, &ctx);
ctx.scope.exit_scope();
let curr = acc?.drain_vec(); let curr = acc?.drain_vec();
let curr = curr let curr = curr
.get(0) .get(0)
.ok_or_else(|| ShellError::unexpected("No value to check with"))?; .ok_or_else(|| ShellError::unexpected("No value to check with"))?;
let cond = curr.as_bool()?; let cond = curr.as_bool()?;
match condition { match condition {
Ok(condition) => match condition.as_bool() { Ok(condition) => match condition.as_bool() {
Ok(b) => Ok(InputStream::one( Ok(b) => Ok(InputStream::one(
UntaggedValue::boolean(cond && b).into_value(&curr.tag), UntaggedValue::boolean(cond && b).into_value(&curr.tag),
)), )),
Err(e) => Err(e),
},
Err(e) => Err(e), Err(e) => Err(e),
} },
})? Err(e) => Err(e),
.to_action_stream()) }
});
scope.exit_scope();
Ok(result?.to_output_stream())
} }
#[cfg(test)] #[cfg(test)]