Browse Source

Initial config work

Kevin Lee 1 year ago
parent
commit
251e02a8b6

+ 75 - 0
Cargo.lock

@@ -182,6 +182,12 @@ dependencies = [
  "termcolor",
 ]
 
+[[package]]
+name = "equivalent"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
+
 [[package]]
 name = "errno"
 version = "0.3.8"
@@ -203,9 +209,17 @@ dependencies = [
  "log",
  "modular-bitfield",
  "num",
+ "serde",
+ "serde_yaml",
  "strum",
 ]
 
+[[package]]
+name = "hashbrown"
+version = "0.14.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
+
 [[package]]
 name = "heck"
 version = "0.4.1"
@@ -224,6 +238,16 @@ version = "2.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
 
+[[package]]
+name = "indexmap"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
+dependencies = [
+ "equivalent",
+ "hashbrown",
+]
+
 [[package]]
 name = "is-terminal"
 version = "0.4.9"
@@ -235,6 +259,12 @@ dependencies = [
  "windows-sys 0.48.0",
 ]
 
+[[package]]
+name = "itoa"
+version = "1.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
+
 [[package]]
 name = "libc"
 version = "0.2.151"
@@ -428,6 +458,45 @@ version = "1.0.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
 
+[[package]]
+name = "ryu"
+version = "1.0.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
+
+[[package]]
+name = "serde"
+version = "1.0.193"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.193"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.42",
+]
+
+[[package]]
+name = "serde_yaml"
+version = "0.9.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a15e0ef66bf939a7c890a0bf6d5a733c70202225f9888a89ed5c62298b019129"
+dependencies = [
+ "indexmap",
+ "itoa",
+ "ryu",
+ "serde",
+ "unsafe-libyaml",
+]
+
 [[package]]
 name = "static_assertions"
 version = "1.1.0"
@@ -499,6 +568,12 @@ version = "1.0.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
 
+[[package]]
+name = "unsafe-libyaml"
+version = "0.2.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
+
 [[package]]
 name = "utf8parse"
 version = "0.2.1"

+ 2 - 0
Cargo.toml

@@ -21,4 +21,6 @@ env_logger = "0.10.1"
 log = "0.4.20"
 modular-bitfield = "0.11.2"
 num = "0.4.1"
+serde = { version = "1.0.193", features = ["derive"] }
+serde_yaml = "0.9.29"
 strum = { version = "0.25.0", features = ["derive"] }

+ 3 - 0
config.yml

@@ -0,0 +1,3 @@
+- !Pen
+  id: 0
+  color: [255, 255, 255]

data/Circle.mlp → samples/Circle.mlp


data/Circle2.mlp → samples/Circle2.mlp


data/Circle3.mlp → samples/Circle3.mlp


data/Curve.mlp → samples/Curve.mlp


data/Ellipse.mlp → samples/Ellipse.mlp


data/Ellipse2.mlp → samples/Ellipse2.mlp


data/HatchCircle.mlp → samples/HatchCircle.mlp


data/HatchRectangle.mlp → samples/HatchRectangle.mlp


data/HatchRectangle2.mlp → samples/HatchRectangle2.mlp


data/Line.mlp → samples/Line.mlp


data/Multicuve.mlp → samples/Multicuve.mlp


data/Multicuve2.mlp → samples/Multicuve2.mlp


data/Multiline.mlp → samples/Multiline.mlp


data/Point.mlp → samples/Point.mlp


data/Polygon.mlp → samples/Polygon.mlp


data/Polygon2.mlp → samples/Polygon2.mlp


data/Polygon3.mlp → samples/Polygon3.mlp


data/Polygon4.mlp → samples/Polygon4.mlp


data/Rectangle.mlp → samples/Rectangle.mlp


+ 12 - 0
src/config.rs

@@ -0,0 +1,12 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct Pen {
+    pub id: u8,
+    pub color: Option<(u8, u8, u8)>,
+    pub disabled: Option<bool>,
+    pub loop_count: Option<u32>,
+    pub speed: Option<f64>,
+    pub power: Option<f64>,
+    pub frequency: Option<u32>,
+}

+ 22 - 10
src/main.rs

@@ -1,5 +1,6 @@
 use std::{
-    fs::{self},
+    fs::{self, read_to_string, File},
+    io::Read,
     path::PathBuf,
     time::Instant,
 };
@@ -10,6 +11,10 @@ use clap_verbosity_flag::{InfoLevel, Verbosity};
 use ezcad::file::Header;
 use log::{debug, info};
 
+use crate::config::Pen;
+
+mod config;
+
 #[derive(Debug, Parser)]
 struct Cli {
     /// Input file to parse
@@ -20,9 +25,10 @@ struct Cli {
     #[arg(short, long)]
     output: Option<PathBuf>,
 
-    // /// Configuration file
-    // #[arg(short, long)]
-    // config: PathBuf,
+    /// Configuration file
+    #[arg(short, long)]
+    config: Option<PathBuf>,
+
     #[command(flatten)]
     verbose: Verbosity<InfoLevel>,
 }
@@ -35,11 +41,10 @@ fn main() {
         .filter_level(cli.verbose.log_level_filter())
         .init();
 
-    let mut input = fs::File::open(cli.input).unwrap();
-    // let _config = read(args.config).unwrap();
+    let mut input: File = File::open(cli.input).unwrap();
 
-    let time = Instant::now();
-    let file = Header::read_le(&mut input).unwrap();
+    let time: Instant = Instant::now();
+    let file: Header = Header::read_le(&mut input).unwrap();
     info!("Decode time: {:?}", time.elapsed());
 
     for pen in file.pens_offset.data.pens.iter().take(1) {
@@ -52,9 +57,16 @@ fn main() {
         }
     }
 
+    if let Some(config) = cli.config {
+        info!("Processing config file {}", config.to_string_lossy());
+        let config: String = std::fs::read_to_string(config).unwrap();
+        let config: Vec<Pen> = serde_yaml::from_str(&config).unwrap();
+        debug!("{:#?}", config);
+    }
+
     if let Some(output) = cli.output {
-        let mut output = fs::File::create(output).unwrap();
-        let time = Instant::now();
+        let mut output = File::create(output).unwrap();
+        let time: Instant = Instant::now();
         output.write_le(&file).unwrap();
         info!("Encode time: {:?}", time.elapsed());
     }