1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;

pub struct SubCommand;

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",
        )
    }

    fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
        eval(args)
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Evalulate math in the pipeline",
            example: "echo '10 / 4' | math eval",
            result: Some(vec![UntaggedValue::decimal_from_float(
                2.5,
                Span::unknown(),
            )
            .into()]),
        }]
    }
}

pub fn eval(args: CommandArgs) -> Result<OutputStream, ShellError> {
    let expression: Option<Tagged<String>> = args.opt(0)?;
    let name = args.call_info.name_tag.clone();
    let input = args.input;

    if let Some(string) = expression {
        match parse(&string, &string.tag) {
            Ok(value) => Ok(OutputStream::one(value)),
            Err(err) => Err(ShellError::labeled_error(
                "Math evaluation error",
                err,
                &string.tag.span,
            )),
        }
    } else {
        let mapped: Result<Vec<_>, _> = input
            .map(move |x| {
                if let Some(Tagged {
                    tag,
                    item: expression,
                }) = &expression
                {
                    UntaggedValue::string(expression).into_value(tag)
                } else {
                    x
                }
            })
            .map(move |input| {
                if let Ok(string) = input.as_string() {
                    match parse(&string, &input.tag) {
                        Ok(value) => Ok(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.clone(),
                    ))
                }
            })
            .collect();
        match mapped {
            Ok(values) => Ok(OutputStream::from(values)),
            Err(e) => Err(e),
        }
    }
}

pub fn parse<T: Into<Tag>>(math_expression: &str, tag: T) -> Result<Value, String> {
    let mut ctx = meval::Context::new();
    ctx.var("tau", std::f64::consts::TAU);
    match meval::eval_str_with_context(math_expression, &ctx) {
        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 {
    use super::ShellError;
    use super::SubCommand;

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(SubCommand {})
    }
}