From d768a257a32914d8273f5f90b6c64c72f3774912 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 6 Feb 2024 23:29:43 -0700 Subject: [PATCH] rpn(1): made EvaluationError struct contain exit code --- src/rpn.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 1068b06..6cbb55b 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -112,19 +112,15 @@ impl Display for CalcType { } #[derive(Debug, Clone)] -struct EvaluationError { pub message: String } - -impl Display for EvaluationError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}", self.message) - } +struct EvaluationError { + message: String, + code: i32, } // I’m no math nerd but I want the highest possible approximation of 0.9 // repeating and it seems this can give it to me const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0; - fn eval( input: &str, initial_stack: VecDeque, @@ -150,7 +146,8 @@ fn eval( Val(v) => stack.push_back(v), Invalid(i) => { return Err(EvaluationError { - message: format!("{}: Invalid token", i) + message: format!("{}: Invalid token", i), + code: EX_DATAERR, }) }, op => { @@ -175,7 +172,8 @@ fn eval( }; } else { return Err(EvaluationError { - message: format!("{}: Unexpected operation.", op) + message: format!("{}: Unexpected operation.", op), + code: EX_DATAERR, }) } }, @@ -217,7 +215,7 @@ fn main() -> ExitCode { }, Err(err) => { eprintln!("{}: {}", argv[0], err.message); - return ExitCode::from(EX_DATAERR as u8); + return ExitCode::from(err.code as u8); }, }; } @@ -242,7 +240,7 @@ fn main() -> ExitCode { }, Err(err) => { eprintln!("{}: {}", argv[0], err.message); - return ExitCode::from(EX_DATAERR as u8); + return ExitCode::from(err.code as u8); }, }; }