diff --git a/crates/nu-command/src/commands/hash_/base64_.rs b/crates/nu-command/src/commands/hash_/base64_.rs index 74e8bf1331..d0feeeaca5 100644 --- a/crates/nu-command/src/commands/hash_/base64_.rs +++ b/crates/nu-command/src/commands/hash_/base64_.rs @@ -2,21 +2,11 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; -use nu_protocol::{ - ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::{Tag, Tagged}; use base64::{decode_config, encode_config}; -#[derive(Deserialize)] -pub struct Arguments { - pub rest: Vec, - pub character_set: Option>, - pub encode: Tagged, - pub decode: Tagged, -} - #[derive(Clone)] pub struct Base64Config { pub character_set: String, @@ -46,13 +36,13 @@ impl WholeStreamCommand for SubCommand { Some('c'), ) .switch( - "encode", - "encode the input as base64. This is the default behavior if not specified.", + "encode", + "encode the input as base64. This is the default behavior if not specified.", Some('e') ) .switch( - "decode", - "decode the input from base64", + "decode", + "decode the input from base64", Some('d')) .rest( SyntaxShape::ColumnPath, @@ -64,7 +54,7 @@ impl WholeStreamCommand for SubCommand { "base64 encode or decode a value" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { operate(args) } @@ -95,29 +85,25 @@ impl WholeStreamCommand for SubCommand { } } -fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); + let args = args.evaluate_once()?; - let ( - Arguments { - encode, - decode, - character_set, - rest, - }, - input, - ) = args.process()?; + let encode = args.has_flag("encode"); + let decode = args.has_flag("decode"); + let character_set: Option> = args.get_flag("character_set")?; + let column_paths: Vec = args.rest(0)?; - if encode.item && decode.item { - return Ok(ActionStream::one(Err(ShellError::labeled_error( + if encode && decode { + return Err(ShellError::labeled_error( "only one of --decode and --encode flags can be used", "conflicting flags", name_tag, - )))); + )); } // Default the action to be encoding if no flags are specified. - let action_type = if *decode.item() { + let action_type = if decode { ActionType::Decode } else { ActionType::Encode @@ -134,12 +120,11 @@ fn operate(args: CommandArgs) -> Result { action_type, }; - let column_paths: Vec<_> = rest; - - Ok(input + Ok(args + .input .map(move |v| { if column_paths.is_empty() { - ReturnSuccess::value(action(&v, &encoding_config, v.tag())?) + action(&v, &encoding_config, v.tag()) } else { let mut ret = v; @@ -151,10 +136,10 @@ fn operate(args: CommandArgs) -> Result { )?; } - ReturnSuccess::value(ret) + Ok(ret) } }) - .to_action_stream()) + .to_input_stream()) } fn action( @@ -182,7 +167,7 @@ fn action( return Err(ShellError::labeled_error( "value is not an accepted character set", format!( - "{} is not a valid character-set.\nPlease use `help hash base64` to see a list of valid character sets.", + "{} is not a valid character-set.\nPlease use `help hash base64` to see a list of valid character sets.", &base64_config.character_set ), tag.into().span, diff --git a/crates/nu-command/src/commands/hash_/command.rs b/crates/nu-command/src/commands/hash_/command.rs index b5c8091c16..0709b8113e 100644 --- a/crates/nu-command/src/commands/hash_/command.rs +++ b/crates/nu-command/src/commands/hash_/command.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; pub struct Command; @@ -21,10 +21,10 @@ impl WholeStreamCommand for Command { "Apply hash function." } - fn run_with_actions(&self, args: CommandArgs) -> Result { - Ok(ActionStream::one(ReturnSuccess::value( + fn run(&self, args: CommandArgs) -> Result { + Ok(OutputStream::one( UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()), - ))) + )) } } diff --git a/crates/nu-command/src/commands/hash_/md5_.rs b/crates/nu-command/src/commands/hash_/md5_.rs index 1f9cfce07a..e3eab879dc 100644 --- a/crates/nu-command/src/commands/hash_/md5_.rs +++ b/crates/nu-command/src/commands/hash_/md5_.rs @@ -2,16 +2,9 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; -use nu_protocol::{ - ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tag; -#[derive(Deserialize)] -pub struct Arguments { - pub rest: Vec, -} - pub struct SubCommand; impl WholeStreamCommand for SubCommand { @@ -30,7 +23,7 @@ impl WholeStreamCommand for SubCommand { "md5 encode a value" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { operate(args) } @@ -56,15 +49,15 @@ impl WholeStreamCommand for SubCommand { } } -fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process()?; +fn operate(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let column_paths: Vec = args.rest(0)?; - let column_paths: Vec<_> = rest; - - Ok(input + Ok(args + .input .map(move |v| { if column_paths.is_empty() { - ReturnSuccess::value(action(&v, v.tag())?) + action(&v, v.tag()) } else { let mut ret = v; @@ -75,10 +68,10 @@ fn operate(args: CommandArgs) -> Result { )?; } - ReturnSuccess::value(ret) + Ok(ret) } }) - .to_action_stream()) + .to_input_stream()) } fn action(input: &Value, tag: impl Into) -> Result { diff --git a/crates/nu-command/src/commands/into/binary.rs b/crates/nu-command/src/commands/into/binary.rs index 66512fb99f..00c14fcd27 100644 --- a/crates/nu-command/src/commands/into/binary.rs +++ b/crates/nu-command/src/commands/into/binary.rs @@ -1,20 +1,11 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use num_bigint::{BigInt, ToBigInt}; pub struct SubCommand; -#[derive(Deserialize)] -pub struct Arguments { - pub rest: Vec, - pub skip: Option, - pub bytes: Option, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "into binary" @@ -44,7 +35,7 @@ impl WholeStreamCommand for SubCommand { "Convert value to a binary primitive" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { into_binary(args) } @@ -121,20 +112,17 @@ impl WholeStreamCommand for SubCommand { } } -fn into_binary(args: CommandArgs) -> Result { - let ( - Arguments { - rest: column_paths, - skip, - bytes, - }, - input, - ) = args.process()?; +fn into_binary(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let skip: Option = args.get_flag("skip")?; + let bytes: Option = args.get_flag("bytes")?; + let column_paths: Vec = args.rest(0)?; - Ok(input + Ok(args + .input .map(move |v| { if column_paths.is_empty() { - ReturnSuccess::value(action(&v, v.tag(), &skip, &bytes)?) + action(&v, v.tag(), &skip, &bytes) } else { let mut ret = v; for path in &column_paths { @@ -146,10 +134,10 @@ fn into_binary(args: CommandArgs) -> Result { )?; } - ReturnSuccess::value(ret) + Ok(ret) } }) - .to_action_stream()) + .to_input_stream()) } fn int_to_endian(n: i64) -> Vec { diff --git a/crates/nu-command/src/commands/into/command.rs b/crates/nu-command/src/commands/into/command.rs index 4e90f0878f..3ae2ff8972 100644 --- a/crates/nu-command/src/commands/into/command.rs +++ b/crates/nu-command/src/commands/into/command.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; +use nu_protocol::{Signature, UntaggedValue}; pub struct Command; @@ -18,10 +18,10 @@ impl WholeStreamCommand for Command { "Apply into function." } - fn run_with_actions(&self, args: CommandArgs) -> Result { - Ok(ActionStream::one(ReturnSuccess::value( + fn run(&self, args: CommandArgs) -> Result { + Ok(OutputStream::one( UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()), - ))) + )) } } diff --git a/crates/nu-command/src/commands/into/int.rs b/crates/nu-command/src/commands/into/int.rs index 28aded4649..7a4c10f2f8 100644 --- a/crates/nu-command/src/commands/into/int.rs +++ b/crates/nu-command/src/commands/into/int.rs @@ -1,18 +1,11 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use num_bigint::ToBigInt; pub struct SubCommand; -#[derive(Deserialize)] -pub struct Arguments { - pub rest: Vec, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "into int" @@ -29,7 +22,7 @@ impl WholeStreamCommand for SubCommand { "Convert value to integer" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { into_int(args) } @@ -85,13 +78,15 @@ impl WholeStreamCommand for SubCommand { } } -fn into_int(args: CommandArgs) -> Result { - let (Arguments { rest: column_paths }, input) = args.process()?; +fn into_int(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let column_paths: Vec = args.rest(0)?; - Ok(input + Ok(args + .input .map(move |v| { if column_paths.is_empty() { - ReturnSuccess::value(action(&v, v.tag())?) + action(&v, v.tag()) } else { let mut ret = v; for path in &column_paths { @@ -101,10 +96,10 @@ fn into_int(args: CommandArgs) -> Result { )?; } - ReturnSuccess::value(ret) + Ok(ret) } }) - .to_action_stream()) + .to_input_stream()) } pub fn action(input: &Value, tag: impl Into) -> Result { diff --git a/crates/nu-command/src/commands/into/string.rs b/crates/nu-command/src/commands/into/string.rs index aa7032bc39..0b1ca9ae5f 100644 --- a/crates/nu-command/src/commands/into/string.rs +++ b/crates/nu-command/src/commands/into/string.rs @@ -1,9 +1,7 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use num_bigint::{BigInt, BigUint, ToBigInt}; // TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml) @@ -14,13 +12,6 @@ use std::iter; pub struct SubCommand; -#[derive(Deserialize)] -pub struct Arguments { - decimals: Option>, - group_digits: bool, - column_paths: Vec, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "into string" @@ -44,7 +35,7 @@ impl WholeStreamCommand for SubCommand { "Convert value to string" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { into_string(args) } @@ -89,35 +80,33 @@ impl WholeStreamCommand for SubCommand { } } -fn into_string(args: CommandArgs) -> Result { - let (options, input) = args.extract(|params| { - Ok(Arguments { - decimals: params.get_flag("decimals")?, - group_digits: false, - column_paths: params.rest_args()?, - }) - })?; +fn into_string(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; - let digits = options.decimals.as_ref().map(|tagged| tagged.item); - let group_digits = options.group_digits; + let decimals: Option> = args.get_flag("decimals")?; + let column_paths: Vec = args.rest(0)?; - Ok(input + let digits = decimals.as_ref().map(|tagged| tagged.item); + let group_digits = false; + + Ok(args + .input .map(move |v| { - if options.column_paths.is_empty() { - ReturnSuccess::value(action(&v, v.tag(), digits, group_digits)?) + if column_paths.is_empty() { + action(&v, v.tag(), digits, group_digits) } else { let mut ret = v; - for path in &options.column_paths { + for path in &column_paths { ret = ret.swap_data_by_column_path( path, Box::new(move |old| action(old, old.tag(), digits, group_digits)), )?; } - ReturnSuccess::value(ret) + Ok(ret) } }) - .to_action_stream()) + .to_input_stream()) } pub fn action( diff --git a/crates/nu-command/src/commands/keep/command.rs b/crates/nu-command/src/commands/keep/command.rs index 06b356f94c..b78a327aea 100644 --- a/crates/nu-command/src/commands/keep/command.rs +++ b/crates/nu-command/src/commands/keep/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 { "keep" @@ -28,7 +23,7 @@ impl WholeStreamCommand for Command { "Keep the number of rows only." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { keep(args) } @@ -53,15 +48,17 @@ impl WholeStreamCommand for Command { } } -fn keep(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process()?; +fn keep(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let rows: Option> = args.opt(0)?; + let rows_desired = if let Some(quantity) = rows { *quantity } else { 1 }; - Ok(input.take(rows_desired).to_action_stream()) + Ok(args.input.take(rows_desired).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/keep/until.rs b/crates/nu-command/src/commands/keep/until.rs index 9313e9532c..a9b4caec08 100644 --- a/crates/nu-command/src/commands/keep/until.rs +++ b/crates/nu-command/src/commands/keep/until.rs @@ -27,7 +27,7 @@ impl WholeStreamCommand for SubCommand { "Keeps rows until the condition matches." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); let call_info = args.evaluate_once()?; @@ -93,7 +93,7 @@ impl WholeStreamCommand for SubCommand { !matches!(result, Ok(ref v) if v.is_true()) }) - .to_action_stream()) + .to_output_stream()) } } diff --git a/crates/nu-command/src/commands/keep/while_.rs b/crates/nu-command/src/commands/keep/while_.rs index 0688e0d551..5c1150bcdd 100644 --- a/crates/nu-command/src/commands/keep/while_.rs +++ b/crates/nu-command/src/commands/keep/while_.rs @@ -26,7 +26,7 @@ impl WholeStreamCommand for SubCommand { "Keeps rows while the condition matches." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); let call_info = args.evaluate_once()?; @@ -92,7 +92,7 @@ impl WholeStreamCommand for SubCommand { matches!(result, Ok(ref v) if v.is_true()) }) - .to_action_stream()) + .to_output_stream()) } }