autorouter: improve error handling by removing some unwraps

This commit is contained in:
Mikolaj Wielgus 2024-07-08 13:25:58 +02:00
parent f4054783d1
commit c36ccc287a
3 changed files with 124 additions and 115 deletions

View File

@ -15,7 +15,7 @@ use super::{
pub struct Autoroute {
ratlines_iter: Box<dyn Iterator<Item = EdgeIndex<usize>>>,
route: Option<Route>,
cur_ratline: Option<EdgeIndex<usize>>,
curr_ratline: Option<EdgeIndex<usize>>,
}
impl Autoroute {
@ -25,16 +25,16 @@ impl Autoroute {
) -> Result<Self, AutorouterError> {
let mut ratlines_iter = Box::new(ratlines.into_iter());
let Some(cur_ratline) = ratlines_iter.next() else {
let Some(curr_ratline) = ratlines_iter.next() else {
return Err(AutorouterError::NothingToRoute);
};
let (source, target) = autorouter.ratline_endpoints(cur_ratline);
let (source, target) = autorouter.ratline_endpoints(curr_ratline);
let mut router = Router::new(autorouter.board.layout_mut());
let this = Self {
ratlines_iter,
cur_ratline: Some(cur_ratline),
curr_ratline: Some(curr_ratline),
route: Some(router.route_walk(source, target, 100.0)?),
};
@ -50,11 +50,11 @@ impl Autoroute {
return Ok(AutorouterStatus::Finished);
};
let Some(cur_ratline) = self.cur_ratline else {
let Some(curr_ratline) = self.curr_ratline else {
return Ok(AutorouterStatus::Finished);
};
let (source, target) = autorouter.ratline_endpoints(cur_ratline);
let (source, target) = autorouter.ratline_endpoints(curr_ratline);
let band = {
let mut router = Router::new(autorouter.board.layout_mut());
@ -67,21 +67,21 @@ impl Autoroute {
autorouter
.ratsnest
.assign_band_to_ratline(self.cur_ratline.unwrap(), band);
.assign_band_to_ratline(self.curr_ratline.unwrap(), band);
autorouter
.board
.try_set_band_between_nodes(source, target, band);
let Some(new_ratline) = self.ratlines_iter.next() else {
self.cur_ratline = None;
self.curr_ratline = None;
return Ok(AutorouterStatus::Finished);
};
let (source, target) = autorouter.ratline_endpoints(new_ratline);
let mut router = Router::new(autorouter.board.layout_mut());
self.cur_ratline = Some(new_ratline);
self.curr_ratline = Some(new_ratline);
self.route = Some(router.route_walk(source, target, 100.0)?);
Ok(AutorouterStatus::Running)

View File

@ -176,9 +176,9 @@ impl<M: AccessMesadata> Invoker<M> {
(self.autorouter, self.history, self.ongoing_command)
}
#[debug_requires(self.ongoing_command.is_none())]
//#[debug_requires(self.ongoing_command.is_none())]
pub fn execute(&mut self, command: Command) -> Result<(), InvokerError> {
let mut execute = self.execute_walk(command);
let mut execute = self.execute_walk(command)?;
loop {
let status = match execute.step(self) {
@ -194,21 +194,21 @@ impl<M: AccessMesadata> Invoker<M> {
}
#[debug_requires(self.ongoing_command.is_none())]
pub fn execute_walk(&mut self, command: Command) -> Execute {
pub fn execute_walk(&mut self, command: Command) -> Result<Execute, InvokerError> {
let execute = self.dispatch_command(&command);
self.ongoing_command = Some(command);
execute
}
#[debug_requires(self.ongoing_command.is_none())]
fn dispatch_command(&mut self, command: &Command) -> Execute {
fn dispatch_command(&mut self, command: &Command) -> Result<Execute, InvokerError> {
match command {
Command::Autoroute(selection) => {
Execute::Autoroute(self.autorouter.autoroute_walk(selection).unwrap())
}
Command::PlaceVia(weight) => {
Execute::PlaceVia(self.autorouter.place_via_walk(*weight).unwrap())
}
Command::Autoroute(selection) => Ok::<Execute, InvokerError>(Execute::Autoroute(
self.autorouter.autoroute_walk(selection)?,
)),
Command::PlaceVia(weight) => Ok::<Execute, InvokerError>(Execute::PlaceVia(
self.autorouter.place_via_walk(*weight)?,
)),
}
}
@ -227,7 +227,7 @@ impl<M: AccessMesadata> Invoker<M> {
//#[debug_requires(self.ongoing.is_none())]
pub fn redo(&mut self) -> Result<(), InvokerError> {
let command = self.history.last_undone()?.clone();
let mut execute = self.execute_walk(command);
let mut execute = self.execute_walk(command)?;
loop {
let status = match execute.step(self) {

View File

@ -4,7 +4,9 @@ use std::{
};
use topola::{
autorouter::invoker::{Command, Execute, ExecuteWithStatus, Invoker, InvokerStatus},
autorouter::invoker::{
Command, Execute, ExecuteWithStatus, Invoker, InvokerError, InvokerStatus,
},
specctra::mesadata::SpecctraMesadata,
};
@ -37,8 +39,10 @@ impl Top {
arc_mutex_maybe_invoker: Arc<Mutex<Option<Invoker<SpecctraMesadata>>>>,
maybe_execute: &mut Option<ExecuteWithStatus>,
maybe_overlay: &mut Option<Overlay>,
) {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
) -> Result<(), InvokerError> {
Ok::<(), InvokerError>(
egui::TopBottomPanel::top("top_panel")
.show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Open").clicked() {
@ -112,7 +116,7 @@ impl Top {
let selection = overlay.selection().clone();
overlay.clear_selection();
maybe_execute.insert(ExecuteWithStatus::new(
invoker.execute_walk(Command::Autoroute(selection)),
invoker.execute_walk(Command::Autoroute(selection))?,
));
}
}
@ -125,7 +129,8 @@ impl Top {
if ui.button("Undo").clicked()
|| ctx.input_mut(|i| i.consume_key(egui::Modifiers::CTRL, egui::Key::Z))
{
if let Some(invoker) = arc_mutex_maybe_invoker.lock().unwrap().as_mut() {
if let Some(invoker) = arc_mutex_maybe_invoker.lock().unwrap().as_mut()
{
invoker.undo();
}
}
@ -133,7 +138,8 @@ impl Top {
if ui.button("Redo").clicked()
|| ctx.input_mut(|i| i.consume_key(egui::Modifiers::CTRL, egui::Key::Y))
{
if let Some(ref mut invoker) = arc_mutex_maybe_invoker.lock().unwrap().as_mut()
if let Some(ref mut invoker) =
arc_mutex_maybe_invoker.lock().unwrap().as_mut()
{
invoker.redo();
}
@ -147,7 +153,10 @@ impl Top {
ui.separator();
egui::widgets::global_dark_light_mode_buttons(ui);
Ok::<(), InvokerError>(())
});
});
})
.inner,
)
}
}