From 0905a2c3a26abb64029fbc652b5a2f710c4d61ff Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Wed, 12 May 2021 20:07:20 +0100 Subject: [PATCH] commands to engine p (#3417) * commands to engine p * Clippy suggestion * Clippy suggestion --- crates/nu-command/src/commands/drop/column.rs | 22 +++--- .../nu-command/src/commands/drop/command.rs | 20 ++--- crates/nu-command/src/commands/each/group.rs | 23 +++--- crates/nu-command/src/commands/each/window.rs | 32 ++++---- .../nu-command/src/commands/format/command.rs | 21 +++-- .../src/commands/format/format_filesize.rs | 77 +++++++++---------- 6 files changed, 88 insertions(+), 107 deletions(-) diff --git a/crates/nu-command/src/commands/drop/column.rs b/crates/nu-command/src/commands/drop/column.rs index 7d500d494..90ba17371 100644 --- a/crates/nu-command/src/commands/drop/column.rs +++ b/crates/nu-command/src/commands/drop/column.rs @@ -2,16 +2,11 @@ use crate::prelude::*; use nu_data::base::select_fields; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; +use nu_protocol::{Signature, SyntaxShape}; use nu_source::Tagged; pub struct SubCommand; -#[derive(Deserialize)] -pub struct Arguments { - columns: Option>, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "drop column" @@ -29,7 +24,7 @@ impl WholeStreamCommand for SubCommand { "Remove the last number of columns. If you want to remove columns by name, try 'reject'." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { drop(args) } @@ -47,8 +42,9 @@ impl WholeStreamCommand for SubCommand { } } -fn drop(args: CommandArgs) -> Result { - let (Arguments { columns }, input) = args.process()?; +fn drop(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let columns: Option> = args.opt(0)?; let to_drop = if let Some(quantity) = columns { *quantity as usize @@ -56,7 +52,8 @@ fn drop(args: CommandArgs) -> Result { 1 }; - Ok(input + Ok(args + .input .map(move |item| { let headers = item.data_descriptors(); @@ -66,10 +63,9 @@ fn drop(args: CommandArgs) -> Result { n => &headers[..n - to_drop], }; - select_fields(&item, descs, item.tag()) + Ok(select_fields(&item, descs, item.tag())) }) - .map(ReturnSuccess::value) - .to_action_stream()) + .to_input_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/drop/command.rs b/crates/nu-command/src/commands/drop/command.rs index 230f93ad6..01b575937 100644 --- a/crates/nu-command/src/commands/drop/command.rs +++ b/crates/nu-command/src/commands/drop/command.rs @@ -6,11 +6,6 @@ use nu_source::Tagged; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - rows: Option>, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "drop" @@ -28,7 +23,7 @@ impl WholeStreamCommand for Command { "Remove the last number of rows or columns." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { drop(args) } @@ -51,9 +46,10 @@ impl WholeStreamCommand for Command { } } -fn drop(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process()?; - let v: Vec<_> = input.into_vec(); +fn drop(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let rows: Option> = args.opt(0)?; + let v: Vec<_> = args.input.into_vec(); let rows_to_drop = if let Some(quantity) = rows { *quantity as usize @@ -62,7 +58,7 @@ fn drop(args: CommandArgs) -> Result { }; Ok(if rows_to_drop == 0 { - v.into_iter().to_action_stream() + v.into_iter().map(Ok).to_input_stream() } else { let k = if v.len() < rows_to_drop { 0 @@ -70,8 +66,8 @@ fn drop(args: CommandArgs) -> Result { v.len() - rows_to_drop }; - let iter = v.into_iter().take(k); + let iter = v.into_iter().map(Ok).take(k); - iter.to_action_stream() + iter.to_input_stream() }) } diff --git a/crates/nu-command/src/commands/each/group.rs b/crates/nu-command/src/commands/each/group.rs index dfbae193f..ce11f5065 100644 --- a/crates/nu-command/src/commands/each/group.rs +++ b/crates/nu-command/src/commands/each/group.rs @@ -7,17 +7,9 @@ use nu_protocol::{ Signature, SyntaxShape, UntaggedValue, Value, }; use nu_source::Tagged; -use serde::Deserialize; pub struct EachGroup; -#[derive(Deserialize)] -pub struct EachGroupArgs { - group_size: Tagged, - block: CapturedBlock, - //numbered: Tagged, -} - impl WholeStreamCommand for EachGroup { fn name(&self) -> &str { "each group" @@ -45,21 +37,24 @@ impl WholeStreamCommand for EachGroup { }] } - fn run_with_actions(&self, raw_args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); let external_redirection = raw_args.call_info.args.external_redirection; - let (each_args, input): (EachGroupArgs, _) = raw_args.process()?; - let block = Arc::new(Box::new(each_args.block)); + let args = raw_args.evaluate_once()?; + + let group_size: Tagged = args.req(0)?; + let block: CapturedBlock = args.req(1)?; + let block = Arc::new(Box::new(block)); let each_group_iterator = EachGroupIterator { block, context, - group_size: each_args.group_size.item, - input, + group_size: group_size.item, + input: args.input, external_redirection, }; - Ok(each_group_iterator.flatten().to_action_stream()) + Ok(each_group_iterator.flatten().map(Ok).to_input_stream()) } } diff --git a/crates/nu-command/src/commands/each/window.rs b/crates/nu-command/src/commands/each/window.rs index 26ad9028f..bc412705a 100644 --- a/crates/nu-command/src/commands/each/window.rs +++ b/crates/nu-command/src/commands/each/window.rs @@ -5,17 +5,9 @@ use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; -use serde::Deserialize; pub struct EachWindow; -#[derive(Deserialize)] -pub struct EachWindowArgs { - window_size: Tagged, - block: CapturedBlock, - stride: Option>, -} - impl WholeStreamCommand for EachWindow { fn name(&self) -> &str { "each window" @@ -49,22 +41,29 @@ impl WholeStreamCommand for EachWindow { }] } - fn run_with_actions(&self, raw_args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); let external_redirection = raw_args.call_info.args.external_redirection; - let (each_args, mut input): (EachWindowArgs, _) = raw_args.process()?; - let block = Arc::new(Box::new(each_args.block)); - let mut window: Vec<_> = input + let mut args = raw_args.evaluate_once()?; + let window_size: Tagged = args.req(0)?; + let block: CapturedBlock = args.req(1)?; + let stride: Option> = args.get_flag("stride")?; + + let block = Arc::new(Box::new(block)); + + let mut window: Vec<_> = args + .input .by_ref() - .take(*each_args.window_size - 1) + .take(*window_size - 1) .collect::>(); // `window` must start with dummy values, which will be removed on the first iteration - let stride = each_args.stride.map(|x| *x).unwrap_or(1); + let stride = stride.map(|x| *x).unwrap_or(1); window.insert(0, UntaggedValue::Primitive(Primitive::Nothing).into()); - Ok(input + Ok(args + .input .enumerate() .map(move |(i, input)| { // This would probably be more efficient if `last` was a VecDeque @@ -89,7 +88,8 @@ impl WholeStreamCommand for EachWindow { }) .flatten() .flatten() - .to_action_stream()) + .map(Ok) + .to_input_stream()) } } diff --git a/crates/nu-command/src/commands/format/command.rs b/crates/nu-command/src/commands/format/command.rs index 2f1dac234..e6917edf7 100644 --- a/crates/nu-command/src/commands/format/command.rs +++ b/crates/nu-command/src/commands/format/command.rs @@ -2,17 +2,12 @@ use crate::prelude::*; use nu_engine::evaluate_baseline_expr; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; use std::borrow::Borrow; pub struct Format; -#[derive(Deserialize)] -pub struct FormatArgs { - pattern: Tagged, -} - impl WholeStreamCommand for Format { fn name(&self) -> &str { "format" @@ -30,7 +25,7 @@ impl WholeStreamCommand for Format { "Format columns into a string using a simple pattern." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { format_command(args) } @@ -43,14 +38,16 @@ impl WholeStreamCommand for Format { } } -fn format_command(args: CommandArgs) -> Result { +fn format_command(args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let (FormatArgs { pattern }, input) = args.process()?; + let args = args.evaluate_once()?; + let pattern: Tagged = args.req(0)?; let format_pattern = format(&pattern); let commands = Arc::new(format_pattern); - Ok(input + Ok(args + .input .map(move |value| { let mut output = String::new(); let commands = commands.clone(); @@ -82,9 +79,9 @@ fn format_command(args: CommandArgs) -> Result { } } - ReturnSuccess::value(UntaggedValue::string(output).into_untagged_value()) + Ok(UntaggedValue::string(output).into_untagged_value()) }) - .to_action_stream()) + .to_input_stream()) } #[derive(Debug)] diff --git a/crates/nu-command/src/commands/format/format_filesize.rs b/crates/nu-command/src/commands/format/format_filesize.rs index 4b5189732..ef70bae96 100644 --- a/crates/nu-command/src/commands/format/format_filesize.rs +++ b/crates/nu-command/src/commands/format/format_filesize.rs @@ -2,20 +2,12 @@ use crate::prelude::*; use nu_data::base::shape::InlineShape; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - ColumnPath, Primitive::Filesize, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive::Filesize, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use nu_value_ext::get_data_by_column_path; pub struct FileSize; -#[derive(Deserialize)] -pub struct Arguments { - field: ColumnPath, - format: Tagged, -} - impl WholeStreamCommand for FileSize { fn name(&self) -> &str { "format filesize" @@ -39,7 +31,7 @@ impl WholeStreamCommand for FileSize { "Converts a column of filesizes to some specified format" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { filesize(args) } @@ -63,50 +55,55 @@ fn process_row( input: Value, format: Tagged, field: Arc, -) -> Result { - Ok({ - let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error); - match replace_for { - Ok(s) => { - if let Value { - value: UntaggedValue::Primitive(Filesize(fs)), - .. - } = s - { - let byte_format = InlineShape::format_bytes(&fs, Some(&format.item)); - let byte_value = Value::from(byte_format.1); - ActionStream::one(ReturnSuccess::value( - input.replace_data_at_column_path(&field, byte_value).expect("Given that the existence check was already done, this shouldn't trigger never"), - )) - } else { - return Err(ShellError::labeled_error( - "the data in this row is not of the type filesize", - "invalid datatype in row", - input.tag(), - )); - } +) -> Result { + let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error); + match replace_for { + Ok(s) => { + if let Value { + value: UntaggedValue::Primitive(Filesize(fs)), + .. + } = s + { + let byte_format = InlineShape::format_bytes(&fs, Some(&format.item)); + let byte_value = Value::from(byte_format.1); + Ok(input + .replace_data_at_column_path(&field, byte_value) + .expect( + "Given that the existence check was already done, this shouldn't trigger never", + )) + } else { + Err(ShellError::labeled_error( + "the data in this row is not of the type filesize", + "invalid datatype in row", + input.tag(), + )) } - Err(e) => ActionStream::one(Err(e)), } - }) + Err(e) => Err(e), + } } -fn filesize(raw_args: CommandArgs) -> Result { - let (Arguments { field, format }, input) = raw_args.process()?; +fn filesize(raw_args: CommandArgs) -> Result { + let args = raw_args.evaluate_once()?; + + let field: ColumnPath = args.req(0)?; + let format: Tagged = args.req(1)?; let field = Arc::new(field); - Ok(input + Ok(args + .input .map(move |input| { let format = format.clone(); let field = field.clone(); match process_row(input, format, field) { - Ok(s) => s, - Err(e) => ActionStream::one(Err(e)), + Ok(s) => Ok(s), + Err(e) => Err(e), } }) .flatten() - .to_action_stream()) + .map(Ok) + .to_input_stream()) } #[cfg(test)]