From cdb2011efa6580865a1a658a00b5448cea93f069 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Tue, 27 Apr 2021 00:08:10 -0400 Subject: [PATCH] Bubble more errors up for cleaner code --- src/calc/errors.rs | 25 ++++++++++++++----------- src/calc/operations.rs | 6 ++++-- src/main.rs | 30 +++++++++++++++--------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/calc/errors.rs b/src/calc/errors.rs index 64aefe1..37052ad 100644 --- a/src/calc/errors.rs +++ b/src/calc/errors.rs @@ -1,3 +1,5 @@ +use std::fmt; + pub enum CalculatorError { ArithmeticError, NotEnoughStackEntries, @@ -7,22 +9,23 @@ pub enum CalculatorError { NoSuchRegister, NoSuchMacro, ParseError, + NoSuchOperator, } -impl CalculatorError { - //TODO: Use &str instead of Strings - pub fn message(&self) -> String { +impl fmt::Display for CalculatorError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - CalculatorError::ArithmeticError => String::from("Arithmetic Error"), - CalculatorError::NotEnoughStackEntries => String::from("Not enough items in the stack"), + CalculatorError::ArithmeticError => write!(f, "Arithmetic Error"), + CalculatorError::NotEnoughStackEntries => write!(f, "Not enough items in the stack"), CalculatorError::CorruptStateChange(msg) => { - String::from("Corrupt state change: ") + msg + write!(f, "Corrupt state change: {}", msg) } - CalculatorError::EmptyHistory(msg) => String::from("No history to ") + msg, - CalculatorError::NoSuchConstant => String::from("No such constant"), - CalculatorError::NoSuchRegister => String::from("No such register"), - CalculatorError::NoSuchMacro => String::from("No such macro"), - CalculatorError::ParseError => String::from("Parse error"), + CalculatorError::EmptyHistory(msg) => write!(f, "No history to {}", msg), + CalculatorError::NoSuchOperator => write!(f, "No such operator"), + CalculatorError::NoSuchConstant => write!(f, "No such constant"), + CalculatorError::NoSuchRegister => write!(f, "No such register"), + CalculatorError::NoSuchMacro => write!(f, "No such macro"), + CalculatorError::ParseError => write!(f, "Parse error"), } } } diff --git a/src/calc/operations.rs b/src/calc/operations.rs index 4bfcca7..c64c328 100644 --- a/src/calc/operations.rs +++ b/src/calc/operations.rs @@ -1,3 +1,5 @@ +use super::errors::CalculatorError; + pub enum CalculatorOperation { Add, Subtract, @@ -29,7 +31,7 @@ pub enum CalculatorOperation { } impl CalculatorOperation { - pub fn from_char(key: char) -> Result { + pub fn from_char(key: char) -> Result { match key { '+' => Ok(CalculatorOperation::Add), '-' => Ok(CalculatorOperation::Subtract), @@ -59,7 +61,7 @@ impl CalculatorOperation { 'l' => Ok(CalculatorOperation::Log), 'L' => Ok(CalculatorOperation::Ln), 'e' => Ok(CalculatorOperation::E), - _ => Err(()), + _ => Err(CalculatorError::NoSuchOperator), } } diff --git a/src/main.rs b/src/main.rs index 4e46d9e..74cfe94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -228,7 +228,7 @@ fn main() -> Result<(), Box> { // Exit the program Ok(true) => break 'outer Ok(()), Ok(false) => None, - Err(e) => Some(e.message()), + Err(e) => Some(format!("{}", e)), }; } @@ -239,7 +239,7 @@ fn main() -> Result<(), Box> { // Exit the program Ok(true) => break 'outer Ok(()), Ok(false) => None, - Err(e) => Some(e.message()), + Err(e) => Some(format!("{}", e)), }; } Event::MacroEnd => app.current_macro = None, @@ -290,7 +290,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result { if app.input.is_empty() { - calc_operation(app, '\n'); + calc_operation(app, '\n')?; } else { let mut tmp_input = app.input.clone(); if tmp_input.ends_with('e') { @@ -305,7 +305,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result { - calc_operation(app, '>'); + calc_operation(app, '>')?; } Key::Down => { if let Ok(x) = app.calculator.pop() { @@ -319,7 +319,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result { - calc_operation(app, c); + calc_operation(app, c)?; } _ => {} }, @@ -365,6 +365,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result { if !app.input.is_empty() { //TODO: A better way to do this + //Can use calc_operation in the future. Already performs this check if let Ok(f) = app.input.parse::() { match app.calculator.push(f) { Ok(()) => app.input.clear(), @@ -386,19 +387,18 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result Result<(), CalculatorError> { + let op = CalculatorOperation::from_char(c)?; + if !app.input.is_empty() { if let Ok(f) = app.input.parse::() { - if app.calculator.push(f).is_ok() { - app.input.clear(); - } - } - - app.error_msg = match app.calculator.op(op) { - Err(e) => Some(e.message()), - Ok(()) => None, + app.calculator.push(f)?; + app.input.clear(); + } else { + return Err(CalculatorError::ParseError); } } + + app.calculator.op(op) } struct ClippyRectangle<'a> {