Browse Source

Replace Display with Debug

Kevin Lee 1 year ago
parent
commit
6f4bc33636

+ 17 - 1
src/ezcad/array_of.rs

@@ -7,7 +7,7 @@ use std::{
 
 /// Wrapper to prefix array of single type
 #[binrw]
-#[derive(Debug, PartialEq)]
+#[derive(PartialEq)]
 pub struct ArrayOf<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199
@@ -30,6 +30,22 @@ where
     _marker: PhantomData<S>,
 }
 
+impl<T, S> Debug for ArrayOf<T, S>
+where
+    T: for<'a> BinRead<Args<'a> = ()> + 'static,
+    T: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    T: Debug,
+    S: for<'a> BinRead<Args<'a> = ()> + 'static,
+    S: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    S: TryFrom<usize> + Display + Debug + Copy + Clone,
+    <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
+    usize: TryFrom<S>,
+{
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{:?}", self.value)
+    }
+}
+
 impl<T, S> Deref for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + 'static,

+ 13 - 2
src/ezcad/field_of.rs

@@ -1,13 +1,13 @@
 use binrw::{binrw, BinRead, BinWrite};
 use std::{
-    fmt::Display,
+    fmt::{Debug, Display},
     mem::size_of,
     ops::{Deref, DerefMut},
 };
 
 /// Wrapper to prefix arbitrary structs with size of struct as u32
 #[binrw]
-#[derive(Debug, PartialEq)]
+#[derive(PartialEq)]
 pub struct FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
@@ -43,6 +43,17 @@ where
     }
 }
 
+impl<T> Debug for FieldOf<T>
+where
+    T: for<'a> BinRead<Args<'a> = ()>,
+    T: for<'a> BinWrite<Args<'a> = ()>,
+    T: Debug,
+{
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{:?}", self.value)
+    }
+}
+
 impl<T> Display for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,

+ 0 - 12
src/ezcad/objects/circle.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 
 use crate::{
@@ -19,13 +17,3 @@ pub struct Circle {
     pub clockwise: U32,
     _unknown_1: [Field; 2],
 }
-
-impl Display for Circle {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "{} origin: {}, radius: {:.2}, start_angle: {:.2}, clockwise: {}",
-            self.base, *self.origin, *self.radius, *self.start_angle, *self.clockwise
-        )
-    }
-}

+ 0 - 18
src/ezcad/objects/ellipse.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 
 use crate::{
@@ -21,19 +19,3 @@ pub struct Ellipse {
     _unknown_1: [Field; 2],
     pub open_curve: U32,
 }
-
-impl Display for Ellipse {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "{} corners: {}{}, start angle: {:.2}, end angle: {:.2}, clockwise: {}, open: {}",
-            self.base,
-            *self.corner_a,
-            *self.corner_b,
-            *self.start_angle,
-            *self.end_angle,
-            *self.clockwise,
-            *self.open_curve
-        )
-    }
-}

+ 0 - 8
src/ezcad/objects/hatch.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 use modular_bitfield::{
     bitfield,
@@ -99,9 +97,3 @@ pub struct Hatch {
     _unknown_6: [Field; 13],
     pub lines: ArrayOf<HatchLine>,
 }
-
-impl Display for Hatch {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "[HATCH]")
-    }
-}

+ 0 - 22
src/ezcad/objects/line.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 
 use crate::{array_of::ArrayOf, types::Point};
@@ -21,23 +19,3 @@ pub struct Lines {
     pub base: ObjectBase,
     pub lines: ArrayOf<LineType, u64>,
 }
-
-impl Display for Lines {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "{} ", self.base)?;
-        for line in self.lines.iter() {
-            match line {
-                LineType::Point { _zero, point } => write!(f, "point: {}", point)?,
-                LineType::Line { _zero, points } => {
-                    write!(f, "points: ")?;
-                    points.iter().try_for_each(|x| write!(f, "{}", x))?;
-                }
-                LineType::Bezier { _zero, points } => {
-                    write!(f, "points: ")?;
-                    points.iter().try_for_each(|x| write!(f, "{}", x))?;
-                }
-            }
-        }
-        Ok(())
-    }
-}

