diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-10 12:46:02 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-10 12:46:02 -0330 |
| commit | f384806b72937ad65f416c973da6d0088eb2cd24 (patch) | |
| tree | ff2a26ee9f17367f6a76572e9efa256d600484d2 | |
| parent | 4777781061ea1381363072d00839d2efc5221099 (diff) | |
| download | pfc-f384806b72937ad65f416c973da6d0088eb2cd24.zip | |
fix bug: parsing buffer when it contains invalid chars
| -rw-r--r-- | src/input.rs | 18 |
1 files changed, 9 insertions, 9 deletions
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::<f64>().unwrap()); + } else if let Ok(bu) = self.input_buffer.parse::<f64>() { + 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::<f64>().unwrap()); + if let Some(st) = self.stack.pop() { + if let Ok(bu) = self.input_buffer.parse::<f64>() { + self.stack.push(bu); } - self.input_buffer = format!("{}", f); + self.input_buffer = format!("{}", st); } } |