Switch to "engine-p" (#3270)

* WIP

* WIP

* first builds

* Tests pass
This commit is contained in:
Jonathan Turner
2021-04-07 04:19:43 +12:00
committed by GitHub
parent ad1c4f5e39
commit 073e5727c6
262 changed files with 2269 additions and 2660 deletions

View File

@ -5,7 +5,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math abs"
@ -19,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
"Returns absolute values of a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let mapped = args.input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => {
UntaggedValue::int(val.magnitude().clone()).into()

View File

@ -14,7 +14,6 @@ use bigdecimal::FromPrimitive;
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math avg"
@ -28,8 +27,8 @@ impl WholeStreamCommand for SubCommand {
"Finds the average of a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), average).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), average)
}
fn examples(&self) -> Vec<Example> {

View File

@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math ceil"
@ -21,14 +20,13 @@ impl WholeStreamCommand for SubCommand {
"Applies the ceil function to a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_numerical_functions_on_stream(
RunnableContext::from_command_args(args),
ceil_big_int,
ceil_big_decimal,
ceil_default,
)
.await
}
fn examples(&self) -> Vec<Example> {

View File

@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct Command;
#[async_trait]
impl WholeStreamCommand for Command {
fn name(&self) -> &str {
"math"
@ -19,7 +18,7 @@ impl WholeStreamCommand for Command {
"Use mathematical functions as aggregate functions on a list of numbers or tables."
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()),
))))

View File

@ -11,7 +11,6 @@ pub struct SubCommandArgs {
expression: Option<Tagged<String>>,
}
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math eval"
@ -29,8 +28,8 @@ impl WholeStreamCommand for SubCommand {
)
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
eval(args).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
eval(args)
}
fn examples(&self) -> Vec<Example> {
@ -46,9 +45,9 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn eval(args: CommandArgs) -> Result<OutputStream, ShellError> {
pub fn eval(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.span;
let (SubCommandArgs { expression }, input) = args.process().await?;
let (SubCommandArgs { expression }, input) = args.process()?;
if let Some(string) = expression {
match parse(&string, &string.tag) {

View File

@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math floor"
@ -21,14 +20,13 @@ impl WholeStreamCommand for SubCommand {
"Applies the floor function to a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_numerical_functions_on_stream(
RunnableContext::from_command_args(args),
floor_big_int,
floor_big_decimal,
floor_default,
)
.await
}
fn examples(&self) -> Vec<Example> {

View File

@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math max"
@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand {
"Finds the maximum within a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), maximum).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), maximum)
}
fn examples(&self) -> Vec<Example> {

View File

@ -11,7 +11,6 @@ use nu_protocol::{
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math median"
@ -25,8 +24,8 @@ impl WholeStreamCommand for SubCommand {
"Gets the median of a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), median).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), median)
}
fn examples(&self) -> Vec<Example> {

View File

@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math min"
@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand {
"Finds the minimum within a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), minimum).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), minimum)
}
fn examples(&self) -> Vec<Example> {

View File

@ -7,7 +7,6 @@ use std::cmp::Ordering;
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math mode"
@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand {
"Gets the most frequent element(s) from a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), mode).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), mode)
}
fn examples(&self) -> Vec<Example> {

View File

@ -7,7 +7,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math product"
@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand {
"Finds the product of a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), product).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), product)
}
fn examples(&self) -> Vec<Example> {

View File

@ -11,7 +11,6 @@ struct Arguments {
precision: Option<Tagged<i64>>,
}
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math round"
@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand {
"Applies the round function to a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args)
}
fn examples(&self) -> Vec<Example> {
@ -58,8 +57,8 @@ impl WholeStreamCommand for SubCommand {
}
}
async fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { precision }, input) = args.process().await?;
fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { precision }, input) = args.process()?;
let precision = precision.map(|p| p.item).unwrap_or(0);
let mapped = input.map(move |val| match val.value {

View File

@ -5,7 +5,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math sqrt"
@ -19,8 +18,8 @@ impl WholeStreamCommand for SubCommand {
"Applies the square root function to a list of numbers"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(operate(args))
}
fn examples(&self) -> Vec<Example> {
@ -35,13 +34,13 @@ impl WholeStreamCommand for SubCommand {
}
}
async fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
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),
});
Ok(OutputStream::from_input(mapped))
OutputStream::from_input(mapped)
}
fn sqrt_big_decimal(val: BigDecimal) -> Value {

View File

@ -13,7 +13,6 @@ struct Arguments {
sample: Tagged<bool>,
}
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math stddev"
@ -31,11 +30,11 @@ impl WholeStreamCommand for SubCommand {
"Finds the stddev of a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (Arguments { sample }, mut input) = args.process().await?;
let (Arguments { sample }, mut input) = args.process()?;
let values: Vec<Value> = input.drain_vec().await;
let values: Vec<Value> = input.drain_vec();
let n = if let Tagged { item: true, .. } = sample {
values.len() - 1

View File

@ -8,7 +8,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math sum"
@ -22,8 +21,8 @@ impl WholeStreamCommand for SubCommand {
"Finds the sum of a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), summation).await
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), summation)
}
fn examples(&self) -> Vec<Example> {

View File

@ -6,13 +6,13 @@ use indexmap::map::IndexMap;
pub type MathFunction = fn(values: &[Value], tag: &Tag) -> Result<Value, ShellError>;
pub async fn run_with_function(
pub fn run_with_function(
RunnableContext {
mut input, name, ..
}: RunnableContext,
mf: MathFunction,
) -> Result<OutputStream, ShellError> {
let values: Vec<Value> = input.drain_vec().await;
let values: Vec<Value> = input.drain_vec();
let res = calculate(&values, &name, mf);
match res {
@ -37,7 +37,7 @@ pub type DecimalFunction = fn(val: BigDecimal) -> Value;
pub type DefaultFunction = fn(val: UntaggedValue) -> Value;
pub async fn run_with_numerical_functions_on_stream(
pub fn run_with_numerical_functions_on_stream(
RunnableContext { input, .. }: RunnableContext,
int_function: IntFunction,
decimal_function: DecimalFunction,

View File

@ -15,7 +15,6 @@ struct Arguments {
sample: Tagged<bool>,
}
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"math variance"
@ -29,11 +28,11 @@ impl WholeStreamCommand for SubCommand {
"Finds the variance of a list of numbers or tables"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (Arguments { sample }, mut input) = args.process().await?;
let (Arguments { sample }, mut input) = args.process()?;
let values: Vec<Value> = input.drain_vec().await;
let values: Vec<Value> = input.drain_vec();
let n = if let Tagged { item: true, .. } = sample {
values.len() - 1