nushell/crates/nu-command/src/math/abs.rs

64 lines
1.8 KiB
Rust
Raw Normal View History

2021-10-21 16:52:26 +02:00
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"
}
2021-10-21 17:29:57 +02:00
fn run(&self, _context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
let head = call.head;
input.map(head, move |val| match val {
2021-10-21 16:52:26 +02:00
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},
2021-10-21 17:29:57 +02:00
other => abs_default(other, head),
})
2021-10-21 16:52:26 +02:00
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get absolute of each value in a list of numbers",
example: "echo [-50 25] | math abs",
2021-10-21 17:29:57 +02:00
result: Some(Value:: List {
vals: vec![
Value::test_int(50),
Value::test_int(25),
],
span: Span::unknown(),
}),
2021-10-21 16:52:26 +02:00
}]
}
}
2021-10-21 17:29:57 +02:00
fn abs_default(_: Value, head: Span) -> Value {
Value::Error {error: ShellError::UnsupportedInput(
String::from("Only numerical values are supported"),
head
)}
2021-10-21 16:52:26 +02:00
}
#[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 {})
}
}