autorouter: use new `Step` trait for stepping

This commit is contained in:
Mikolaj Wielgus 2024-08-03 16:40:04 +02:00
parent 5b32797022
commit 1305487c7f
5 changed files with 46 additions and 22 deletions

View File

@ -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<M: AccessMesadata> Step<Autorouter<M>, AutorouterStatus, AutorouterError> for Autoroute {
fn step(
&mut self,
autorouter: &mut Autorouter<impl AccessMesadata>,
autorouter: &mut Autorouter<M>,
) -> Result<AutorouterStatus, AutorouterError> {
let Some(ref mut route) = self.route else {
// Shouldn't happen.

View File

@ -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<M: AccessMesadata> {
pub(super) board: Board<M>,
pub(super) ratsnest: Ratsnest,

View File

@ -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<M: AccessMesadata>(
&mut self,
invoker: &mut Invoker<M>,
) -> Result<InvokerStatus, InvokerError> {
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<M: AccessMesadata>(
&mut self,
invoker: &mut Invoker<M>,
@ -111,6 +98,25 @@ impl Execute {
}
}
impl<M: AccessMesadata> Step<Invoker<M>, InvokerStatus, InvokerError> for Execute {
fn step(&mut self, invoker: &mut Invoker<M>) -> Result<InvokerStatus, InvokerError> {
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<InvokerStatus>,

View File

@ -10,4 +10,5 @@ pub mod layout;
pub mod math;
pub mod router;
pub mod specctra;
pub mod step;
pub mod triangulation;

7
src/step.rs Normal file
View File

@ -0,0 +1,7 @@
pub trait IsFinished {
fn finished(&self) -> bool;
}
pub trait Step<C, S: IsFinished, E> {
fn step(&mut self, context: &mut C) -> Result<S, E>;
}