mod maths;
use maths::*;
use std::collections::HashMap;
pub fn math_parse_int(expression: &str, variable_map: Option<&HashMap<String, String>>) -> Result<i64, MathParseErrors> {
match math_compute(expression, variable_map)? {
Number::Int(i) => Ok(i),
Number::Float(f) => Err(ReturnFloatExpectedInt(f)),
}
}
pub fn math_parse_float(expression: &str, variable_map: Option<&HashMap<String, String>>) -> Result<f64, MathParseErrors> {
match math_compute(expression, variable_map)? {
Number::Float(f) => Ok(f),
Number::Int(i) => Ok(i as f64),
}
}
#[derive(Debug, PartialEq)]
pub enum MathParseErrors {
UnclosedParenthesis,
UnopenedParenthesis,
EmptyLine,
InvalidNumber(String),
MisplacedOperator(char),
TrailingOperator,
IntConversion(f64),
BinaryOpOnFloat(f64, char),
ReturnFloatExpectedInt(f64),
BadOperatorHint(char, &'static str),
UnexpectedZero,
UnexpectedNegative,
MathParseInternalBug(String),
}
use MathParseErrors::*;
use std::fmt;
impl fmt::Display for MathParseErrors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UnclosedParenthesis => write!(f, "A parenthesis was opened but never closed."),
UnopenedParenthesis => write!(f, "A closing parenthesis was used with no matching open parenthesis."),
EmptyLine => write!(f, "The math expression is empty. Or the right hand side of an operator is empty."),
InvalidNumber(s) => write!(f, "The expression `{s}` that should have been a number but can't be read."),
MisplacedOperator(c) => write!(f, "The operator `{c}`is not where it should be. Or the left hand side of an operator being empty."),
TrailingOperator => write!(f, "An operator is the last element of a line of math."),
IntConversion(fp) => write!(f, "The floating point number {fp} could not be converted to an int which is needed."),
BinaryOpOnFloat(fp, c) => write!(f, "The bitwise operation `{c}` is being performed on the floating point number `{fp}`."),
ReturnFloatExpectedInt(fp) => write!(f, "An integer was wanted but the floating point number `{fp}` was returned instead."),
BadOperatorHint(c, s) => write!(f, "The operator '{c}' is invalid. Did you meant '{s}'?"),
UnexpectedZero => write!(f, "There is a 0 in an operation where it is invalid such as a division or a remainder."),
UnexpectedNegative => write!(f, "There is a negative number in an operation where it is invalid such as a logical shift."),
MathParseInternalBug(s) => write!(f, "There is a bug in the math-parse library. The error message is the following:\n{s}\nPlease, report it with the input given to the library to the developer of math-parse over here: https://github.com/Arkaeriit/math-parse"),
}
}
}