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 {
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"),
}
}
}

View File

@ -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<CalculatorOperation, ()> {
pub fn from_char(key: char) -> Result<CalculatorOperation, CalculatorError> {
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),
}
}

View File

@ -228,7 +228,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// 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<dyn Error>> {
// 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<bool, Calculat
}
Key::Char('\n') | Key::Char(' ') => {
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<bool, Calculat
}
}
Key::Right => {
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<bool, Calculat
app.input.clear();
}
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) => {
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::<f64>() {
match app.calculator.push(f) {
Ok(()) => app.input.clear(),
@ -386,19 +387,18 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> Result<bool, Calculat
return Ok(false);
}
fn calc_operation(app: &mut App, c: char) {
if let Ok(op) = CalculatorOperation::from_char(c) {
fn calc_operation(app: &mut App, c: char) -> Result<(), CalculatorError> {
let op = CalculatorOperation::from_char(c)?;
if !app.input.is_empty() {
if let Ok(f) = app.input.parse::<f64>() {
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> {