From f2969decd0a5cb84a4b827049ec797e25c9fc24a Mon Sep 17 00:00:00 2001 From: Ellen Emilia Anna Zscheile Date: Sun, 2 Feb 2025 21:39:47 +0100 Subject: [PATCH] feat/debug: make core::fmt::Debug output easier to read --- crates/specctra-core/src/math.rs | 26 +++++++++++++++++++++++--- src/drawing/drawing.rs | 15 +++++++++++++-- src/graph.rs | 9 ++++++--- src/router/navmesh.rs | 8 +++++++- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/crates/specctra-core/src/math.rs b/crates/specctra-core/src/math.rs index 4911e2f..752c8d2 100644 --- a/crates/specctra-core/src/math.rs +++ b/crates/specctra-core/src/math.rs @@ -2,22 +2,32 @@ // // SPDX-License-Identifier: MIT -use core::ops::Sub; +use core::{fmt, ops::Sub}; use geo_types::geometry::Point; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct Circle { pub pos: Point, pub r: f64, } -#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct PointWithRotation { pub pos: Point, pub rot: f64, } +impl fmt::Debug for Circle { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Circle") + .field("x", &self.pos.0.x) + .field("y", &self.pos.0.y) + .field("r", &self.r) + .finish() + } +} + impl Circle { /// Calculate the point that lies on the circle at angle `phi`, /// relative to coordinate axes. @@ -52,6 +62,16 @@ impl Sub for Circle { } } +impl fmt::Debug for PointWithRotation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("PointWithRotation") + .field("x", &self.pos.0.x) + .field("y", &self.pos.0.y) + .field("rot", &self.rot) + .finish() + } +} + impl Default for PointWithRotation { fn default() -> Self { Self { diff --git a/src/drawing/drawing.rs b/src/drawing/drawing.rs index 9359cf9..c0b3281 100644 --- a/src/drawing/drawing.rs +++ b/src/drawing/drawing.rs @@ -7,6 +7,7 @@ use derive_getters::Getters; use enum_dispatch::enum_dispatch; use geo::Point; +use core::fmt; use rstar::{RTree, AABB}; use thiserror::Error; @@ -44,8 +45,7 @@ use crate::{ graph::MakeRef, }; -#[enum_dispatch] -#[derive(Error, Debug, Clone, Copy)] +#[derive(Clone, Copy, Error)] pub enum DrawingException { #[error(transparent)] NoTangents(#[from] NoTangents), @@ -57,6 +57,17 @@ pub enum DrawingException { AlreadyConnected(#[from] AlreadyConnected), } +impl fmt::Debug for DrawingException { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::NoTangents(this) => fmt::Debug::fmt(this, f), + Self::Infringement(this) => fmt::Debug::fmt(this, f), + Self::Collision(this) => fmt::Debug::fmt(this, f), + Self::AlreadyConnected(this) => fmt::Debug::fmt(this, f), + } + } +} + // TODO add real error messages + these should eventually use Display #[derive(Error, Debug, Clone, Copy)] #[error("{0:?} infringes on {1:?}")] diff --git a/src/graph.rs b/src/graph.rs index d0ed84d..fdb1ad4 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -29,6 +29,7 @@ pub struct GenericIndex { } impl GenericIndex { + #[inline] pub fn new(index: NodeIndex) -> Self { Self { node_index: index, @@ -50,10 +51,9 @@ impl core::clone::Clone for GenericIndex { impl core::marker::Copy for GenericIndex {} impl core::fmt::Debug for GenericIndex { + #[inline] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_tuple("GenericIndex") - .field(&self.node_index.index()) - .finish() + core::fmt::Display::fmt(&self.node_index.index(), f) } } @@ -67,18 +67,21 @@ impl PartialEq for GenericIndex { impl Eq for GenericIndex {} impl PartialOrd for GenericIndex { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.node_index.partial_cmp(&other.node_index) } } impl Ord for GenericIndex { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.node_index.cmp(&other.node_index) } } impl GetPetgraphIndex for GenericIndex { + #[inline] fn petgraph_index(&self) -> NodeIndex { self.node_index } diff --git a/src/router/navmesh.rs b/src/router/navmesh.rs index a4d3195..6764ad1 100644 --- a/src/router/navmesh.rs +++ b/src/router/navmesh.rs @@ -37,9 +37,15 @@ use crate::{ use super::RouterOptions; -#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] pub struct NavvertexIndex(NodeIndex); +impl core::fmt::Debug for NavvertexIndex { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + core::fmt::Display::fmt(&self.0.index(), f) + } +} + impl GetPetgraphIndex for NavvertexIndex { fn petgraph_index(&self) -> NodeIndex { self.0