+ 0 - 32
src/ezcad/objects/mod.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 use modular_bitfield::{
     bitfield,
@@ -52,22 +50,6 @@ pub struct ObjectBase {
     _unknown_3: Field,
 }
 
-impl Display for ObjectBase {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "[{}, disabled: {}, locked: {}, count: {}, origin: {}, z: {:.2}, a: {:.2}]",
-            *self.obj_type,
-            self.flags.disabled() != 0,
-            self.flags.aspect_ratio_unlocked() != 0,
-            *self.count,
-            *self.origin,
-            *self.z,
-            *self.a,
-        )
-    }
-}
-
 #[derive(BinRead, BinWrite, Debug)]
 pub enum Object {
     #[brw(magic = 1u32)]
@@ -85,17 +67,3 @@ pub enum Object {
     #[brw(magic = 32u32)]
     Hatch(Hatch),
 }
-
-impl Display for Object {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            Object::Curve(x) => write!(f, "{}", x),
-            Object::Point(x) => write!(f, "{}", x),
-            Object::Rectangle(x) => write!(f, "{}", x),
-            Object::Circle(x) => write!(f, "{}", x),
-            Object::Ellipse(x) => write!(f, "{}", x),
-            Object::Polygon(x) => write!(f, "{}", x),
-            Object::Hatch(x) => write!(f, "{}", x),
-        }
-    }
-}

+ 0 - 20
src/ezcad/objects/polygon.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 
 use crate::{
@@ -23,21 +21,3 @@ pub struct Polygon {
     pub edges: U32,
     _unknown_1: [Field; 2],
 }
-
-impl Display for Polygon {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "{} corners: {}{}, offsets: ({:.2}, {:.2}, {:.2}, {:.2}), edges: {}, invert: {}",
-            self.base,
-            *self.corner_a,
-            *self.corner_b,
-            *self.offset_cx,
-            *self.offset_cy,
-            *self.offset_dx,
-            *self.offset_dy,
-            *self.edges,
-            *self.invert_shape
-        )
-    }
-}

+ 0 - 18
src/ezcad/objects/rectangle.rs

@@ -1,5 +1,3 @@
-use std::fmt::Display;
-
 use binrw::{BinRead, BinWrite};
 
 use crate::{
@@ -21,19 +19,3 @@ pub struct Rectangle {
     pub round_top_left: F64,
     _unknown_1: [Field; 2],
 }
-
-impl Display for Rectangle {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "{} corners: {}{}, rounding: ({:.2}, {:.2}, {:.2}, {:.2})",
-            self.base,
-            *self.corner_a,
-            *self.corner_b,
-            *self.round_top_left,
-            *self.round_top_right,
-            *self.round_bottom_left,
-            *self.round_bottom_right
-        )
-    }
-}

+ 8 - 18
src/ezcad/types.rs

@@ -1,6 +1,6 @@
 #![allow(dead_code)]
 
-use std::fmt::Display;
+use std::fmt::{Debug, Display};
 
 use binrw::{binrw, BinRead, BinWrite};
 
@@ -14,7 +14,7 @@ pub type U16 = FieldOf<u16>;
 pub type F64 = FieldOf<f64>;
 
 #[binrw]
-#[derive(Debug, PartialEq)]
+#[derive(PartialEq)]
 pub struct WString {
     // Temporary variable that holds number of elements
     #[br(temp)]
@@ -38,6 +38,12 @@ impl From<String> for WString {
     }
 }
 
+impl Debug for WString {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "\"{}\"", String::from(self))
+    }
+}
+
 impl Display for WString {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "{}", String::from(self))
@@ -52,28 +58,12 @@ pub struct Rgba {
     pub alpha: u8,
 }
 
-impl Display for Rgba {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "({}, {}, {}, {})",
-            self.red, self.green, self.blue, self.alpha
-        )
-    }
-}
-
 #[derive(BinRead, BinWrite, PartialEq, Debug)]
 pub struct Point {
     pub x: f64,
     pub y: f64,
 }
 
-impl Display for Point {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "({:.2}, {:.2})", self.x, self.y)
-    }
-}
-
 #[derive(BinRead, BinWrite, PartialEq, Debug, strum::Display)]
 #[brw(repr(u32))]
 #[repr(u32)]

+ 2 - 1
src/main.rs

@@ -50,7 +50,8 @@ fn main() {
     // dbg!(&file.pens_offset.start_offset.pens[255].wobble_type);
     for layer in file.layers.iter() {
         for object in layer.objects.iter() {
-            info!("{object}");
+            // info!("{object:?}");
+            dbg!(object);
         }
     }
 }