rpn(1): fixed Invalid error handling

This commit is contained in:
Emma Tebibyte 2024-02-02 21:05:34 -07:00
parent a30152d783
commit 228a0111c7
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -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::<f64>() {
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 = (