From 1a46e70dfb6b943cc1cc17dfe32422fa6d4c4f15 Mon Sep 17 00:00:00 2001 From: Leonhard Kipp Date: Tue, 20 Apr 2021 08:38:36 +0200 Subject: [PATCH] Use new functions in which (#3310) * Use new functions in which * Impl rest_with_minimum and use it * Use has_flag instead of get_switch --- crates/nu-command/src/commands/which_.rs | 31 ++++++++++-------------- crates/nu-engine/src/command_args.rs | 14 +++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/crates/nu-command/src/commands/which_.rs b/crates/nu-command/src/commands/which_.rs index 7889e55f9..d13cac8c6 100644 --- a/crates/nu-command/src/commands/which_.rs +++ b/crates/nu-command/src/commands/which_.rs @@ -3,7 +3,7 @@ use indexmap::map::IndexMap; use log::trace; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; pub struct Which; @@ -24,7 +24,7 @@ impl WholeStreamCommand for Which { "Finds a program file, alias or custom command." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { which(args) } } @@ -165,10 +165,9 @@ fn get_all_entries_in_path(_: &str, _: Tag) -> Vec { vec![] } -#[derive(Deserialize, Debug)] +#[derive(Debug)] struct WhichArgs { - application: Tagged, - rest: Vec>, + applications: Vec>, all: bool, } @@ -214,26 +213,22 @@ fn which_single(application: Tagged, all: bool, scope: &Scope) -> Vec Result { - let scope = args.scope.clone(); +fn which(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; - let ( - WhichArgs { - application, - rest, - all, - }, - _, - ) = args.process()?; + let which_args = WhichArgs { + applications: args.rest_with_minimum(0, 1)?, + all: args.has_flag("all"), + }; let mut output = vec![]; - for app in vec![application].into_iter().chain(rest.into_iter()) { - let values = which_single(app, all, &scope); + for app in which_args.applications { + let values = which_single(app, which_args.all, &args.scope); output.extend(values); } - Ok((output.into_iter().map(ReturnSuccess::value)).to_action_stream()) + Ok(output.into_iter().to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-engine/src/command_args.rs b/crates/nu-engine/src/command_args.rs index ee8d410f3..ce70bfc3e 100644 --- a/crates/nu-engine/src/command_args.rs +++ b/crates/nu-engine/src/command_args.rs @@ -281,4 +281,18 @@ impl EvaluatedCommandArgs { Ok(output) } + + pub fn rest_with_minimum( + &self, + pos: usize, + count: usize, + ) -> Result, ShellError> { + let mut output = vec![]; + for i in pos..pos + count { + output.push(self.req(i)?); + } + output.extend(self.rest(pos + count)?); + + Ok(output) + } }