瀏覽代碼

Point/line/curve work

Kevin Lee 1 年之前
父節點
當前提交
3af62100c5

+ 37 - 8
src/ezcad/array_of.rs

@@ -1,28 +1,44 @@
 use binrw::{binrw, BinRead, BinWrite};
-use std::ops::{Deref, DerefMut};
+use std::{
+    fmt::{Debug, Display},
+    marker::PhantomData,
+    ops::{Deref, DerefMut},
+};
 
 /// Wrapper to prefix array of single type
 #[binrw]
 #[derive(Debug, PartialEq)]
-pub struct ArrayOf<T>
+pub struct ArrayOf<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    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>,
 {
     // Temporary variable that holds number of elements
     #[br(temp)]
-    #[bw(try_calc(u32::try_from(value.len())))]
-    count: u32,
+    #[bw(try_calc(S::try_from(value.len())))]
+    count: S,
 
     #[br(count = count)]
     pub value: Vec<T>,
+
+    _marker: PhantomData<S>,
 }
 
-impl<T> Deref for ArrayOf<T>
+impl<T, S> Deref for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    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>,
 {
     type Target = Vec<T>;
 
@@ -31,22 +47,35 @@ where
     }
 }
 
-impl<T> DerefMut for ArrayOf<T>
+impl<T, S> DerefMut for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    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 deref_mut(&mut self) -> &mut Self::Target {
         &mut self.value
     }
 }
 
-impl<T> From<Vec<T>> for ArrayOf<T>
+impl<T, S> From<Vec<T>> for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,
+    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 from(value: Vec<T>) -> Self {
-        Self { value }
+        Self {
+            value,
+            _marker: PhantomData,
+        }
     }
 }

+ 1 - 1
src/ezcad/objects/ellipse.rs

@@ -26,7 +26,7 @@ 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: {}",
+            "{} corners: {}{}, start angle: {:.2}, end angle: {:.2}, clockwise: {}, open: {}",
             self.base,
             *self.corner_a,
             *self.corner_b,

+ 16 - 10
src/ezcad/objects/line.rs

@@ -8,18 +8,18 @@ use super::ObjectBase;
 
 #[derive(BinRead, BinWrite, Debug)]
 pub enum LineType {
-    #[brw(magic = 0x0001u64)]
-    Point(Point),
-    #[brw(magic = 0x0100u64)]
-    Line { points: ArrayOf<Point> },
-    #[brw(magic = 0x0300u64)]
-    Bezier { points: ArrayOf<Point> },
+    #[brw(magic = 0x0001u16)]
+    Point { _zero: u32, point: Point },
+    #[brw(magic = 0x0100u16)]
+    Line { _zero: u32, points: ArrayOf<Point> },
+    #[brw(magic = 0x0300u16)]
+    Bezier { _zero: u32, points: ArrayOf<Point> },
 }
 
 #[derive(BinRead, BinWrite, Debug)]
 pub struct Lines {
     pub base: ObjectBase,
-    pub lines: ArrayOf<LineType>,
+    pub lines: ArrayOf<LineType, u64>,
 }
 
 impl Display for Lines {
@@ -27,9 +27,15 @@ impl Display for Lines {
         write!(f, "{} ", self.base)?;
         for line in self.lines.iter() {
             match line {
-                LineType::Point(x) => write!(f, "point: {}", x)?,
-                LineType::Line { points } => todo!(),
-                LineType::Bezier { points } => todo!(),
+                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(())

+ 1 - 1
src/ezcad/objects/mod.rs

@@ -53,7 +53,7 @@ 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}]",
+            "[{}, disabled: {}, locked: {}, count: {}, origin: {}, z: {:.2}, a: {:.2}]",
             *self.obj_type,
             self.flags.disabled() != 0,
             self.flags.aspect_ratio_unlocked() != 0,

+ 1 - 1
src/ezcad/objects/polygon.rs

@@ -28,7 +28,7 @@ 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: {}",
+            "{} corners: {}{}, offsets: ({:.2}, {:.2}, {:.2}, {:.2}), edges: {}, invert: {}",
             self.base,
             *self.corner_a,
             *self.corner_b,

+ 1 - 1
src/ezcad/objects/rectangle.rs

@@ -26,7 +26,7 @@ impl Display for Rectangle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(
             f,
-            "{} corners: {{{}, {}}}, rounding: ({:.2}, {:.2}, {:.2}, {:.2})",
+            "{} corners: {}{}, rounding: ({:.2}, {:.2}, {:.2}, {:.2})",
             self.base,
             *self.corner_a,
             *self.corner_b,

+ 12 - 20
tests/array_of.rs

@@ -8,10 +8,7 @@ fn array_of_be() {
     let encoded: Vec<u8> = vec![
         0x00, 0x00, 0x00, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
     ];
-    let decoded: ArrayOf<u32> = ArrayOf {
-        value: vec![0x0A0B0C0D, 0x01020304],
-    };
-
+    let decoded: ArrayOf<u32> = ArrayOf::from(vec![0x0A0B0C0D, 0x01020304]);
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
     assert_eq!(ArrayOf::<u32>::read_be(&mut stream).unwrap(), decoded);
 
@@ -25,10 +22,7 @@ fn array_of_le() {
     let encoded: Vec<u8> = vec![
         0x02, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
     ];
-    let decoded: ArrayOf<u32> = ArrayOf {
-        value: vec![0x0D0C0B0A, 0x04030201],
-    };
-
+    let decoded: ArrayOf<u32> = ArrayOf::from(vec![0x0D0C0B0A, 0x04030201]);
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
     assert_eq!(ArrayOf::<u32>::read_le(&mut stream).unwrap(), decoded);
 
@@ -48,18 +42,16 @@ fn array_of_struct() {
     let encoded: Vec<u8> = vec![
         0x00, 0x00, 0x00, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
     ];
-    let decoded: ArrayOf<Test> = ArrayOf {
-        value: vec![
-            Test {
-                a: 0x0A0B,
-                b: 0x0C0D,
-            },
-            Test {
-                a: 0x0102,
-                b: 0x0304,
-            },
-        ],
-    };
+    let decoded: ArrayOf<Test> = ArrayOf::from(vec![
+        Test {
+            a: 0x0A0B,
+            b: 0x0C0D,
+        },
+        Test {
+            a: 0x0102,
+            b: 0x0304,
+        },
+    ]);
 
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
     assert_eq!(ArrayOf::<Test>::read_be(&mut stream).unwrap(), decoded);