From f384806b72937ad65f416c973da6d0088eb2cd24 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 10 Feb 2023 12:46:02 -0330 Subject: fix bug: parsing buffer when it contains invalid chars --- src/input.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/input.rs') diff --git a/src/input.rs b/src/input.rs index 59bf5b9..ab4a1d1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -27,13 +27,13 @@ impl Calculator { KeyCode::Backspace => { self.input_buffer.pop(); } - KeyCode::Enter if self.input_buffer.len() > 0 => { + KeyCode::Enter => { if let Ok(func) = Function::parse(&self.input_buffer) { - if let Some(f) = self.stack.pop() { - self.stack.push(func.call_on(f)); + if let Some(st) = self.stack.pop() { + self.stack.push(func.call_on(st)); } - } else { - self.stack.push(self.input_buffer.parse::().unwrap()); + } else if let Ok(bu) = self.input_buffer.parse::() { + self.stack.push(bu); } self.input_buffer = String::new(); } @@ -66,11 +66,11 @@ impl Calculator { // Swap the bottom of the stack and the input buffer. If the input buffer // is empty, this simply pops from the stack into the input buffer. fn swap(&mut self) { - if let Some(f) = self.stack.pop() { - if self.input_buffer.len() > 0 { - self.stack.push(self.input_buffer.parse::().unwrap()); + if let Some(st) = self.stack.pop() { + if let Ok(bu) = self.input_buffer.parse::() { + self.stack.push(bu); } - self.input_buffer = format!("{}", f); + self.input_buffer = format!("{}", st); } } -- cgit v1.2.3