mirror of
https://github.com/nushell/nushell.git
synced 2025-04-20 11:18:20 +02:00
* Output error when ls into a file without permission * math sqrt * added test to check fails when ls into prohibited dir * fix lint * math sqrt with tests and doc * trigger wasm build * Update filesystem_shell.rs * converted math commands to outputstream and new method for arg evaluation * fmt * clippy Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use crate::commands::math::reducers::{reducer_for, Reduce};
|
|
use crate::commands::math::utils::run_with_function;
|
|
use crate::prelude::*;
|
|
use nu_engine::WholeStreamCommand;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{Signature, UntaggedValue, Value};
|
|
|
|
pub struct SubCommand;
|
|
|
|
impl WholeStreamCommand for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"math max"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("math max")
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Finds the maximum within a list of numbers or tables"
|
|
}
|
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
run_with_function(args, maximum)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Find the maximum of list of numbers",
|
|
example: "echo [-50 100 25] | math max",
|
|
result: Some(vec![UntaggedValue::int(100).into()]),
|
|
}]
|
|
}
|
|
}
|
|
|
|
pub fn maximum(values: &[Value], _name: &Tag) -> Result<Value, ShellError> {
|
|
let max_func = reducer_for(Reduce::Maximum);
|
|
max_func(Value::nothing(), values.to_vec())
|
|
}
|
|
|
|
#[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 {})
|
|
}
|
|
}
|