Bubble more errors up for cleaner code

This commit is contained in:
Austen Adler 2021-04-27 00:08:10 -04:00
parent fe38a6c7b1
commit cdb2011efa
3 changed files with 33 additions and 28 deletions

View File

@ -1,3 +1,5 @@
use std::fmt;
pub enum CalculatorError { pub enum CalculatorError {
ArithmeticError, ArithmeticError,
NotEnoughStackEntries, NotEnoughStackEntries,
@ -7,22 +9,23 @@ pub enum CalculatorError {
NoSuchRegister, NoSuchRegister,
NoSuchMacro, NoSuchMacro,
ParseError, ParseError,
NoSuchOperator,
} }
impl CalculatorError { impl fmt::Display for CalculatorError {
//TODO: Use &str instead of Strings fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
pub fn message(&self) -> String {
match self { match self {
CalculatorError::ArithmeticError => String::from("Arithmetic Error"), CalculatorError::ArithmeticError => write!(f, "Arithmetic Error"),
CalculatorError::NotEnoughStackEntries => String::from("Not enough items in the stack"), CalculatorError::NotEnoughStackEntries => write!(f, "Not enough items in the stack"),
CalculatorError::CorruptStateChange(msg) => { 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::EmptyHistory(msg) => write!(f, "No history to {}", msg),
CalculatorError::NoSuchConstant => String::from("No such constant"), CalculatorError::NoSuchOperator => write!(f, "No such operator"),
CalculatorError::NoSuchRegister => String::from("No such register"), CalculatorError::NoSuchConstant => write!(f, "No such constant"),
CalculatorError::NoSuchMacro => String::from("No such macro"), CalculatorError::NoSuchRegister => write!(f, "No such register"),
CalculatorError::ParseError => String::from("Parse error"), CalculatorError::NoSuchMacro => write!(f, "No such macro"),
CalculatorError::ParseError => write!(f, "Parse error"),
} }
} }
} }

View File

@ -1,3 +1,5 @@
use super::errors::CalculatorError;
pub enum CalculatorOperation { pub enum CalculatorOperation {
Add, Add,
Subtract, Subtract,
@ -29,7 +31,7 @@ pub enum CalculatorOperation {
} }
impl CalculatorOperation { impl CalculatorOperation {
pub fn from_char(key: char) -> Result<CalculatorOperation, ()> { pub fn from_char(key: char) -> Result<CalculatorOperation, CalculatorError> {
match key { match key {
'+' => Ok(CalculatorOperation::Add), '+' => Ok(CalculatorOperation::Add),
'-' => Ok(CalculatorOperation::Subtract), '-' => Ok(CalculatorOperation::Subtract),
@ -59,7 +61,7 @@ impl CalculatorOperation {
'l' => Ok(CalculatorOperation::Log), 'l' => Ok(CalculatorOperation::Log),
'L' => Ok(CalculatorOperation::Ln), 'L' => Ok(CalculatorOperation::Ln),
'e' => Ok(CalculatorOperation::E), 'e' => Ok(CalculatorOperation::E),
_ => Err(()), _ => Err(CalculatorError::NoSuchOperator),
} }
} }

View File

@ -228,7 +228,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Exit the program // Exit the program
Ok(true) => break 'outer Ok(()), Ok(true) => break 'outer Ok(()),
Ok(false) => None, Ok(false) => None,
Err(e) => Some(e.message()), Err(e) => Some(format!("{}", e)),
}; };
} }
@ -239,7 +239,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Exit the program // Exit the program
Ok(true) => break 'outer Ok(()), Ok(true) => break 'outer Ok(()),
Ok(false) => None, Ok(false) => None,
Err(e) => Some(e.message()), Err(e) => Some(format!("{}", e)),
}; };
} }
Event::MacroEnd => app.current_macro = None, Event::MacroEnd => app.current_macro = None,
@ -290,7 +290,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
} }
Key::Char('\n') | Key::Char(' ') => { Key::Char('\n') | Key::Char(' ') => {
if app.input.is_empty() { if app.input.is_empty() {
calc_operation(app, '\n'); calc_operation(app, '\n')?;
} else { } else {
let mut tmp_input = app.input.clone(); let mut tmp_input = app.input.clone();
if tmp_input.ends_with('e') { if tmp_input.ends_with('e') {
@ -305,7 +305,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
} }
} }
Key::Right => { Key::Right => {
calc_operation(app, '>'); calc_operation(app, '>')?;
} }
Key::Down => { Key::Down => {
if let Ok(x) = app.calculator.pop() { if let Ok(x) = app.calculator.pop() {
@ -319,7 +319,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
app.input.clear(); app.input.clear();
} }
Key::Char(c) => { Key::Char(c) => {
calc_operation(app, c); calc_operation(app, c)?;
} }
_ => {} _ => {}
}, },
@ -365,6 +365,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
Key::Char(c) => { Key::Char(c) => {
if !app.input.is_empty() { if !app.input.is_empty() {
//TODO: A better way to do this //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::<f64>() { if let Ok(f) = app.input.parse::<f64>() {
match app.calculator.push(f) { match app.calculator.push(f) {
Ok(()) => app.input.clear(), Ok(()) => app.input.clear(),
@ -386,19 +387,18 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
return Ok(false); return Ok(false);
} }
fn calc_operation(app: &mut App, c: char) { fn calc_operation(app: &mut App, c: char) -> Result<(), CalculatorError> {
if let Ok(op) = CalculatorOperation::from_char(c) { let op = CalculatorOperation::from_char(c)?;
if !app.input.is_empty() {
if let Ok(f) = app.input.parse::<f64>() { if let Ok(f) = app.input.parse::<f64>() {
if app.calculator.push(f).is_ok() { app.calculator.push(f)?;
app.input.clear(); app.input.clear();
} else {
return Err(CalculatorError::ParseError);
} }
} }
app.error_msg = match app.calculator.op(op) { app.calculator.op(op)
Err(e) => Some(e.message()),
Ok(()) => None,
}
}
} }
struct ClippyRectangle<'a> { struct ClippyRectangle<'a> {