From b1d7e3aa49496b5ff7c49a3fd4502cd3f90c3912 Mon Sep 17 00:00:00 2001 From: Luccas Mateus Date: Thu, 21 Oct 2021 11:52:26 -0300 Subject: [PATCH 1/7] starting to build this --- crates/nu-command/src/lib.rs | 2 ++ crates/nu-command/src/math/abs.rs | 60 +++++++++++++++++++++++++++++++ crates/nu-command/src/math/mod.rs | 3 ++ 3 files changed, 65 insertions(+) create mode 100644 crates/nu-command/src/math/abs.rs create mode 100644 crates/nu-command/src/math/mod.rs diff --git a/crates/nu-command/src/lib.rs b/crates/nu-command/src/lib.rs index 4e0f0a7f4..34ca20542 100644 --- a/crates/nu-command/src/lib.rs +++ b/crates/nu-command/src/lib.rs @@ -8,6 +8,7 @@ mod filesystem; mod filters; mod formats; mod strings; +mod math; mod system; mod viewers; @@ -21,5 +22,6 @@ pub use filesystem::*; pub use filters::*; pub use formats::*; pub use strings::*; +pub use math::*; pub use system::*; pub use viewers::*; diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs new file mode 100644 index 000000000..2bf717a67 --- /dev/null +++ b/crates/nu-command/src/math/abs.rs @@ -0,0 +1,60 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{Example, ShellError, Signature, Span, Type, Value}; + +pub struct SubCommand; + +impl Command for SubCommand { + fn name(&self) -> &str { + "math abs" + } + + fn signature(&self) -> Signature { + Signature::build("math abs") + } + + fn usage(&self) -> &str { + "Returns absolute values of a list of numbers" + } + + fn run(&self, context: &EvaluationContext, call: &Call, input: Value) -> Result { + let mapped = input.map(move |val| match val.value { + Value::Int { val, span } => Value::int(val.abs(), span), + Value::Float { val, span } => Value::Float{val: val.abs(), span: span}, + Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span}, + other => abs_default(other)?, + }); + Ok(mapped) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Get absolute of each value in a list of numbers", + example: "echo [-50 25] | math abs", + result: Some(vec![ + Value::test_int(50), + Value::test_int(25), + ]), + }] + } +} + +fn abs_default(_: Value) -> Result { + Value::Error(ShellError::unexpected( + "Only numerical values are supported", + )) + .into() +} + +#[cfg(test)] +mod tests { + use super::ShellError; + use super::SubCommand; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + test_examples(SubCommand {}) + } +} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs new file mode 100644 index 000000000..ea889f5db --- /dev/null +++ b/crates/nu-command/src/math/mod.rs @@ -0,0 +1,3 @@ +pub mod abs; + +pub use abs::SubCommand as MathAbs; From 51bea2e884b0a9367d8d0690f2585a78bfc9b748 Mon Sep 17 00:00:00 2001 From: Luccas Mateus Date: Thu, 21 Oct 2021 12:29:57 -0300 Subject: [PATCH 2/7] still not working --- crates/nu-command/src/math/abs.rs | 31 +++++++++++++++++-------------- crates/nu-command/src/math/mod.rs | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index 2bf717a67..535953054 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -17,33 +17,36 @@ impl Command for SubCommand { "Returns absolute values of a list of numbers" } - fn run(&self, context: &EvaluationContext, call: &Call, input: Value) -> Result { - let mapped = input.map(move |val| match val.value { + fn run(&self, _context: &EvaluationContext, call: &Call, input: Value) -> Result { + let head = call.head; + input.map(head, move |val| match val { Value::Int { val, span } => Value::int(val.abs(), span), Value::Float { val, span } => Value::Float{val: val.abs(), span: span}, Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span}, - other => abs_default(other)?, - }); - Ok(mapped) + other => abs_default(other, head), + }) } fn examples(&self) -> Vec { vec![Example { description: "Get absolute of each value in a list of numbers", example: "echo [-50 25] | math abs", - result: Some(vec![ - Value::test_int(50), - Value::test_int(25), - ]), + result: Some(Value:: List { + vals: vec![ + Value::test_int(50), + Value::test_int(25), + ], + span: Span::unknown(), + }), }] } } -fn abs_default(_: Value) -> Result { - Value::Error(ShellError::unexpected( - "Only numerical values are supported", - )) - .into() +fn abs_default(_: Value, head: Span) -> Value { + Value::Error {error: ShellError::UnsupportedInput( + String::from("Only numerical values are supported"), + head + )} } #[cfg(test)] diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index ea889f5db..88e1bccde 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -1,3 +1,3 @@ -pub mod abs; +mod abs; pub use abs::SubCommand as MathAbs; From aa5ab8a666c8a83334c47118e38194c4162be5c2 Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Sun, 24 Oct 2021 20:58:18 -0300 Subject: [PATCH 3/7] final math abs --- crates/nu-command/src/default_context.rs | 2 + crates/nu-command/src/lib.rs | 4 +- crates/nu-command/src/math/abs.rs | 68 +++++++++++++++++------- crates/nu-command/src/math/command.rs | 32 +++++++++++ crates/nu-command/src/math/mod.rs | 2 + 5 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 crates/nu-command/src/math/command.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 1fe5d90ec..c871f0e4b 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -51,6 +51,8 @@ pub fn create_default_context() -> Rc> { LetEnv, Lines, Ls, + Math, + MathAbs, Mkdir, Module, Mv, diff --git a/crates/nu-command/src/lib.rs b/crates/nu-command/src/lib.rs index 34ca20542..151c6076c 100644 --- a/crates/nu-command/src/lib.rs +++ b/crates/nu-command/src/lib.rs @@ -7,8 +7,8 @@ mod experimental; mod filesystem; mod filters; mod formats; -mod strings; mod math; +mod strings; mod system; mod viewers; @@ -21,7 +21,7 @@ pub use experimental::*; pub use filesystem::*; pub use filters::*; pub use formats::*; -pub use strings::*; pub use math::*; +pub use strings::*; pub use system::*; pub use viewers::*; diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index 535953054..817e43041 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -1,6 +1,6 @@ use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{Example, ShellError, Signature, Span, Type, Value}; +use nu_protocol::{Example, ShellError, Signature, Span, Value}; pub struct SubCommand; @@ -17,23 +17,39 @@ impl Command for SubCommand { "Returns absolute values of a list of numbers" } - fn run(&self, _context: &EvaluationContext, call: &Call, input: Value) -> Result { + fn run( + &self, + _context: &EvaluationContext, + call: &Call, + input: Value, + ) -> Result { let head = call.head; - input.map(head, move |val| match val { - Value::Int { val, span } => Value::int(val.abs(), span), - Value::Float { val, span } => Value::Float{val: val.abs(), span: span}, - Value::Duration { val, span } => Value::Duration{val: val.abs(), span: span}, - other => abs_default(other, head), - }) + match input { + Value::List { vals, span } => Ok(Value::List { + vals: vals + .into_iter() + .map(move |val| abs_helper(val, head)) + .collect(), + span, + }), + other => match abs_helper(other, head) { + Value::Error { error } => Err(error), + ok => Ok(ok), + }, + } } fn examples(&self) -> Vec { vec![Example { description: "Get absolute of each value in a list of numbers", - example: "echo [-50 25] | math abs", - result: Some(Value:: List { + example: "echo [-50 -100.0 25] | math abs", + result: Some(Value::List { vals: vec![ Value::test_int(50), + Value::Float { + val: 100.0, + span: Span::unknown(), + }, Value::test_int(25), ], span: Span::unknown(), @@ -42,21 +58,33 @@ impl Command for SubCommand { } } -fn abs_default(_: Value, head: Span) -> Value { - Value::Error {error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), - head - )} +fn abs_helper(val: Value, head: Span) -> Value { + match val { + Value::Int { val, span } => Value::int(val.abs(), span), + Value::Float { val, span } => Value::Float { + val: val.abs(), + span, + }, + Value::Duration { val, span } => Value::Duration { + val: val.abs(), + span, + }, + _ => Value::Error { + error: ShellError::UnsupportedInput( + String::from("Only numerical values are supported"), + head, + ), + }, + } } #[cfg(test)] -mod tests { - use super::ShellError; - use super::SubCommand; +mod test { + use super::*; #[test] - fn examples_work_as_expected() -> Result<(), ShellError> { - use crate::examples::test as test_examples; + fn test_examples() { + use crate::test_examples; test_examples(SubCommand {}) } diff --git a/crates/nu-command/src/math/command.rs b/crates/nu-command/src/math/command.rs new file mode 100644 index 000000000..8eab51b5b --- /dev/null +++ b/crates/nu-command/src/math/command.rs @@ -0,0 +1,32 @@ +use nu_engine::get_full_help; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{ShellError, Signature, Value}; + +pub struct MathCommand; + +impl Command for MathCommand { + fn name(&self) -> &str { + "math" + } + + fn signature(&self) -> Signature { + Signature::build("math") + } + + fn usage(&self) -> &str { + "Use mathematical functions as aggregate functions on a list of numbers or tables." + } + + fn run( + &self, + context: &EvaluationContext, + call: &Call, + _input: Value, + ) -> Result { + Ok(Value::String { + val: get_full_help(&MathCommand.signature(), &MathCommand.examples(), context), + span: call.head, + }) + } +} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 88e1bccde..5abb051b2 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -1,3 +1,5 @@ mod abs; +pub mod command; pub use abs::SubCommand as MathAbs; +pub use command::MathCommand as Math; From 3f313da4c3a84733472ccb154bb67baf979e7b08 Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Mon, 25 Oct 2021 08:10:17 -0300 Subject: [PATCH 4/7] Fix test --- crates/nu-command/src/math/abs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index 817e43041..e4d201421 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -42,7 +42,7 @@ impl Command for SubCommand { fn examples(&self) -> Vec { vec![Example { description: "Get absolute of each value in a list of numbers", - example: "echo [-50 -100.0 25] | math abs", + example: "[-50 -100.0 25] | math abs", result: Some(Value::List { vals: vec![ Value::test_int(50), From 017b1d899620a9fd9f84af2c47248cc83107d6cd Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Mon, 25 Oct 2021 20:56:22 -0300 Subject: [PATCH 5/7] Updated to new PipeLineData and made the tests run --- crates/nu-command/src/example_test.rs | 3 +- crates/nu-command/src/math/abs.rs | 44 ++++++++++++++++----------- crates/nu-command/src/math/command.rs | 25 ++++++++++----- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index c7ca4f001..53e98e4a0 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -5,7 +5,7 @@ use nu_protocol::{ PipelineData, }; -use super::{From, Into, Split}; +use super::{From, Into, Split, Math}; pub fn test_examples(cmd: impl Command + 'static) { let examples = cmd.examples(); @@ -18,6 +18,7 @@ pub fn test_examples(cmd: impl Command + 'static) { working_set.add_decl(Box::new(From)); working_set.add_decl(Box::new(Into)); working_set.add_decl(Box::new(Split)); + working_set.add_decl(Box::new(Math)); // Adding the command that is being tested to the working set working_set.add_decl(Box::new(cmd)); diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index e4d201421..e7f43205d 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -1,7 +1,8 @@ use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{Example, ShellError, Signature, Span, Value}; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value}; +#[derive(Clone)] pub struct SubCommand; impl Command for SubCommand { @@ -19,24 +20,31 @@ impl Command for SubCommand { fn run( &self, - _context: &EvaluationContext, + _engine_state: &EngineState, + _stack: &mut Stack, call: &Call, - input: Value, - ) -> Result { + input: PipelineData, + ) -> Result { let head = call.head; - match input { - Value::List { vals, span } => Ok(Value::List { - vals: vals - .into_iter() - .map(move |val| abs_helper(val, head)) - .collect(), - span, - }), - other => match abs_helper(other, head) { - Value::Error { error } => Err(error), - ok => Ok(ok), - }, - } + input.map(move |value| abs_helper(value, head)) + // PipelineData::Value(Value::List { vals, span }) => Ok(Value::List { + // vals: vals + // .into_iter() + // .map(move |val| abs_helper(val, head)) + // .collect(), + // span, + // }), + // PipelineData::Value(other) => match abs_helper(other, head) { + // Value::Error { error } => Err(error), + // ok => Ok(nu_protocolok), + // }, + // _ => Value::Error { + // error: ShellError::UnsupportedInput( + // String::from("Only numerical values are supported"), + // head, + // ), + // }, + // } } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/math/command.rs b/crates/nu-command/src/math/command.rs index 8eab51b5b..6efc11f87 100644 --- a/crates/nu-command/src/math/command.rs +++ b/crates/nu-command/src/math/command.rs @@ -1,8 +1,11 @@ use nu_engine::get_full_help; -use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{ShellError, Signature, Value}; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + IntoPipelineData, PipelineData, Signature, Value, +}; +#[derive(Clone)] pub struct MathCommand; impl Command for MathCommand { @@ -20,13 +23,19 @@ impl Command for MathCommand { fn run( &self, - context: &EvaluationContext, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, - _input: Value, - ) -> Result { + _input: PipelineData, + ) -> Result { Ok(Value::String { - val: get_full_help(&MathCommand.signature(), &MathCommand.examples(), context), + val: get_full_help( + &MathCommand.signature(), + &MathCommand.examples(), + engine_state, + ), span: call.head, - }) + } + .into_pipeline_data()) } } From 2ce034d0f0a64290bf1178c191026b1f6373482c Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Mon, 25 Oct 2021 20:57:45 -0300 Subject: [PATCH 6/7] linting --- crates/nu-command/src/example_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index 53e98e4a0..3685af399 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -5,7 +5,7 @@ use nu_protocol::{ PipelineData, }; -use super::{From, Into, Split, Math}; +use super::{From, Into, Math, Split}; pub fn test_examples(cmd: impl Command + 'static) { let examples = cmd.examples(); From 11d8e6c71ff957ed6b7e30cf4821059e926e3f32 Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Mon, 25 Oct 2021 21:11:20 -0300 Subject: [PATCH 7/7] Just removed a few comments --- crates/nu-command/src/math/abs.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index e7f43205d..95f46ef7a 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -27,24 +27,6 @@ impl Command for SubCommand { ) -> Result { let head = call.head; input.map(move |value| abs_helper(value, head)) - // PipelineData::Value(Value::List { vals, span }) => Ok(Value::List { - // vals: vals - // .into_iter() - // .map(move |val| abs_helper(val, head)) - // .collect(), - // span, - // }), - // PipelineData::Value(other) => match abs_helper(other, head) { - // Value::Error { error } => Err(error), - // ok => Ok(nu_protocolok), - // }, - // _ => Value::Error { - // error: ShellError::UnsupportedInput( - // String::from("Only numerical values are supported"), - // head, - // ), - // }, - // } } fn examples(&self) -> Vec {