From c24d748bea6980147f469cee5eeaac4d570f5637 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Thu, 20 Jun 2024 12:14:51 +0200 Subject: [PATCH] autorouter: don't save in history until command is successfully finished --- src/autorouter/invoker.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index 8a608da..c34c10f 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -1,3 +1,4 @@ +use contracts::debug_requires; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -41,6 +42,23 @@ impl Execute { pub fn step( &mut self, invoker: &mut Invoker, + ) -> Result { + match self.step_catch_err(invoker) { + Ok(InvokerStatus::Running) => Ok(InvokerStatus::Running), + Ok(InvokerStatus::Finished) => { + invoker.history.do_(invoker.ongoing.take().unwrap()); + Ok(InvokerStatus::Finished) + } + Err(err) => { + invoker.ongoing = None; + Err(err) + } + } + } + + fn step_catch_err( + &mut self, + invoker: &mut Invoker, ) -> Result { match self { Execute::Autoroute(autoroute) => match autoroute.step(&mut invoker.autorouter)? { @@ -58,6 +76,7 @@ impl Execute { pub struct Invoker { autorouter: Autorouter, history: History, + ongoing: Option, } impl Invoker { @@ -69,13 +88,15 @@ impl Invoker { Self { autorouter, history, + ongoing: None, } } - pub fn destruct(self) -> (Autorouter, History) { - (self.autorouter, self.history) + pub fn destruct(self) -> (Autorouter, History, Option) { + (self.autorouter, self.history, self.ongoing) } + #[debug_requires(self.ongoing.is_none())] pub fn execute(&mut self, command: Command) -> Result<(), InvokerError> { let mut execute = self.execute_walk(command); @@ -92,12 +113,14 @@ impl Invoker { } } + #[debug_requires(self.ongoing.is_none())] pub fn execute_walk(&mut self, command: Command) -> Execute { let execute = self.dispatch_command(&command); - self.history.do_(command); + self.ongoing = Some(command); execute } + #[debug_requires(self.ongoing.is_none())] fn dispatch_command(&mut self, command: &Command) -> Execute { match command { Command::Autoroute(selection) => { @@ -109,6 +132,7 @@ impl Invoker { } } + #[debug_requires(self.ongoing.is_none())] pub fn undo(&mut self) -> Result<(), InvokerError> { let command = self.history.last_done()?; @@ -117,9 +141,10 @@ impl Invoker { Command::PlaceVia(weight) => self.autorouter.undo_place_via(*weight), } - Ok(self.history.undo()?) + Ok::<(), InvokerError>(self.history.undo()?) } + //#[debug_requires(self.ongoing.is_none())] pub fn redo(&mut self) -> Result<(), InvokerError> { let command = self.history.last_undone()?.clone(); let mut execute = self.dispatch_command(&command); @@ -136,6 +161,7 @@ impl Invoker { } } + #[debug_requires(self.ongoing.is_none())] pub fn replay(&mut self, history: History) { let (done, undone) = history.destructure();