From 228a0111c79fc8ba2a5a29845dd04a3ff988a5f5 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 2 Feb 2024 21:05:34 -0700 Subject: [PATCH] rpn(1): fixed Invalid error handling --- src/rpn.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 697a4a9..1068b06 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -56,7 +56,7 @@ extern crate sysexits; use sysexits::EX_DATAERR; -#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)] +#[derive(Clone, PartialEq, PartialOrd, Debug)] // enum CalcType is a type containing operations used in the calculator enum CalcType { Add, @@ -67,7 +67,7 @@ enum CalcType { Floor, Modulo, Val(f64), - Invalid, + Invalid(String), } impl From<&str> for CalcType { @@ -83,7 +83,7 @@ impl From<&str> for CalcType { _ => { match value.parse::() { Ok(x) => Val(x), - Err(_) => Invalid, + Err(_) => Invalid(value.to_owned()), } }, } @@ -104,7 +104,9 @@ impl Display for CalcType { Val(x) => { y = x.to_string(); &y }, - Invalid => "", + Invalid(i) => { + y = i.to_string(); &y + }, }) } } @@ -146,13 +148,13 @@ fn eval( while let Some(n) = toks.pop_back() { match n { Val(v) => stack.push_back(v), - Invalid => { + Invalid(i) => { return Err(EvaluationError { - message: format!("{}: Invalid token", n) + message: format!("{}: Invalid token", i) }) }, op => { - ops.push_back(op); + ops.push_back(op.clone()); oper = true; let vals = (