Added ToString trait to CalcError enum

This commit is contained in:
PaddiM8 2020-12-30 22:54:32 +01:00
parent 72cea91394
commit 5daaa129f2

View File

@ -93,25 +93,25 @@ pub enum CalcError {
impl ToString for CalcError { impl ToString for CalcError {
fn to_string(&self) -> String { fn to_string(&self) -> String {
match err { match self {
IncorrectAmountOfArguments(expected, func, got) => format!( CalcError::IncorrectAmountOfArguments(expected, func, got) => format!(
"Expected {} arguments for function {}, but got {}.", "Expected {} arguments for function {}, but got {}.",
expected, func, got expected, func, got
), ),
InvalidNumberLiteral(x) => format!("Invalid number literal: '{}'.", x), CalcError::InvalidNumberLiteral(x) => format!("Invalid number literal: '{}'.", x),
InvalidOperator => format!("Invalid operator."), CalcError::InvalidOperator => format!("Invalid operator."),
InvalidUnit => format!("Invalid unit."), CalcError::InvalidUnit => format!("Invalid unit."),
TimedOut => format!("Operation took too long."), CalcError::TimedOut => format!("Operation took too long."),
VariableReferencesItself => format!("Variable references itself."), CalcError::VariableReferencesItself => format!("Variable references itself."),
UnexpectedToken(got, expected) => { CalcError::UnexpectedToken(got, expected) => {
format!("Unexpected token: '{:?}', expected '{:?}'.", got, expected) format!("Unexpected token: '{:?}', expected '{:?}'.", got, expected)
} }
UnableToInvert(msg) => format!("Unable to invert: {}", msg), CalcError::UnableToInvert(msg) => format!("Unable to invert: {}", msg),
UndefinedFn(name) => format!("Undefined function: '{}'.", name), CalcError::UndefinedFn(name) => format!("Undefined function: '{}'.", name),
UndefinedVar(name) => format!("Undefined variable: '{}'.", name), CalcError::UndefinedVar(name) => format!("Undefined variable: '{}'.", name),
UnableToParseExpression => format!("Unable to parse expression."), CalcError::UnableToParseExpression => format!("Unable to parse expression."),
UnableToSolveEquation => format!("Unable to solve equation."), CalcError::UnableToSolveEquation => format!("Unable to solve equation."),
Unknown => format!("Unknown error."), CalcError::Unknown => format!("Unknown error."),
} }
} }
} }
@ -146,7 +146,7 @@ pub fn simple_eval(input: &str) -> Result<JsValue, JsValue> {
match result { match result {
Ok(Some(value)) => Ok(value.to_f64().into()), Ok(Some(value)) => Ok(value.to_f64().into()),
Ok(None) => Ok(JsValue::NULL), Ok(None) => Ok(JsValue::NULL),
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string().into()),
} }
} }