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] 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 1fe5d90ec3..c871f0e4b8 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 34ca205423..151c6076cf 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 5359530543..817e430413 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 0000000000..8eab51b5b5 --- /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 88e1bccde6..5abb051b23 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;