Set cancel return type to result

This commit is contained in:
Austen Adler 2021-05-02 23:16:53 -04:00
parent d81fb1f45d
commit 062cd97609
2 changed files with 12 additions and 5 deletions

View File

@ -205,7 +205,8 @@ impl<'a> Calculator<'a> {
for c in value.chars() { for c in value.chars() {
self.take_input(c).map_err(|e| { self.take_input(c).map_err(|e| {
self.cancel(); // Try cancelling, but if you cannot, that is okay
self.cancel().unwrap_or(());
e e
})?; })?;
} }
@ -239,9 +240,15 @@ impl<'a> Calculator<'a> {
} }
} }
pub fn cancel(&mut self) { pub fn cancel(&mut self) -> CalculatorResult<()> {
self.state = CalculatorState::Normal; self.state = CalculatorState::Normal;
// We died in a macro. Quit and push an end macro state
if !self.active_macros.is_empty() {
self.active_macros.clear(); self.active_macros.clear();
// Should always be successful, but report the error if there is one
self.op(CalculatorOperation::Macro(MacroState::End))?;
}
Ok(())
} }
pub fn backspace(&mut self) -> CalculatorResult<()> { pub fn backspace(&mut self) -> CalculatorResult<()> {
self.l.pop(); self.l.pop();

View File

@ -268,7 +268,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<bool
(AppState::Help, _) => match key { (AppState::Help, _) => match key {
Key::Esc | Key::Char('q') => { Key::Esc | Key::Char('q') => {
app.state = AppState::Calculator; app.state = AppState::Calculator;
app.calculator.cancel(); app.calculator.cancel()?;
} }
_ => {} _ => {}
}, },
@ -277,7 +277,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<bool
| (AppState::Calculator, CalculatorState::WaitingForMacro) => match key { | (AppState::Calculator, CalculatorState::WaitingForMacro) => match key {
Key::Esc => { Key::Esc => {
app.state = AppState::Calculator; app.state = AppState::Calculator;
app.calculator.cancel(); app.calculator.cancel()?;
} }
Key::Char(c) => { Key::Char(c) => {
app.calculator.take_input(c)?; app.calculator.take_input(c)?;