From 6be3df413322c57c602fa1a1e88cd4268c24b28c Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Tue, 28 May 2024 11:15:19 +0300 Subject: [PATCH] Fix the stepdata messages Signed-off-by: Dimitris Zervas --- packages/cadmium-macros/src/lib.rs | 19 +++++++++++++++++-- packages/cadmium/src/message.rs | 14 +++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/cadmium-macros/src/lib.rs b/packages/cadmium-macros/src/lib.rs index 2237a87e..319901b3 100644 --- a/packages/cadmium-macros/src/lib.rs +++ b/packages/cadmium-macros/src/lib.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use convert_case::{Case, Casing}; use proc_macro::TokenStream; use quote::quote; @@ -14,6 +16,7 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream { syn::Data::Enum(data) => data, _ => panic!("StepData can only be derived for enums"), }; + let mut actions = vec![]; let variants = data.variants.iter().map(|variant| { let variant_name = &variant.ident; @@ -58,7 +61,7 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream { let mut wb_var = quote! {}; if !skip_workbench { wb_var = quote! { - let wb_ = self.native.workbenches + let wb_ = self.workbenches .get_mut(workbench_id as usize) .ok_or(anyhow::anyhow!("Could not find workbench"))?; }; @@ -107,6 +110,10 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream { && field.to_string() != id_arg_name.to_string() ).collect::>(); + actions.push(quote! { + #name::#variant_name { + #( #function_args_full ),* + } => project.#add_func_name(name, #(* #function_args_full ),* ) , }); quote! { pub fn #add_func_name(&mut self, name: String, #( #function_defs ),*) -> Result { @@ -133,9 +140,17 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream { }); let expanded = quote! { - impl crate::Project { + impl crate::project::Project { #( #variants )* } + + impl #name { + pub fn do_action(&self, project: &mut crate::project::Project, name: String) -> Result { + match self { + #( #actions )* + } + } + } }; TokenStream::from(expanded) diff --git a/packages/cadmium/src/message.rs b/packages/cadmium/src/message.rs index e24e18a0..57b5a8f6 100644 --- a/packages/cadmium/src/message.rs +++ b/packages/cadmium/src/message.rs @@ -8,6 +8,7 @@ use crate::error::CADmiumError; use crate::extrusion::{Direction, Extrusion, ExtrusionMode}; use crate::project::Project; use crate::step::StepData; +use crate::IDType; #[derive(Tsify, Debug, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] @@ -35,7 +36,10 @@ impl From> for MessageResult { #[derive(Tsify, Debug, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum Message { - StepAction(StepData), + StepAction { + name: String, + data: StepData, + }, RenameWorkbench { workbench_id: u64, new_name: String, @@ -139,8 +143,12 @@ impl Message { pub fn handle(&self, project: &mut Project) -> Result { match self { - Message::StepAction(data) => { - + Message::StepAction { + name, + data, + } => { + let id = data.do_action(project, *name)?; + Ok(format!("\"id\": \"{}\"", id)) } Message::RenameProject { new_name } => { project.name = new_name.to_owned();