feat/debug: make core::fmt::Debug output easier to read

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-02-02 21:39:47 +01:00
parent ffb0c94080
commit f2969decd0
4 changed files with 49 additions and 9 deletions

View File

@ -2,22 +2,32 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use core::ops::Sub; use core::{fmt, ops::Sub};
use geo_types::geometry::Point; use geo_types::geometry::Point;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Circle { pub struct Circle {
pub pos: Point, pub pos: Point,
pub r: f64, pub r: f64,
} }
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PointWithRotation { pub struct PointWithRotation {
pub pos: Point, pub pos: Point,
pub rot: f64, 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 { impl Circle {
/// Calculate the point that lies on the circle at angle `phi`, /// Calculate the point that lies on the circle at angle `phi`,
/// relative to coordinate axes. /// 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 { impl Default for PointWithRotation {
fn default() -> Self { fn default() -> Self {
Self { Self {

View File

@ -7,6 +7,7 @@ use derive_getters::Getters;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use geo::Point; use geo::Point;
use core::fmt;
use rstar::{RTree, AABB}; use rstar::{RTree, AABB};
use thiserror::Error; use thiserror::Error;
@ -44,8 +45,7 @@ use crate::{
graph::MakeRef, graph::MakeRef,
}; };
#[enum_dispatch] #[derive(Clone, Copy, Error)]
#[derive(Error, Debug, Clone, Copy)]
pub enum DrawingException { pub enum DrawingException {
#[error(transparent)] #[error(transparent)]
NoTangents(#[from] NoTangents), NoTangents(#[from] NoTangents),
@ -57,6 +57,17 @@ pub enum DrawingException {
AlreadyConnected(#[from] AlreadyConnected), 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 // TODO add real error messages + these should eventually use Display
#[derive(Error, Debug, Clone, Copy)] #[derive(Error, Debug, Clone, Copy)]
#[error("{0:?} infringes on {1:?}")] #[error("{0:?} infringes on {1:?}")]

View File

@ -29,6 +29,7 @@ pub struct GenericIndex<W> {
} }
impl<W> GenericIndex<W> { impl<W> GenericIndex<W> {
#[inline]
pub fn new(index: NodeIndex<usize>) -> Self { pub fn new(index: NodeIndex<usize>) -> Self {
Self { Self {
node_index: index, node_index: index,
@ -50,10 +51,9 @@ impl<W> core::clone::Clone for GenericIndex<W> {
impl<W> core::marker::Copy for GenericIndex<W> {} impl<W> core::marker::Copy for GenericIndex<W> {}
impl<W> core::fmt::Debug for GenericIndex<W> { impl<W> core::fmt::Debug for GenericIndex<W> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("GenericIndex") core::fmt::Display::fmt(&self.node_index.index(), f)
.field(&self.node_index.index())
.finish()
} }
} }
@ -67,18 +67,21 @@ impl<W> PartialEq for GenericIndex<W> {
impl<W> Eq for GenericIndex<W> {} impl<W> Eq for GenericIndex<W> {}
impl<W> PartialOrd for GenericIndex<W> { impl<W> PartialOrd for GenericIndex<W> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.node_index.partial_cmp(&other.node_index) self.node_index.partial_cmp(&other.node_index)
} }
} }
impl<W> Ord for GenericIndex<W> { impl<W> Ord for GenericIndex<W> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
self.node_index.cmp(&other.node_index) self.node_index.cmp(&other.node_index)
} }
} }
impl<W> GetPetgraphIndex for GenericIndex<W> { impl<W> GetPetgraphIndex for GenericIndex<W> {
#[inline]
fn petgraph_index(&self) -> NodeIndex<usize> { fn petgraph_index(&self) -> NodeIndex<usize> {
self.node_index self.node_index
} }

View File

@ -37,9 +37,15 @@ use crate::{
use super::RouterOptions; use super::RouterOptions;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
pub struct NavvertexIndex(NodeIndex<usize>); pub struct NavvertexIndex(NodeIndex<usize>);
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 { impl GetPetgraphIndex for NavvertexIndex {
fn petgraph_index(&self) -> NodeIndex<usize> { fn petgraph_index(&self) -> NodeIndex<usize> {
self.0 self.0