Browse Source

Add bool type

Kevin Lee 1 year ago
parent
commit
4eb644ca2d
3 changed files with 42 additions and 14 deletions
  1. 3 3
      src/ezcad/pen.rs
  2. 21 1
      src/ezcad/types.rs
  3. 18 10
      src/main.rs

+ 3 - 3
src/ezcad/pen.rs

@@ -5,7 +5,7 @@ use diff::Diff;
 
 use crate::{
     field_of::FieldOf,
-    types::{Field, Rgba, WString, WobbleType, F64, U32},
+    types::{Field, Rgba, WString, WobbleType, F64, U32, Bool},
 };
 
 #[derive(BinRead, Debug)]
@@ -59,8 +59,8 @@ pub struct Pens {
 pub struct Pen {
     pub color: FieldOf<Rgba>,
     pub name: WString,
-    pub disabled: U32,
-    pub use_default: U32,
+    pub disabled: Bool,
+    pub use_default: Bool,
     pub loop_count: U32,
     pub speed: F64, // Changes with wobble relative speed
     pub power: F64,

+ 21 - 1
src/ezcad/types.rs

@@ -8,9 +8,29 @@ use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
 /// Generic field with structure of length + data
 pub type Field = ArrayOfPrimitive<u8>;
 
-pub type U32 = FieldOf<u32>;
 pub type U16 = FieldOf<u16>;
+pub type I16 = FieldOf<i16>;
+pub type U32 = FieldOf<u32>;
+pub type I32 = FieldOf<i32>;
 pub type F64 = FieldOf<f64>;
+pub type Bool = FieldOf<u32>;
+
+impl From<FieldOf<u32>> for bool {
+    fn from(value: FieldOf<u32>) -> Self {
+        value.value != 0
+    }
+}
+
+impl From<bool> for FieldOf<u32> {
+    fn from(value: bool) -> Self {
+        Self {
+            value: match value {
+                true => 1,
+                false => 0,
+            },
+        }
+    }
+}
 
 #[binrw]
 #[derive(Clone, Default, PartialEq)]

+ 18 - 10
src/main.rs

@@ -17,6 +17,10 @@ struct Cli {
     #[arg(short, long)]
     input: PathBuf,
 
+    /// File to diff input against
+    #[arg(short, long)]
+    diff: Option<PathBuf>,
+
     /// Output file to write to
     #[arg(short, long)]
     output: Option<PathBuf>,
@@ -41,17 +45,12 @@ fn main() {
     let mut input: File = File::open(cli.input).expect("Failed to open input file");
 
     let time: Instant = Instant::now();
-    let mut file: EzCadHeader =
+    let mut input_file: EzCadHeader =
         EzCadHeader::read_le(&mut input).expect("Failed to parse input file as EZCAD format");
     info!("Input decode time: {:?}", time.elapsed());
 
-    let pen_0 = file.pens_offset.data.pens.get(0).unwrap();
-    let pen_1 = file.pens_offset.data.pens.get(1).unwrap();
-    info!("{:#?}", pen_0.diff(pen_1));
-    // info!("{:?}", diff.color.value.);
-
     // Print info on pens with non-default settings
-    for pen in file
+    for pen in input_file
         .pens_offset
         .data
         .pens
@@ -62,26 +61,35 @@ fn main() {
     }
 
     // Print all objects
-    for layer in file.layers_offset.iter() {
+    for layer in input_file.layers_offset.iter() {
         for object in layer.objects.iter() {
             trace!("{:#?}", object);
         }
     }
 
+    if let Some(diff) = cli.diff {
+        info!("Processing diff file {}", diff.to_string_lossy());
+        let mut diff: File = File::open(diff).expect("Failed to open diff file");
+        let diff_file: EzCadHeader =
+            EzCadHeader::read_le(&mut diff).expect("Failed to parse diff file as EZCAD format");
+
+        info!("{:#?}", input_file.pens_offset.data.pens.diff(&diff_file.pens_offset.data.pens));
+    }
+
     if let Some(config) = cli.config {
         info!("Processing config file {}", config.to_string_lossy());
         let config: String = std::fs::read_to_string(config).expect("Failed to open config file");
         let config: Config = serde_yaml::from_str(&config).expect("Failed to parse config file");
 
         // Patch pen settings
-        config.ops.apply(&mut file.pens_offset.data.pens);
+        config.ops.apply(&mut input_file.pens_offset.data.pens);
     }
 
     if let Some(output) = cli.output {
         let mut output = File::create(output).expect("Failed to open output file");
         let time: Instant = Instant::now();
         output
-            .write_le(&file)
+            .write_le(&input_file)
             .expect("Failed to serialize contents in EZCAD format");
         info!("Output encode time: {:?}", time.elapsed());
     }