From 85d6529f0d2ad81ab544b2b68cfbc59fbae98416 Mon Sep 17 00:00:00 2001 From: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> Date: Fri, 29 Sep 2023 21:57:15 +0200 Subject: [PATCH] chore: Small refactor of `eval.rs` (#10554) # Description - Extract long expression in `eval.rs` into variable - Improve loop in eval.rs # User-Facing Changes None --- crates/nu-engine/src/eval.rs | 39 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index c89ab6e8b..521aaf02c 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1015,33 +1015,36 @@ pub fn eval_block( let num_pipelines = block.len(); for (pipeline_idx, pipeline) in block.pipelines.iter().enumerate() { - let mut i = 0; - - while i < pipeline.elements.len() { + let mut elements_iter = pipeline.elements.iter().peekable(); + while let Some(element) = elements_iter.next() { let redirect_stderr = redirect_stderr - || ((i < pipeline.elements.len() - 1) - && (matches!( - pipeline.elements[i + 1], + || (elements_iter.peek().map_or(false, |next_element| { + matches!( + next_element, PipelineElement::Redirection(_, Redirection::Stderr, _) | PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _) | PipelineElement::SeparateRedirection { .. } - ))); + ) + })); + + let redirect_stdout = redirect_stdout + || (elements_iter.peek().map_or(false, |next_element| { + matches!( + next_element, + PipelineElement::Redirection(_, Redirection::Stdout, _) + | PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _) + | PipelineElement::Expression(..) + | PipelineElement::SeparateRedirection { .. } + ) + })); // if eval internal command failed, it can just make early return with `Err(ShellError)`. let eval_result = eval_element_with_input( engine_state, stack, - &pipeline.elements[i], + element, input, - redirect_stdout - || (i != pipeline.elements.len() - 1) - && (matches!( - pipeline.elements[i + 1], - PipelineElement::Redirection(_, Redirection::Stdout, _) - | PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _) - | PipelineElement::Expression(..) - | PipelineElement::SeparateRedirection { .. } - )), + redirect_stdout, redirect_stderr, ); @@ -1073,8 +1076,6 @@ pub fn eval_block( } } } - - i += 1; } if pipeline_idx < (num_pipelines) - 1 {