diff --git a/src/autorouter/autoroute.rs b/src/autorouter/autoroute.rs index b2d1188..e391d2d 100644 --- a/src/autorouter/autoroute.rs +++ b/src/autorouter/autoroute.rs @@ -5,6 +5,7 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{navmesh::Navmesh, route::Route, trace::Trace, Router, RouterStatus}, + step::Step, }; use super::{ @@ -40,10 +41,12 @@ impl Autoroute { Ok(this) } +} - pub fn step( +impl Step, AutorouterStatus, AutorouterError> for Autoroute { + fn step( &mut self, - autorouter: &mut Autorouter, + autorouter: &mut Autorouter, ) -> Result { let Some(ref mut route) = self.route else { // Shouldn't happen. diff --git a/src/autorouter/autorouter.rs b/src/autorouter/autorouter.rs index 7a01d95..7d2afae 100644 --- a/src/autorouter/autorouter.rs +++ b/src/autorouter/autorouter.rs @@ -7,6 +7,7 @@ use crate::{ drawing::{band::BandTermsegIndex, dot::FixedDotIndex, Infringement}, layout::via::ViaWeight, router::{navmesh::NavmeshError, RouterError}, + step::{IsFinished, Step}, triangulation::GetTrianvertexNodeIndex, }; @@ -36,6 +37,12 @@ pub enum AutorouterStatus { Finished, } +impl IsFinished for AutorouterStatus { + fn finished(&self) -> bool { + matches!(self, AutorouterStatus::Finished) + } +} + pub struct Autorouter { pub(super) board: Board, pub(super) ratsnest: Ratsnest, diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index a0b3181..10055be 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -9,6 +9,7 @@ use crate::{ geometry::primitive::PrimitiveShape, layout::via::ViaWeight, router::{navmesh::Navmesh, trace::Trace}, + step::{IsFinished, Step}, }; use super::{ @@ -54,6 +55,12 @@ pub enum InvokerStatus { Finished, } +impl IsFinished for InvokerStatus { + fn finished(&self) -> bool { + matches!(self, InvokerStatus::Finished) + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Command { Autoroute(PinSelection), @@ -69,26 +76,6 @@ pub enum Execute { } 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) => { - if let Some(command) = invoker.ongoing_command.take() { - invoker.history.do_(command); - } - - Ok(InvokerStatus::Finished) - } - Err(err) => { - invoker.ongoing_command = None; - Err(err) - } - } - } - fn step_catch_err( &mut self, invoker: &mut Invoker, @@ -111,6 +98,25 @@ impl Execute { } } +impl Step, InvokerStatus, InvokerError> for Execute { + fn step(&mut self, invoker: &mut Invoker) -> Result { + match self.step_catch_err(invoker) { + Ok(InvokerStatus::Running) => Ok(InvokerStatus::Running), + Ok(InvokerStatus::Finished) => { + if let Some(command) = invoker.ongoing_command.take() { + invoker.history.do_(command); + } + + Ok(InvokerStatus::Finished) + } + Err(err) => { + invoker.ongoing_command = None; + Err(err) + } + } + } +} + pub struct ExecuteWithStatus { execute: Execute, maybe_status: Option, diff --git a/src/lib.rs b/src/lib.rs index 6084948..2e0434e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,4 +10,5 @@ pub mod layout; pub mod math; pub mod router; pub mod specctra; +pub mod step; pub mod triangulation; diff --git a/src/step.rs b/src/step.rs new file mode 100644 index 0000000..09e79e2 --- /dev/null +++ b/src/step.rs @@ -0,0 +1,7 @@ +pub trait IsFinished { + fn finished(&self) -> bool; +} + +pub trait Step { + fn step(&mut self, context: &mut C) -> Result; +}