Struct MathParse

Source
pub struct MathParse { /* private fields */ }
Expand description

Object generated when parsing a string of math. Can be later used for solving or formatting to other representations.

Implementations§

Source§

impl MathParse

Source

pub fn parse(expression: &str) -> Result<Self, MathParseErrors>

Parse a math expression in infix notation.

math_parse::MathParse::parse("3 + 4").unwrap();
Source

pub fn parse_rpn(expression: &str) -> Result<Self, MathParseErrors>

Parse a math expression in postfix notation (RPN).

math_parse::MathParse::parse_rpn("3 4 +").unwrap();
Source§

impl MathParse

Source

pub fn solve_auto( &self, map: Option<&HashMap<String, String>>, ) -> Result<Result<i64, f64>, MathParseErrors>

Does all the computation from a string with a line of math to the final resulting number. If the result can be an int, return it as Ok(Ok(int)). If it can only be a float, return it as Ok(Err(floar)). If it can’t be solved, return Err(error).

use math_parse::MathParse;
use math_parse::MathParseErrors::*;

assert_eq!(
    MathParse::parse("3 + 8.0").unwrap().solve_auto(None),
    Ok(Ok(11)));
assert_eq!(
    MathParse::parse("3 - 8.5").unwrap().solve_auto(None),
    Ok(Err(-5.5)));
assert_eq!(
    MathParse::parse("34 + bcd").unwrap().solve_auto(None),
    Err(InvalidNumber("bcd".to_string())));

A optional map of variable name can be taken as argument.

Source

pub fn solve_int( &self, variable_map: Option<&HashMap<String, String>>, ) -> Result<i64, MathParseErrors>

Does all the computation from a string with a line of math to the final resulting number. If the result can be an int, return it as Ok(int). If it can’t be solved as an int, return Err(error).

use math_parse::MathParse;
use math_parse::MathParseErrors::*;

assert_eq!(
    MathParse::parse("3 + 8.0").unwrap().solve_int(None),
    Ok(11));
assert_eq!(
    MathParse::parse("3 - 8.5").unwrap().solve_int(None),
    Err(ReturnFloatExpectedInt(-5.5)));

A optional map of variable name can be taken as argument:

use math_parse::MathParse;

let variables = std::collections::HashMap::from([
    ("a".to_string(), "1".to_string()),
    ("b".to_string(), "3*3".to_string()),
]);
let result = MathParse::parse("a+b").unwrap().solve_int(Some(&variables)).unwrap();
assert_eq!(result, 10);
Source

pub fn solve_float( &self, variable_map: Option<&HashMap<String, String>>, ) -> Result<f64, MathParseErrors>

Does all the computation from a string with a line of math to the final resulting number. The result is returned as a Ok(f64). If it can’t be solved, return Err(error).

use math_parse::MathParse;
use math_parse::MathParseErrors::*;

assert_eq!(
    MathParse::parse("3 + 8").unwrap().solve_float(None),
    Ok(11.0));
assert_eq!(
    MathParse::parse("3 - 8.5").unwrap().solve_float(None),
    Ok(-5.5));

A optional map of variable name can be taken as argument.

Source§

impl MathParse

Source

pub fn to_rpn(&self) -> Result<Vec<RPN>, MathParseErrors>

Parse a math expression into list of instructions in Reverse Polish notation (postfix notation).

Example:

use math_parse::RPN::*;
use math_parse::UnaryOp::*;
use math_parse::BinaryOp::*;
use math_parse::MathParse;

assert_eq!(
    MathParse::parse("3-4+(-5)").unwrap().to_rpn(),
    Ok(vec![Name("3".to_string()), Name("4".to_string()), Binary(Subtraction), Name("5".to_string()), Unary(Minus), Binary(Addition)]));
Source§

impl MathParse

Source

pub fn to_tree(&self) -> Result<Tree, MathParseErrors>

Parse a math expression into list of instructions as a Tree (infix notation).

Example:

use math_parse::Tree::*;
use math_parse::UnaryOp::*;
use math_parse::BinaryOp::*;
use math_parse::MathParse;

assert_eq!(
    MathParse::parse("3*4+(-5)").unwrap().to_tree(),
    Ok(Binary(Addition,
        Box::new(Binary(Multiplication,
            Box::new(Name("3".to_string())),
            Box::new(Name("4".to_string())))),
        Box::new(Unary(Minus,
            Box::new(Name("5".to_string())))))));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.