2020-07-18 06:11:19 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
|
|
use nu_source::Tagged;
|
|
|
|
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct SubCommandArgs {
|
|
|
|
expression: Option<Tagged<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl WholeStreamCommand for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math eval"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Evaluate a math expression into a number"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("math eval").desc(self.usage()).optional(
|
|
|
|
"math expression",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the math expression to evaluate",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
eval(args).await
|
2020-07-18 06:11:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Evalulate math in the pipeline",
|
|
|
|
example: "echo '10 / 4' | math eval",
|
2020-09-21 19:28:31 +02:00
|
|
|
result: Some(vec![UntaggedValue::decimal_from_float(
|
|
|
|
2.5,
|
|
|
|
Span::unknown(),
|
|
|
|
)
|
|
|
|
.into()]),
|
2020-07-18 06:11:19 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
pub async fn eval(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-07-18 06:11:19 +02:00
|
|
|
let name = args.call_info.name_tag.span;
|
2020-12-18 08:53:49 +01:00
|
|
|
let (SubCommandArgs { expression }, input) = args.process().await?;
|
2020-07-18 06:11:19 +02:00
|
|
|
|
2021-01-03 02:22:44 +01:00
|
|
|
if let Some(string) = expression {
|
|
|
|
match parse(&string, &string.tag) {
|
|
|
|
Ok(value) => Ok(OutputStream::one(ReturnSuccess::value(value))),
|
|
|
|
Err(err) => Err(ShellError::labeled_error(
|
|
|
|
"Math evaluation error",
|
|
|
|
err,
|
|
|
|
&string.tag.span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(input
|
|
|
|
.map(move |x| {
|
|
|
|
if let Some(Tagged {
|
|
|
|
tag,
|
|
|
|
item: expression,
|
|
|
|
}) = &expression
|
|
|
|
{
|
|
|
|
UntaggedValue::string(expression).into_value(tag)
|
|
|
|
} else {
|
|
|
|
x
|
2020-07-18 06:11:19 +02:00
|
|
|
}
|
2021-01-03 02:22:44 +01:00
|
|
|
})
|
|
|
|
.map(move |input| {
|
|
|
|
if let Ok(string) = input.as_string() {
|
|
|
|
match parse(&string, &input.tag) {
|
|
|
|
Ok(value) => ReturnSuccess::value(value),
|
|
|
|
Err(err) => Err(ShellError::labeled_error(
|
|
|
|
"Math evaluation error",
|
|
|
|
err,
|
|
|
|
&input.tag.span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
|
|
|
name,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.to_output_stream())
|
|
|
|
}
|
2020-07-18 06:11:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse<T: Into<Tag>>(math_expression: &str, tag: T) -> Result<Value, String> {
|
2020-10-22 02:16:51 +02:00
|
|
|
let mut ctx = meval::Context::new();
|
|
|
|
ctx.var("tau", std::f64::consts::TAU);
|
|
|
|
match meval::eval_str_with_context(math_expression, &ctx) {
|
2020-07-18 06:11:19 +02:00
|
|
|
Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()),
|
|
|
|
Ok(num) => Ok(UntaggedValue::from(Primitive::from(num)).into_value(tag)),
|
|
|
|
Err(error) => Err(error.to_string().to_lowercase()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-07-18 06:11:19 +02:00
|
|
|
use super::SubCommand;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-07-18 06:11:19 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(SubCommand {})?)
|
2020-07-18 06:11:19 +02:00
|
|
|
}
|
|
|
|
}
|