Skip to content

Commit

Permalink
Fix the stepdata messages
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <dzervas@dzervas.gr>
  • Loading branch information
dzervas committed May 28, 2024
1 parent 2c26b08 commit 6be3df4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 17 additions & 2 deletions packages/cadmium-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use quote::quote;
Expand All @@ -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;
Expand Down Expand Up @@ -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"))?;
};
Expand Down Expand Up @@ -107,6 +110,10 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream {
&& field.to_string() != id_arg_name.to_string()
).collect::<Vec<_>>();

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<crate::IDType, anyhow::Error> {
Expand All @@ -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<IDType, anyhow::Error> {
match self {
#( #actions )*
}
}
}
};

TokenStream::from(expanded)
Expand Down
14 changes: 11 additions & 3 deletions packages/cadmium/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -35,7 +36,10 @@ impl From<Result<String, anyhow::Error>> 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,
Expand Down Expand Up @@ -139,8 +143,12 @@ impl Message {

pub fn handle(&self, project: &mut Project) -> Result<String, anyhow::Error> {
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();
Expand Down

0 comments on commit 6be3df4

Please sign in to comment.