diff --git a/crates/nu-command/src/commands/math/abs.rs b/crates/nu-command/src/commands/math/abs.rs index 0ed327b268..bec1cb633c 100644 --- a/crates/nu-command/src/commands/math/abs.rs +++ b/crates/nu-command/src/commands/math/abs.rs @@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand { "Returns absolute values of a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let mapped = args.input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => { UntaggedValue::int(val.magnitude().clone()).into() @@ -31,7 +31,7 @@ impl WholeStreamCommand for SubCommand { } other => abs_default(other), }); - Ok(ActionStream::from_input(mapped)) + Ok(mapped.to_output_stream()) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/avg.rs b/crates/nu-command/src/commands/math/avg.rs index 84f46a8e4e..13f7ccd74c 100644 --- a/crates/nu-command/src/commands/math/avg.rs +++ b/crates/nu-command/src/commands/math/avg.rs @@ -27,7 +27,7 @@ impl WholeStreamCommand for SubCommand { "Finds the average of a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, average) } diff --git a/crates/nu-command/src/commands/math/ceil.rs b/crates/nu-command/src/commands/math/ceil.rs index 7b10387040..c981592a50 100644 --- a/crates/nu-command/src/commands/math/ceil.rs +++ b/crates/nu-command/src/commands/math/ceil.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Applies the ceil function to a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let input = args.input; run_with_numerical_functions_on_stream(input, ceil_big_int, ceil_big_decimal, ceil_default) diff --git a/crates/nu-command/src/commands/math/command.rs b/crates/nu-command/src/commands/math/command.rs index 6b8f546fca..dee791ce66 100644 --- a/crates/nu-command/src/commands/math/command.rs +++ b/crates/nu-command/src/commands/math/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 { "Use mathematical functions as aggregate functions on a list of numbers or tables." } - fn run_with_actions(&self, args: CommandArgs) -> Result { - Ok(ActionStream::one(Ok(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/math/eval.rs b/crates/nu-command/src/commands/math/eval.rs index 9077802271..56e795f774 100644 --- a/crates/nu-command/src/commands/math/eval.rs +++ b/crates/nu-command/src/commands/math/eval.rs @@ -1,16 +1,11 @@ use crate::prelude::*; 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 SubCommand; -#[derive(Deserialize)] -pub struct SubCommandArgs { - expression: Option>, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math eval" @@ -28,7 +23,7 @@ impl WholeStreamCommand for SubCommand { ) } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { eval(args) } @@ -45,13 +40,15 @@ impl WholeStreamCommand for SubCommand { } } -pub fn eval(args: CommandArgs) -> Result { - let name = args.call_info.name_tag.span; - let (SubCommandArgs { expression }, input) = args.process()?; +pub fn eval(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let expression: Option, ShellError>> = args.opt(0); + let name = args.call_info.name_tag.clone(); + let input = args.input; - if let Some(string) = expression { + if let Some(Ok(string)) = expression { match parse(&string, &string.tag) { - Ok(value) => Ok(ActionStream::one(ReturnSuccess::value(value))), + Ok(value) => Ok(OutputStream::one(value)), Err(err) => Err(ShellError::labeled_error( "Math evaluation error", err, @@ -59,12 +56,12 @@ pub fn eval(args: CommandArgs) -> Result { )), } } else { - Ok(input + let mapped: Result, _> = input .map(move |x| { - if let Some(Tagged { + if let Some(Ok(Tagged { tag, item: expression, - }) = &expression + })) = &expression { UntaggedValue::string(expression).into_value(tag) } else { @@ -74,7 +71,7 @@ pub fn eval(args: CommandArgs) -> Result { .map(move |input| { if let Ok(string) = input.as_string() { match parse(&string, &input.tag) { - Ok(value) => ReturnSuccess::value(value), + Ok(value) => Ok(value), Err(err) => Err(ShellError::labeled_error( "Math evaluation error", err, @@ -85,11 +82,15 @@ pub fn eval(args: CommandArgs) -> Result { Err(ShellError::labeled_error( "Expected a string from pipeline", "requires string input", - name, + name.clone(), )) } }) - .to_action_stream()) + .collect(); + match mapped { + Ok(values) => Ok(OutputStream::from(values)), + Err(e) => Err(e), + } } } diff --git a/crates/nu-command/src/commands/math/floor.rs b/crates/nu-command/src/commands/math/floor.rs index 1fa82dd6e1..d5da9f6a05 100644 --- a/crates/nu-command/src/commands/math/floor.rs +++ b/crates/nu-command/src/commands/math/floor.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Applies the floor function to a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let input = args.input; run_with_numerical_functions_on_stream( diff --git a/crates/nu-command/src/commands/math/max.rs b/crates/nu-command/src/commands/math/max.rs index 9cd9ca3620..0facfafcc0 100644 --- a/crates/nu-command/src/commands/math/max.rs +++ b/crates/nu-command/src/commands/math/max.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Finds the maximum within a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, maximum) } diff --git a/crates/nu-command/src/commands/math/median.rs b/crates/nu-command/src/commands/math/median.rs index 519bdc0ff4..de7a4a9b76 100644 --- a/crates/nu-command/src/commands/math/median.rs +++ b/crates/nu-command/src/commands/math/median.rs @@ -24,7 +24,7 @@ impl WholeStreamCommand for SubCommand { "Gets the median of a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, median) } diff --git a/crates/nu-command/src/commands/math/min.rs b/crates/nu-command/src/commands/math/min.rs index c747700adf..2ed64a0c44 100644 --- a/crates/nu-command/src/commands/math/min.rs +++ b/crates/nu-command/src/commands/math/min.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Finds the minimum within a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, minimum) } diff --git a/crates/nu-command/src/commands/math/mode.rs b/crates/nu-command/src/commands/math/mode.rs index 932e6ed202..112808ebf2 100644 --- a/crates/nu-command/src/commands/math/mode.rs +++ b/crates/nu-command/src/commands/math/mode.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Gets the most frequent element(s) from a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, mode) } diff --git a/crates/nu-command/src/commands/math/product.rs b/crates/nu-command/src/commands/math/product.rs index 8c36f38b49..62decd9ba4 100644 --- a/crates/nu-command/src/commands/math/product.rs +++ b/crates/nu-command/src/commands/math/product.rs @@ -20,7 +20,7 @@ impl WholeStreamCommand for SubCommand { "Finds the product of a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, product) } diff --git a/crates/nu-command/src/commands/math/round.rs b/crates/nu-command/src/commands/math/round.rs index 60910b77eb..b10f6d07c0 100644 --- a/crates/nu-command/src/commands/math/round.rs +++ b/crates/nu-command/src/commands/math/round.rs @@ -6,11 +6,6 @@ use nu_source::Tagged; pub struct SubCommand; -#[derive(Deserialize)] -struct Arguments { - precision: Option>, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math round" @@ -29,7 +24,7 @@ impl WholeStreamCommand for SubCommand { "Applies the round function to a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { operate(args) } @@ -57,16 +52,23 @@ impl WholeStreamCommand for SubCommand { } } -fn operate(args: CommandArgs) -> Result { - let (Arguments { precision }, input) = args.process()?; - let precision = precision.map(|p| p.item).unwrap_or(0); - +fn operate(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + let precision: Option, ShellError>> = args.get_flag("precision"); + let input = args.input; + let precision = if let Some(precision) = precision { + precision?.item + } else { + 0 + }; let mapped = input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => round_big_int(val), - UntaggedValue::Primitive(Primitive::Decimal(val)) => round_big_decimal(val, precision), + UntaggedValue::Primitive(Primitive::Decimal(val)) => { + round_big_decimal(val, precision.into()) + } other => round_default(other), }); - Ok(ActionStream::from_input(mapped)) + Ok(mapped.to_output_stream()) } fn round_big_int(val: BigInt) -> Value { diff --git a/crates/nu-command/src/commands/math/sqrt.rs b/crates/nu-command/src/commands/math/sqrt.rs index a02ba1647d..92449a3b0b 100644 --- a/crates/nu-command/src/commands/math/sqrt.rs +++ b/crates/nu-command/src/commands/math/sqrt.rs @@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand { "Applies the square root function to a list of numbers" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(operate(args)) } @@ -34,13 +34,13 @@ impl WholeStreamCommand for SubCommand { } } -fn operate(args: CommandArgs) -> ActionStream { +fn operate(args: CommandArgs) -> OutputStream { let mapped = args.input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => sqrt_big_decimal(BigDecimal::from(val)), UntaggedValue::Primitive(Primitive::Decimal(val)) => sqrt_big_decimal(val), other => sqrt_default(other), }); - ActionStream::from_input(mapped) + mapped.to_output_stream() } fn sqrt_big_decimal(val: BigDecimal) -> Value { diff --git a/crates/nu-command/src/commands/math/stddev.rs b/crates/nu-command/src/commands/math/stddev.rs index 3ad5e05989..c979f7d70a 100644 --- a/crates/nu-command/src/commands/math/stddev.rs +++ b/crates/nu-command/src/commands/math/stddev.rs @@ -2,17 +2,11 @@ use super::variance::compute_variance as variance; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; -use nu_source::Tagged; +use nu_protocol::{Dictionary, Primitive, Signature, UntaggedValue, Value}; use std::str::FromStr; pub struct SubCommand; -#[derive(Deserialize)] -struct Arguments { - sample: Tagged, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math stddev" @@ -30,13 +24,14 @@ impl WholeStreamCommand for SubCommand { "Finds the stddev of a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { + let mut args = raw_args.evaluate_once()?; + + let sample: bool = args.has_flag("sample"); + let values: Vec = args.input.drain_vec(); let name = args.call_info.name_tag.clone(); - let (Arguments { sample }, mut input) = args.process()?; - let values: Vec = input.drain_vec(); - - let n = if let Tagged { item: true, .. } = sample { + let n = if sample { values.len() - 1 } else { values.len() @@ -81,13 +76,11 @@ impl WholeStreamCommand for SubCommand { }?; if res.value.is_table() { - Ok(ActionStream::from( - res.table_entries() - .map(|v| ReturnSuccess::value(v.clone())) - .collect::>(), + Ok(OutputStream::from( + res.table_entries().cloned().collect::>(), )) } else { - Ok(ActionStream::one(ReturnSuccess::value(res))) + Ok(OutputStream::one(res)) } } diff --git a/crates/nu-command/src/commands/math/sum.rs b/crates/nu-command/src/commands/math/sum.rs index 224685e3c0..0f09a19831 100644 --- a/crates/nu-command/src/commands/math/sum.rs +++ b/crates/nu-command/src/commands/math/sum.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand { "Finds the sum of a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_function(args, summation) } diff --git a/crates/nu-command/src/commands/math/utils.rs b/crates/nu-command/src/commands/math/utils.rs index fb4cd5d2d5..49378a6622 100644 --- a/crates/nu-command/src/commands/math/utils.rs +++ b/crates/nu-command/src/commands/math/utils.rs @@ -1,6 +1,6 @@ use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{Dictionary, Primitive, ReturnSuccess, UntaggedValue, Value}; +use nu_protocol::{Dictionary, Primitive, UntaggedValue, Value}; use indexmap::map::IndexMap; @@ -9,7 +9,7 @@ pub type MathFunction = fn(values: &[Value], tag: &Tag) -> Result, mf: MathFunction, -) -> Result { +) -> Result { let RunnableContext { mut input, call_info, @@ -23,13 +23,11 @@ pub fn run_with_function( match res { Ok(v) => { if v.value.is_table() { - Ok(ActionStream::from( - v.table_entries() - .map(|v| ReturnSuccess::value(v.clone())) - .collect::>(), + Ok(OutputStream::from( + v.table_entries().cloned().collect::>(), )) } else { - Ok(ActionStream::one(ReturnSuccess::value(v))) + Ok(OutputStream::one(v)) } } Err(e) => Err(e), @@ -47,13 +45,13 @@ pub fn run_with_numerical_functions_on_stream( int_function: IntFunction, decimal_function: DecimalFunction, default_function: DefaultFunction, -) -> Result { +) -> Result { let mapped = input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => int_function(val), UntaggedValue::Primitive(Primitive::Decimal(val)) => decimal_function(val), other => default_function(other), }); - Ok(ActionStream::from_input(mapped)) + Ok(mapped.to_output_stream()) } pub fn calculate(values: &[Value], name: &Tag, mf: MathFunction) -> Result { diff --git a/crates/nu-command/src/commands/math/variance.rs b/crates/nu-command/src/commands/math/variance.rs index dc06e646d6..7e991420d3 100644 --- a/crates/nu-command/src/commands/math/variance.rs +++ b/crates/nu-command/src/commands/math/variance.rs @@ -3,18 +3,10 @@ use bigdecimal::FromPrimitive; use nu_data::value::compute_values; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - hir::Operator, Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value, -}; -use nu_source::Tagged; +use nu_protocol::{hir::Operator, Dictionary, Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[derive(Deserialize)] -struct Arguments { - sample: Tagged, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math variance" @@ -28,13 +20,14 @@ impl WholeStreamCommand for SubCommand { "Finds the variance of a list of numbers or tables" } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { + let mut args = raw_args.evaluate_once()?; + + let sample: bool = args.has_flag("sample"); + let values: Vec = args.input.drain_vec(); let name = args.call_info.name_tag.clone(); - let (Arguments { sample }, mut input) = args.process()?; - let values: Vec = input.drain_vec(); - - let n = if let Tagged { item: true, .. } = sample { + let n = if sample { values.len() - 1 } else { values.len() @@ -79,13 +72,11 @@ impl WholeStreamCommand for SubCommand { }?; if res.value.is_table() { - Ok(ActionStream::from( - res.table_entries() - .map(|v| ReturnSuccess::value(v.clone())) - .collect::>(), + Ok(OutputStream::from( + res.table_entries().cloned().collect::>(), )) } else { - Ok(ActionStream::one(ReturnSuccess::value(res))) + Ok(OutputStream::one(res)) } }