From b6cfcc851801bc86546b609994f22f9ffba89009 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 25 Jun 2024 15:00:15 +0200 Subject: [PATCH] router: don't panic if wraparound navvertex has no cw This may happen if target's guidecircle is in line of sight but its center is not. --- src/router/tracer.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/router/tracer.rs b/src/router/tracer.rs index 1e9b071..4232cb3 100644 --- a/src/router/tracer.rs +++ b/src/router/tracer.rs @@ -1,5 +1,6 @@ use contracts::{debug_ensures, debug_requires}; use petgraph::graph::{NodeIndex, UnGraph}; +use thiserror::Error; use crate::{ drawing::{ @@ -17,6 +18,14 @@ use crate::{ }, }; +#[derive(Error, Debug, Clone, Copy)] +pub enum TracerException { + #[error(transparent)] + CannotDraw(#[from] DrawException), + #[error("cannot wrap")] + CannotWrap, +} + #[derive(Debug)] pub struct Trace { pub path: Vec>, @@ -54,8 +63,8 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { trace: &mut Trace, target: FixedDotIndex, width: f64, - ) -> Result { - Draw::new(self.layout).finish_in_dot(trace.head, target, width) + ) -> Result { + Ok(Draw::new(self.layout).finish_in_dot(trace.head, target, width)?) } #[debug_requires(path[0] == trace.path[0])] @@ -66,7 +75,7 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { trace: &mut Trace, path: &[NodeIndex], width: f64, - ) -> Result<(), DrawException> { + ) -> Result<(), TracerException> { let prefix_length = trace .path .iter() @@ -76,7 +85,7 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { let length = trace.path.len(); self.undo_path(graph, trace, length - prefix_length); - self.path(graph, trace, &path[prefix_length..], width) + Ok::<(), TracerException>(self.path(graph, trace, &path[prefix_length..], width)?) } #[debug_ensures(ret.is_ok() -> trace.path.len() == old(trace.path.len() + path.len()))] @@ -86,11 +95,11 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { trace: &mut Trace, path: &[NodeIndex], width: f64, - ) -> Result<(), DrawException> { + ) -> Result<(), TracerException> { for (i, vertex) in path.iter().enumerate() { if let Err(err) = self.step(graph, trace, *vertex, width) { self.undo_path(graph, trace, i); - return Err(err); + return Err(err.into()); } } @@ -118,11 +127,11 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { trace: &mut Trace, to: NodeIndex, width: f64, - ) -> Result<(), DrawException> { + ) -> Result<(), TracerException> { trace.head = self.wrap(graph, trace.head, to, width)?.into(); trace.path.push(to); - Ok::<(), DrawException>(()) + Ok::<(), TracerException>(()) } fn wrap( @@ -131,8 +140,10 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { head: Head, around: NodeIndex, width: f64, - ) -> Result { - let cw = self.maybe_cw(graph, around).unwrap(); + ) -> Result { + let cw = self + .maybe_cw(graph, around) + .ok_or(TracerException::CannotWrap)?; match self.binavvertex(graph, around) { BinavvertexNodeIndex::FixedDot(dot) => { @@ -152,9 +163,8 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { around: FixedDotIndex, cw: bool, width: f64, - ) -> Result { - let head = Draw::new(self.layout).cane_around_dot(head, around.into(), cw, width)?; - Ok(head) + ) -> Result { + Ok(Draw::new(self.layout).cane_around_dot(head, around.into(), cw, width)?) } fn wrap_around_loose_bend( @@ -164,10 +174,8 @@ impl<'a, R: RulesTrait> Tracer<'a, R> { around: LooseBendIndex, cw: bool, width: f64, - ) -> Result { - let head = Draw::new(self.layout).cane_around_bend(head, around.into(), cw, width)?; - - Ok(head) + ) -> Result { + Ok(Draw::new(self.layout).cane_around_bend(head, around.into(), cw, width)?) } #[debug_ensures(trace.path.len() == old(trace.path.len() - 1))]