From 24696930b8b50edfa1c6f21c6c090b72d3aebb40 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Mon, 22 Jul 2024 16:05:50 +1000 Subject: [PATCH 01/12] work on ts v1 bindgen --- .../src/plugins/typescript/mod.rs | 94 ++++++++++++++++--- 1 file changed, 83 insertions(+), 11 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 2c0d13019c..115a4e0448 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -118,7 +118,6 @@ impl TypescriptPlugin { export interface {name} {{ {native_fields} }} - export const {name}Definition = {{ {fields} }}; @@ -165,10 +164,10 @@ type {} = ", result += format!( " -export const {name}Definition = {{ - type: RecsType.String,{} -}}; -", + export const {name}Definition = {{ + type: RecsType.String,{} + }}; + ", if !token.inners.is_empty() { "\n value: RecsType.String".to_string() } else { @@ -314,7 +313,20 @@ export function defineContractComponents(world: World) { // Formats a system into a C# method used by the contract class // Handled tokens should be a list of all structs and enums used by the contract // Such as a set of referenced tokens from a model - fn format_system(system: &Function, handled_tokens: &[Composite]) -> String { + fn format_system(system: &Function, handled_tokens: &[Composite], namespace: String) -> String { + if [ + "contract_name", + "namespace", + "tag", + "name_hash", + "selector", + "dojo_init", + "namespace_hash", + ] + .contains(&system.name.as_str()) + { + return String::new(); + } fn map_type(token: &Token) -> String { match token { Token::CoreBasic(_) => TypescriptPlugin::map_type(token) @@ -329,6 +341,18 @@ export function defineContractComponents(world: World) { let args = system .inputs .iter() + .filter(|arg| { + ![ + "contract_name", + "namespace", + "tag", + "name_hash", + "selector", + "dojo_init", + "namespace_hash", + ] + .contains(&arg.0.as_str()) + }) .map(|arg| format!("{}: {}", arg.0, map_type(&arg.1))) .collect::>() .join(", "); @@ -395,6 +419,18 @@ export function defineContractComponents(world: World) { let calldata = system .inputs .iter() + .filter(|arg| { + ![ + "contract_name", + "namespace", + "tag", + "name_hash", + "selector", + "dojo_init", + "namespace_hash", + ] + .contains(&arg.0.as_str()) + }) .map(|arg| handle_arg_recursive(&arg.0, &arg.1, handled_tokens)) .collect::>() .join(",\n "); @@ -408,7 +444,8 @@ export function defineContractComponents(world: World) { props.account, contract_name, \"{system_name}\", - [{calldata}] + [{calldata}], + \"{namespace}\" ); }} catch (error) {{ console.error(\"Error executing spawn:\", error); @@ -423,14 +460,19 @@ export function defineContractComponents(world: World) { // formatted args to use our mapped types args = args, // calldata for execute - calldata = calldata + calldata = calldata, + namespace = TypescriptPlugin::get_namespace_from_tag(&namespace) ) } // Formats a contract tag into a pretty contract name - // eg. dojo_examples-actions -> Actions + // eg. dojo_examples-actions -> actions fn formatted_contract_name(tag: &str) -> String { - naming::capitalize(&naming::get_name_from_tag(tag)) + naming::get_name_from_tag(tag) + } + + fn get_namespace_from_tag(tag: &str) -> String { + tag.split('-').next().unwrap_or(tag).to_string() } // Handles a contract definition and its underlying systems @@ -458,8 +500,25 @@ export function defineContractComponents(world: World) { let systems = contract .systems .iter() + .filter(|system| { + let name = system.to_function().unwrap().name.as_str(); + ![ + "contract_name", + "namespace", + "tag", + "name_hash", + "selector", + "dojo_init", + "namespace_hash", + ] + .contains(&name) + }) .map(|system| { - TypescriptPlugin::format_system(system.to_function().unwrap(), handled_tokens) + TypescriptPlugin::format_system( + system.to_function().unwrap(), + handled_tokens, + contract.tag.clone(), + ) }) .collect::>() .join("\n\n "); @@ -485,6 +544,19 @@ export function defineContractComponents(world: World) { contract .systems .iter() + .filter(|system| { + let name = system.to_function().unwrap().name.as_str(); + ![ + "contract_name", + "namespace", + "tag", + "name_hash", + "selector", + "dojo_init", + "namespace_hash", + ] + .contains(&name) + }) .map(|system| { system.to_function().unwrap().name.to_string() }) .collect::>() .join(", ") From c5fd99f5ecf56bf727c3b153c82d32bd23630ec9 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Mon, 22 Jul 2024 17:01:36 +1000 Subject: [PATCH 02/12] work on model def --- crates/dojo-bindgen/src/plugins/typescript/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 115a4e0448..893ca9e7b3 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -133,12 +133,22 @@ export const {name}Definition = {{ // This will be formatted into a C# enum // Enum is mapped using index of cairo enum fn format_enum(token: &Composite) -> String { + if token.type_path == "core::option::Option::" + || token.type_path == "core::option::Option::" + || token.type_path == "core::option::Option::" + || token.type_path == "core::option::Option::" + || token.type_path == "core::option::Option::" + || token.type_path == "core::option::Option::" + { + return String::new(); // Return an empty string for these enums + } + let name = TypescriptPlugin::map_type(&Token::Composite(token.clone())); let mut result = format!( " // Type definition for `{}` enum -type {} = ", +export type {} = ", token.type_path, name ); From 69fd5cc223df706eb7bdbdbd46ebaf57aeadfcf8 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 23 Jul 2024 09:11:50 +1000 Subject: [PATCH 03/12] formatting --- .../src/plugins/typescript/mod.rs | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 893ca9e7b3..a68d90475b 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -82,7 +82,11 @@ impl TypescriptPlugin { fn generated_header() -> String { format!( - "// Generated by dojo-bindgen on {}. Do not modify this file manually.\n", + " +// Generated by dojo-bindgen on {}. Do not modify this file manually. +// Import the necessary types from the recs SDK +// generate again with `sozo build --typescript` +", chrono::Utc::now().to_rfc2822() ) } @@ -174,9 +178,9 @@ export type {} = ", result += format!( " - export const {name}Definition = {{ - type: RecsType.String,{} - }}; +export const {name}Definition = {{ + type: RecsType.String,{} +}}; ", if !token.inners.is_empty() { "\n value: RecsType.String".to_string() @@ -246,9 +250,8 @@ export type {} = ", out += TypescriptPlugin::generated_header().as_str(); out += "import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";\n"; out += "\n"; - out += "export type ContractComponents = Awaited< - ReturnType - >;\n"; + out += "export type ContractComponents = Awaited>;\n"; out += "\n\n"; @@ -277,6 +280,14 @@ export type {} = ", } }); + for token in &tokens.enums { + if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 + { + continue; + } + out += TypescriptPlugin::format_enum(token.to_composite().unwrap()).as_str(); + } + for token in &structs { if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 { @@ -294,14 +305,6 @@ export type {} = ", out += TypescriptPlugin::format_struct(token.to_composite().unwrap()).as_str(); } - for token in &tokens.enums { - if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 - { - continue; - } - out += TypescriptPlugin::format_enum(token.to_composite().unwrap()).as_str(); - } - out += "\n"; } From fe5731e9a96f27dc7154dc8eebed6ad54ab5bc7c Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 23 Jul 2024 12:49:27 +1000 Subject: [PATCH 04/12] improve codegen --- crates/dojo-bindgen/src/plugins/typescript/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index a68d90475b..b384caeabf 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -137,6 +137,8 @@ export const {name}Definition = {{ // This will be formatted into a C# enum // Enum is mapped using index of cairo enum fn format_enum(token: &Composite) -> String { + // filter out common types + // TODO: Make cleaner if token.type_path == "core::option::Option::" || token.type_path == "core::option::Option::" || token.type_path == "core::option::Option::" From f89fee002b1ccc0d232d83bd5eba5a95ad884393 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 23 Jul 2024 15:37:56 +1000 Subject: [PATCH 05/12] add tests --- Cargo.lock | 2 + crates/dojo-bindgen/Cargo.toml | 2 + .../src/plugins/typescript/mod.rs | 27 ++- .../src/plugins/typescript/tests.rs | 180 ++++++++++++++++++ 4 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 crates/dojo-bindgen/src/plugins/typescript/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 182fbf4488..7a0d427075 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4206,6 +4206,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" name = "dojo-bindgen" version = "1.0.0-alpha.2" dependencies = [ + "assert_matches", "async-trait", "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", "camino", @@ -4218,6 +4219,7 @@ dependencies = [ "serde_json", "starknet 0.11.0", "thiserror", + "tokio", ] [[package]] diff --git a/crates/dojo-bindgen/Cargo.toml b/crates/dojo-bindgen/Cargo.toml index 8a97d8c2c2..8498eb10a8 100644 --- a/crates/dojo-bindgen/Cargo.toml +++ b/crates/dojo-bindgen/Cargo.toml @@ -7,6 +7,7 @@ repository.workspace = true version.workspace = true [dependencies] +assert_matches.workspace = true async-trait.workspace = true camino.workspace = true chrono.workspace = true @@ -15,6 +16,7 @@ serde.workspace = true serde_json.workspace = true starknet.workspace = true thiserror.workspace = true +tokio.workspace = true # Some issue with CI on windows, need to be investigated. # https://github.com/dojoengine/dojo/actions/runs/7736050751/job/21092743552?pr=1501#step:6:249 diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index b384caeabf..bab246e312 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -9,6 +9,8 @@ use crate::error::BindgenResult; use crate::plugins::BuiltinPlugin; use crate::{DojoContract, DojoData, DojoModel}; +mod tests; + pub struct TypescriptPlugin {} impl TypescriptPlugin { @@ -201,21 +203,26 @@ export const {name}Definition = {{ fn format_model(namespace: &str, model: &Composite) -> String { let mut custom_types = Vec::::new(); let mut types = Vec::::new(); - let fields = model - .inners - .iter() - .map(|field| { + let (fields, _composite_type) = + model.inners.iter().fold((Vec::new(), model.r#type), |(mut fields, _), field| { let mapped = TypescriptPlugin::map_type(&field.token); - if mapped == field.token.type_name() { + let field_str = if model.r#type == CompositeType::Enum { + // For enums, use RecsType.String + types.push(format!("\"{}\"", field.token.type_name())); + format!("{}: RecsType.String,", field.name) + } else if mapped == field.token.type_name() { custom_types.push(format!("\"{}\"", field.token.type_name())); format!("{}: {}Definition,", field.name, mapped) } else { types.push(format!("\"{}\"", field.token.type_name())); format!("{}: {},", field.name, mapped) - } - }) - .collect::>() - .join("\n "); + }; + + fields.push(field_str); + (fields, model.r#type) + }); + + let fields_str = fields.join("\n "); format!( " @@ -224,7 +231,7 @@ export const {name}Definition = {{ return defineComponent( world, {{ - {fields} + {fields_str} }}, {{ metadata: {{ diff --git a/crates/dojo-bindgen/src/plugins/typescript/tests.rs b/crates/dojo-bindgen/src/plugins/typescript/tests.rs new file mode 100644 index 0000000000..0de89793f3 --- /dev/null +++ b/crates/dojo-bindgen/src/plugins/typescript/tests.rs @@ -0,0 +1,180 @@ +use std::collections::HashMap; + +use assert_matches::assert_matches; +use cainome::parser::tokens::{ + Composite, CompositeInner, CompositeInnerKind, CompositeType, Token, +}; + +use super::*; +use crate::{DojoData, DojoWorld}; + +#[tokio::test] +async fn test_typescript_plugin_generate_code() { + let plugin = TypescriptPlugin::new(); + let data = create_mock_dojo_data(); + + let result = plugin.generate_code(&data).await; + + assert_matches!(result, Ok(output) => { + assert_eq!(output.len(), 2); + assert!(output.contains_key(Path::new("models.gen.ts"))); + assert!(output.contains_key(Path::new("contracts.gen.ts"))); + + // Check content of models.gen.ts + let models_content = String::from_utf8_lossy(&output[Path::new("models.gen.ts")]); + assert!(models_content.contains("import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";")); + assert!(models_content.contains("export type ContractComponents = Awaited>;")); + + // Check content of contracts.gen.ts + let contracts_content = String::from_utf8_lossy(&output[Path::new("contracts.gen.ts")]); + assert!(contracts_content.contains("import { Account, byteArray } from \"starknet\";")); + assert!(contracts_content.contains("import { DojoProvider } from \"@dojoengine/core\";")); + assert!(contracts_content.contains("export type IWorld = Awaited>;")); + }); +} + +#[test] +fn test_map_type() { + let bool_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "bool".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&bool_token), "RecsType.Boolean"); + + let u32_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "u32".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&u32_token), "RecsType.Number"); +} + +#[test] +fn test_formatted_contract_name() { + assert_eq!(TypescriptPlugin::formatted_contract_name("dojo_examples-actions"), "actions"); + assert_eq!(TypescriptPlugin::formatted_contract_name("my-contract"), "contract"); +} + +#[test] +fn test_get_namespace_from_tag() { + assert_eq!(TypescriptPlugin::get_namespace_from_tag("dojo_examples-actions"), "dojo_examples"); + assert_eq!(TypescriptPlugin::get_namespace_from_tag("my-contract"), "my"); +} + +#[test] +fn test_format_model() { + // Create a mock Composite representing a model + let model = Composite { + type_path: "game::models::Position".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![ + CompositeInner { + index: 0, + name: "x".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 1, + name: "y".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 2, + name: "player".to_string(), + kind: CompositeInnerKind::Data, + token: Token::Composite(Composite { + type_path: "game::models::Player".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![], + is_event: false, + alias: None, + }), + }, + ], + is_event: false, + alias: None, + }; + + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); + + let expected = r#" + // Model definition for `game::models::Position` model + Position: (() => { + return defineComponent( + world, + { + x: RecsType.Number, + y: RecsType.Number, + player: PlayerDefinition, + }, + { + metadata: { + namespace: "game", + name: "Position", + types: ["u32", "u32"], + customTypes: ["Player"], + }, + } + ); + })(), +"#; + + assert_eq!(formatted.trim(), expected.trim()); +} + +#[test] +fn test_format_enum_model() { + // Create a mock Composite representing an enum model + let model = Composite { + type_path: "game::models::Direction".to_string(), + r#type: CompositeType::Enum, + generic_args: vec![], + inners: vec![CompositeInner { + index: 0, + name: "North".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "()".to_string(), + }), + }], + is_event: false, + alias: None, + }; + + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); + + let expected = r#" + // Model definition for `game::models::Direction` model + Direction: (() => { + return defineComponent( + world, + { + North: RecsType.String, + }, + { + metadata: { + namespace: "game", + name: "Direction", + types: ["()"], + customTypes: [], + }, + } + ); + })(), +"#; + + assert_eq!(formatted.trim(), expected.trim()); +} +// // Helper function to create mock DojoData for testing +fn create_mock_dojo_data() -> DojoData { + DojoData { + world: DojoWorld { name: 0x01.to_string() }, + models: HashMap::new(), + contracts: HashMap::new(), + } +} From b00100a77ca8ee2795328fa1bba4b31138210859 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Wed, 24 Jul 2024 10:33:40 +1000 Subject: [PATCH 06/12] update enum to string --- Cargo.lock | 46 +++++++++---------- Cargo.toml | 6 +-- .../src/plugins/typescript/mod.rs | 29 ++++++++---- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a0d427075..81fd778b42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2082,14 +2082,14 @@ checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0" [[package]] name = "cainome" version = "0.2.3" -source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.0#6c82c5b8e8169a79fe5606f963a3afdf0aa37078" +source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" dependencies = [ "anyhow", "async-trait", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-rs-macro 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-rs-macro 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "camino", "clap", "clap_complete", @@ -2132,7 +2132,7 @@ dependencies = [ [[package]] name = "cainome-cairo-serde" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.0#6c82c5b8e8169a79fe5606f963a3afdf0aa37078" +source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" dependencies = [ "serde", "starknet 0.11.0", @@ -2152,7 +2152,7 @@ dependencies = [ [[package]] name = "cainome-parser" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.0#6c82c5b8e8169a79fe5606f963a3afdf0aa37078" +source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" dependencies = [ "convert_case 0.6.0", "quote", @@ -2178,11 +2178,11 @@ dependencies = [ [[package]] name = "cainome-rs" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.0#6c82c5b8e8169a79fe5606f963a3afdf0aa37078" +source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" dependencies = [ "anyhow", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "camino", "prettyplease 0.2.20", "proc-macro2", @@ -2214,12 +2214,12 @@ dependencies = [ [[package]] name = "cainome-rs-macro" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.0#6c82c5b8e8169a79fe5606f963a3afdf0aa37078" +source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" dependencies = [ "anyhow", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", - "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "proc-macro2", "quote", "serde_json", @@ -4208,7 +4208,7 @@ version = "1.0.0-alpha.2" dependencies = [ "assert_matches", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "camino", "chrono", "convert_case 0.6.0", @@ -4236,7 +4236,7 @@ version = "1.0.0-alpha.2" dependencies = [ "anyhow", "assert_fs", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "cairo-lang-compiler", "cairo-lang-debug", "cairo-lang-defs", @@ -4351,7 +4351,7 @@ dependencies = [ name = "dojo-types" version = "1.0.0-alpha.2" dependencies = [ - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "crypto-bigint", "hex", "itertools 0.12.1", @@ -4372,7 +4372,7 @@ dependencies = [ "assert_fs", "assert_matches", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "cairo-lang-filesystem", "cairo-lang-project", "cairo-lang-starknet", @@ -7737,7 +7737,7 @@ dependencies = [ "alloy", "anyhow", "assert_matches", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "dojo-metrics", "dojo-test-utils", "dojo-world", @@ -12486,7 +12486,7 @@ dependencies = [ "assert_fs", "async-trait", "bigdecimal 0.4.5", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-filesystem", @@ -12546,7 +12546,7 @@ dependencies = [ "assert_fs", "async-trait", "bigdecimal 0.4.5", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-filesystem", @@ -14152,7 +14152,7 @@ dependencies = [ "anyhow", "async-trait", "base64 0.21.7", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "camino", "chrono", "crypto-bigint", @@ -14277,7 +14277,7 @@ version = "1.0.0-alpha.2" dependencies = [ "anyhow", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.0)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", "chrono", "crypto-bigint", "dojo-test-utils", diff --git a/Cargo.toml b/Cargo.toml index 6d438f0191..a73cc7ca79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,7 @@ inherits = "release" lto = "fat" [workspace.dependencies] -cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.3.0", features = [ "abigen-rs" ] } +cainome = { git = "https://github.com/cartridge-gg/cainome", branch = "fix/inner-composite-resolution", features = [ "abigen-rs" ] } common = { path = "crates/common" } # metrics @@ -231,9 +231,7 @@ slot = { git = "https://github.com/cartridge-gg/slot", rev = "4c1165d" } alloy-contract = { version = "0.2", default-features = false } alloy-json-rpc = { version = "0.2", default-features = false } alloy-network = { version = "0.2", default-features = false } -alloy-provider = { version = "0.2", default-features = false, features = [ - "reqwest", -] } +alloy-provider = { version = "0.2", default-features = false, features = [ "reqwest" ] } alloy-rpc-types-eth = { version = "0.2", default-features = false } alloy-signer = { version = "0.2", default-features = false } alloy-transport = { version = "0.2", default-features = false } diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index bab246e312..33cb8d475d 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -206,16 +206,24 @@ export const {name}Definition = {{ let (fields, _composite_type) = model.inners.iter().fold((Vec::new(), model.r#type), |(mut fields, _), field| { let mapped = TypescriptPlugin::map_type(&field.token); - let field_str = if model.r#type == CompositeType::Enum { - // For enums, use RecsType.String - types.push(format!("\"{}\"", field.token.type_name())); - format!("{}: RecsType.String,", field.name) - } else if mapped == field.token.type_name() { - custom_types.push(format!("\"{}\"", field.token.type_name())); - format!("{}: {}Definition,", field.name, mapped) - } else { - types.push(format!("\"{}\"", field.token.type_name())); - format!("{}: {},", field.name, mapped) + + let field_str = match field.token { + Token::Composite(ref c) if c.r#type == CompositeType::Enum => { + types.push(format!("\"{}\"", field.token.type_name())); + format!("{}: RecsType.String,", field.name) + } + Token::Composite(_) => { + custom_types.push(format!("\"{}\"", field.token.type_name())); + format!("{}: {}Definition,", field.name, mapped) + } + _ if mapped == field.token.type_name() => { + custom_types.push(format!("\"{}\"", field.token.type_name())); + format!("{}: {}Definition,", field.name, mapped) + } + _ => { + types.push(format!("\"{}\"", field.token.type_name())); + format!("{}: {},", field.name, mapped) + } }; fields.push(field_str); @@ -356,6 +364,7 @@ export function defineContractComponents(world: World) { // types should be lowercased .to_lowercase(), Token::Composite(t) => format!("models.{}", t.type_name()), + Token::Array(t) => TypescriptPlugin::map_type(token), _ => panic!("Unsupported token type: {:?}", token), } } From ec106d9ee3ed84943600343f996b2230d8cce9f2 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Wed, 24 Jul 2024 13:31:34 +1000 Subject: [PATCH 07/12] fix mapping in systems gen --- crates/dojo-bindgen/src/plugins/typescript/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 33cb8d475d..67c4125995 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -425,11 +425,7 @@ export function defineContractComponents(world: World) { None => format!("props.{}", arg_name), } } - Token::Array(t) => format!( - "...props.{}.map(item => {}) ", - arg_name, - handle_arg_recursive("item", &t.inner, handled_tokens) - ), + Token::Array(_) => format!("...props.{}", arg_name), Token::Tuple(t) => format!( "...[{}]", t.inners From 37b2ded26a358bc8793caac382a981dd39960db4 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Wed, 24 Jul 2024 13:35:20 +1000 Subject: [PATCH 08/12] remove unneeded filters --- .../src/plugins/typescript/mod.rs | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 67c4125995..7dbf466b6c 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -372,18 +372,6 @@ export function defineContractComponents(world: World) { let args = system .inputs .iter() - .filter(|arg| { - ![ - "contract_name", - "namespace", - "tag", - "name_hash", - "selector", - "dojo_init", - "namespace_hash", - ] - .contains(&arg.0.as_str()) - }) .map(|arg| format!("{}: {}", arg.0, map_type(&arg.1))) .collect::>() .join(", "); @@ -446,18 +434,6 @@ export function defineContractComponents(world: World) { let calldata = system .inputs .iter() - .filter(|arg| { - ![ - "contract_name", - "namespace", - "tag", - "name_hash", - "selector", - "dojo_init", - "namespace_hash", - ] - .contains(&arg.0.as_str()) - }) .map(|arg| handle_arg_recursive(&arg.0, &arg.1, handled_tokens)) .collect::>() .join(",\n "); From 96661037f642c5934c1f0593da5c664594c01c17 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Wed, 24 Jul 2024 13:48:26 +1000 Subject: [PATCH 09/12] fix imports and errors --- .../src/plugins/typescript/mod.rs | 2 +- .../src/plugins/typescript/tests.rs | 262 +++++++++--------- 2 files changed, 135 insertions(+), 129 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 7dbf466b6c..40d61a9096 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -364,7 +364,7 @@ export function defineContractComponents(world: World) { // types should be lowercased .to_lowercase(), Token::Composite(t) => format!("models.{}", t.type_name()), - Token::Array(t) => TypescriptPlugin::map_type(token), + Token::Array(_) => TypescriptPlugin::map_type(token), _ => panic!("Unsupported token type: {:?}", token), } } diff --git a/crates/dojo-bindgen/src/plugins/typescript/tests.rs b/crates/dojo-bindgen/src/plugins/typescript/tests.rs index 0de89793f3..d99fe439d7 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/tests.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/tests.rs @@ -1,107 +1,112 @@ -use std::collections::HashMap; - -use assert_matches::assert_matches; -use cainome::parser::tokens::{ - Composite, CompositeInner, CompositeInnerKind, CompositeType, Token, -}; - -use super::*; -use crate::{DojoData, DojoWorld}; - -#[tokio::test] -async fn test_typescript_plugin_generate_code() { - let plugin = TypescriptPlugin::new(); - let data = create_mock_dojo_data(); - - let result = plugin.generate_code(&data).await; - - assert_matches!(result, Ok(output) => { - assert_eq!(output.len(), 2); - assert!(output.contains_key(Path::new("models.gen.ts"))); - assert!(output.contains_key(Path::new("contracts.gen.ts"))); - - // Check content of models.gen.ts - let models_content = String::from_utf8_lossy(&output[Path::new("models.gen.ts")]); - assert!(models_content.contains("import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";")); - assert!(models_content.contains("export type ContractComponents = Awaited>;")); - - // Check content of contracts.gen.ts - let contracts_content = String::from_utf8_lossy(&output[Path::new("contracts.gen.ts")]); - assert!(contracts_content.contains("import { Account, byteArray } from \"starknet\";")); - assert!(contracts_content.contains("import { DojoProvider } from \"@dojoengine/core\";")); - assert!(contracts_content.contains("export type IWorld = Awaited>;")); - }); -} +#[cfg(test)] +mod tests { + use std::collections::HashMap; -#[test] -fn test_map_type() { - let bool_token = - Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "bool".to_string() }); - assert_eq!(TypescriptPlugin::map_type(&bool_token), "RecsType.Boolean"); + use assert_matches::assert_matches; + use cainome::parser::tokens::{ + Composite, CompositeInner, CompositeInnerKind, CompositeType, Token, + }; - let u32_token = - Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "u32".to_string() }); - assert_eq!(TypescriptPlugin::map_type(&u32_token), "RecsType.Number"); -} + use super::*; + use crate::{DojoData, DojoWorld}; -#[test] -fn test_formatted_contract_name() { - assert_eq!(TypescriptPlugin::formatted_contract_name("dojo_examples-actions"), "actions"); - assert_eq!(TypescriptPlugin::formatted_contract_name("my-contract"), "contract"); -} + #[tokio::test] + async fn test_typescript_plugin_generate_code() { + let plugin = TypescriptPlugin::new(); + let data = create_mock_dojo_data(); -#[test] -fn test_get_namespace_from_tag() { - assert_eq!(TypescriptPlugin::get_namespace_from_tag("dojo_examples-actions"), "dojo_examples"); - assert_eq!(TypescriptPlugin::get_namespace_from_tag("my-contract"), "my"); -} + let result = plugin.generate_code(&data).await; -#[test] -fn test_format_model() { - // Create a mock Composite representing a model - let model = Composite { - type_path: "game::models::Position".to_string(), - r#type: CompositeType::Struct, - generic_args: vec![], - inners: vec![ - CompositeInner { - index: 0, - name: "x".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "u32".to_string(), - }), - }, - CompositeInner { - index: 1, - name: "y".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "u32".to_string(), - }), - }, - CompositeInner { - index: 2, - name: "player".to_string(), - kind: CompositeInnerKind::Data, - token: Token::Composite(Composite { - type_path: "game::models::Player".to_string(), - r#type: CompositeType::Struct, - generic_args: vec![], - inners: vec![], - is_event: false, - alias: None, - }), - }, - ], - is_event: false, - alias: None, - }; + assert_matches!(result, Ok(output) => { + assert_eq!(output.len(), 2); + assert!(output.contains_key(Path::new("models.gen.ts"))); + assert!(output.contains_key(Path::new("contracts.gen.ts"))); + + // Check content of models.gen.ts + let models_content = String::from_utf8_lossy(&output[Path::new("models.gen.ts")]); + assert!(models_content.contains("import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";")); + assert!(models_content.contains("export type ContractComponents = Awaited>;")); + + // Check content of contracts.gen.ts + let contracts_content = String::from_utf8_lossy(&output[Path::new("contracts.gen.ts")]); + assert!(contracts_content.contains("import { Account, byteArray } from \"starknet\";")); + assert!(contracts_content.contains("import { DojoProvider } from \"@dojoengine/core\";")); + assert!(contracts_content.contains("export type IWorld = Awaited>;")); + }); + } + + #[test] + fn test_map_type() { + let bool_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "bool".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&bool_token), "RecsType.Boolean"); - let namespace = "game"; - let formatted = TypescriptPlugin::format_model(namespace, &model); + let u32_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "u32".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&u32_token), "RecsType.Number"); + } + + #[test] + fn test_formatted_contract_name() { + assert_eq!(TypescriptPlugin::formatted_contract_name("dojo_examples-actions"), "actions"); + assert_eq!(TypescriptPlugin::formatted_contract_name("my-contract"), "contract"); + } + + #[test] + fn test_get_namespace_from_tag() { + assert_eq!( + TypescriptPlugin::get_namespace_from_tag("dojo_examples-actions"), + "dojo_examples" + ); + assert_eq!(TypescriptPlugin::get_namespace_from_tag("my-contract"), "my"); + } + + #[test] + fn test_format_model() { + // Create a mock Composite representing a model + let model = Composite { + type_path: "game::models::Position".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![ + CompositeInner { + index: 0, + name: "x".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 1, + name: "y".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 2, + name: "player".to_string(), + kind: CompositeInnerKind::Data, + token: Token::Composite(Composite { + type_path: "game::models::Player".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![], + is_event: false, + alias: None, + }), + }, + ], + is_event: false, + alias: None, + }; + + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); - let expected = r#" + let expected = r#" // Model definition for `game::models::Position` model Position: (() => { return defineComponent( @@ -123,32 +128,32 @@ fn test_format_model() { })(), "#; - assert_eq!(formatted.trim(), expected.trim()); -} + assert_eq!(formatted.trim(), expected.trim()); + } -#[test] -fn test_format_enum_model() { - // Create a mock Composite representing an enum model - let model = Composite { - type_path: "game::models::Direction".to_string(), - r#type: CompositeType::Enum, - generic_args: vec![], - inners: vec![CompositeInner { - index: 0, - name: "North".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "()".to_string(), - }), - }], - is_event: false, - alias: None, - }; + #[test] + fn test_format_enum_model() { + // Create a mock Composite representing an enum model + let model = Composite { + type_path: "game::models::Direction".to_string(), + r#type: CompositeType::Enum, + generic_args: vec![], + inners: vec![CompositeInner { + index: 0, + name: "North".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "()".to_string(), + }), + }], + is_event: false, + alias: None, + }; - let namespace = "game"; - let formatted = TypescriptPlugin::format_model(namespace, &model); + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); - let expected = r#" + let expected = r#" // Model definition for `game::models::Direction` model Direction: (() => { return defineComponent( @@ -168,13 +173,14 @@ fn test_format_enum_model() { })(), "#; - assert_eq!(formatted.trim(), expected.trim()); -} -// // Helper function to create mock DojoData for testing -fn create_mock_dojo_data() -> DojoData { - DojoData { - world: DojoWorld { name: 0x01.to_string() }, - models: HashMap::new(), - contracts: HashMap::new(), + assert_eq!(formatted.trim(), expected.trim()); + } + // // Helper function to create mock DojoData for testing + fn create_mock_dojo_data() -> DojoData { + DojoData { + world: DojoWorld { name: 0x01.to_string() }, + models: HashMap::new(), + contracts: HashMap::new(), + } } } From 6023603beb365d93519f7b1aeef7857de585e3cc Mon Sep 17 00:00:00 2001 From: glihm Date: Wed, 24 Jul 2024 21:32:21 -0600 Subject: [PATCH 10/12] fix: fix tests and fmt --- .../src/plugins/typescript/mod.rs | 1 + .../src/plugins/typescript/tests.rs | 339 +++++++++--------- 2 files changed, 169 insertions(+), 171 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript/mod.rs b/crates/dojo-bindgen/src/plugins/typescript/mod.rs index 40d61a9096..7750d2d1f0 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/mod.rs @@ -9,6 +9,7 @@ use crate::error::BindgenResult; use crate::plugins::BuiltinPlugin; use crate::{DojoContract, DojoData, DojoModel}; +#[cfg(test)] mod tests; pub struct TypescriptPlugin {} diff --git a/crates/dojo-bindgen/src/plugins/typescript/tests.rs b/crates/dojo-bindgen/src/plugins/typescript/tests.rs index d99fe439d7..078d9813ac 100644 --- a/crates/dojo-bindgen/src/plugins/typescript/tests.rs +++ b/crates/dojo-bindgen/src/plugins/typescript/tests.rs @@ -1,186 +1,183 @@ -#[cfg(test)] -mod tests { - use std::collections::HashMap; - - use assert_matches::assert_matches; - use cainome::parser::tokens::{ - Composite, CompositeInner, CompositeInnerKind, CompositeType, Token, - }; - - use super::*; - use crate::{DojoData, DojoWorld}; - - #[tokio::test] - async fn test_typescript_plugin_generate_code() { - let plugin = TypescriptPlugin::new(); - let data = create_mock_dojo_data(); - - let result = plugin.generate_code(&data).await; - - assert_matches!(result, Ok(output) => { - assert_eq!(output.len(), 2); - assert!(output.contains_key(Path::new("models.gen.ts"))); - assert!(output.contains_key(Path::new("contracts.gen.ts"))); - - // Check content of models.gen.ts - let models_content = String::from_utf8_lossy(&output[Path::new("models.gen.ts")]); - assert!(models_content.contains("import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";")); - assert!(models_content.contains("export type ContractComponents = Awaited>;")); +use std::collections::HashMap; +use std::path::Path; + +use assert_matches::assert_matches; +use cainome::parser::tokens::{ + Composite, CompositeInner, CompositeInnerKind, CompositeType, Token, +}; + +use crate::plugins::typescript::TypescriptPlugin; +use crate::{BuiltinPlugin, DojoData, DojoWorld}; + +#[tokio::test] +async fn test_typescript_plugin_generate_code() { + let plugin = TypescriptPlugin::new(); + let data = create_mock_dojo_data(); + + let result = plugin.generate_code(&data).await; + + assert_matches!(result, Ok(output) => { + assert_eq!(output.len(), 2); + assert!(output.contains_key(Path::new("models.gen.ts"))); + assert!(output.contains_key(Path::new("contracts.gen.ts"))); + + // Check content of models.gen.ts + let models_content = String::from_utf8_lossy(&output[Path::new("models.gen.ts")]); + assert!(models_content.contains("import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";")); + assert!(models_content.contains("export type ContractComponents = Awaited>;")); + + // Check content of contracts.gen.ts + let contracts_content = String::from_utf8_lossy(&output[Path::new("contracts.gen.ts")]); + assert!(contracts_content.contains("import { Account, byteArray } from \"starknet\";")); + assert!(contracts_content.contains("import { DojoProvider } from \"@dojoengine/core\";")); + assert!(contracts_content.contains("export type IWorld = Awaited>;")); + }); +} - // Check content of contracts.gen.ts - let contracts_content = String::from_utf8_lossy(&output[Path::new("contracts.gen.ts")]); - assert!(contracts_content.contains("import { Account, byteArray } from \"starknet\";")); - assert!(contracts_content.contains("import { DojoProvider } from \"@dojoengine/core\";")); - assert!(contracts_content.contains("export type IWorld = Awaited>;")); - }); - } +#[test] +fn test_map_type() { + let bool_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "bool".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&bool_token), "RecsType.Boolean"); - #[test] - fn test_map_type() { - let bool_token = - Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "bool".to_string() }); - assert_eq!(TypescriptPlugin::map_type(&bool_token), "RecsType.Boolean"); + let u32_token = + Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "u32".to_string() }); + assert_eq!(TypescriptPlugin::map_type(&u32_token), "RecsType.Number"); +} - let u32_token = - Token::CoreBasic(cainome::parser::tokens::CoreBasic { type_path: "u32".to_string() }); - assert_eq!(TypescriptPlugin::map_type(&u32_token), "RecsType.Number"); - } +#[test] +fn test_formatted_contract_name() { + assert_eq!(TypescriptPlugin::formatted_contract_name("dojo_examples-actions"), "actions"); + assert_eq!(TypescriptPlugin::formatted_contract_name("my-contract"), "contract"); +} - #[test] - fn test_formatted_contract_name() { - assert_eq!(TypescriptPlugin::formatted_contract_name("dojo_examples-actions"), "actions"); - assert_eq!(TypescriptPlugin::formatted_contract_name("my-contract"), "contract"); - } +#[test] +fn test_get_namespace_from_tag() { + assert_eq!(TypescriptPlugin::get_namespace_from_tag("dojo_examples-actions"), "dojo_examples"); + assert_eq!(TypescriptPlugin::get_namespace_from_tag("my-contract"), "my"); +} - #[test] - fn test_get_namespace_from_tag() { - assert_eq!( - TypescriptPlugin::get_namespace_from_tag("dojo_examples-actions"), - "dojo_examples" - ); - assert_eq!(TypescriptPlugin::get_namespace_from_tag("my-contract"), "my"); - } +#[test] +fn test_format_model() { + // Create a mock Composite representing a model + let model = Composite { + type_path: "game::models::Position".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![ + CompositeInner { + index: 0, + name: "x".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 1, + name: "y".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "u32".to_string(), + }), + }, + CompositeInner { + index: 2, + name: "player".to_string(), + kind: CompositeInnerKind::Data, + token: Token::Composite(Composite { + type_path: "game::models::Player".to_string(), + r#type: CompositeType::Struct, + generic_args: vec![], + inners: vec![], + is_event: false, + alias: None, + }), + }, + ], + is_event: false, + alias: None, + }; - #[test] - fn test_format_model() { - // Create a mock Composite representing a model - let model = Composite { - type_path: "game::models::Position".to_string(), - r#type: CompositeType::Struct, - generic_args: vec![], - inners: vec![ - CompositeInner { - index: 0, - name: "x".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "u32".to_string(), - }), - }, - CompositeInner { - index: 1, - name: "y".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "u32".to_string(), - }), + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); + + let expected = r#" + // Model definition for `game::models::Position` model + Position: (() => { + return defineComponent( + world, + { + x: RecsType.Number, + y: RecsType.Number, + player: PlayerDefinition, + }, + { + metadata: { + namespace: "game", + name: "Position", + types: ["u32", "u32"], + customTypes: ["Player"], }, - CompositeInner { - index: 2, - name: "player".to_string(), - kind: CompositeInnerKind::Data, - token: Token::Composite(Composite { - type_path: "game::models::Player".to_string(), - r#type: CompositeType::Struct, - generic_args: vec![], - inners: vec![], - is_event: false, - alias: None, - }), - }, - ], - is_event: false, - alias: None, - }; - - let namespace = "game"; - let formatted = TypescriptPlugin::format_model(namespace, &model); - - let expected = r#" - // Model definition for `game::models::Position` model - Position: (() => { - return defineComponent( - world, - { - x: RecsType.Number, - y: RecsType.Number, - player: PlayerDefinition, - }, - { - metadata: { - namespace: "game", - name: "Position", - types: ["u32", "u32"], - customTypes: ["Player"], - }, - } - ); - })(), + } + ); + })(), "#; - assert_eq!(formatted.trim(), expected.trim()); - } + assert_eq!(formatted.replace([' ', '\n'], "").trim(), expected.replace([' ', '\n'], "").trim()); +} - #[test] - fn test_format_enum_model() { - // Create a mock Composite representing an enum model - let model = Composite { - type_path: "game::models::Direction".to_string(), - r#type: CompositeType::Enum, - generic_args: vec![], - inners: vec![CompositeInner { - index: 0, - name: "North".to_string(), - kind: CompositeInnerKind::Data, - token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { - type_path: "()".to_string(), - }), - }], - is_event: false, - alias: None, - }; - - let namespace = "game"; - let formatted = TypescriptPlugin::format_model(namespace, &model); - - let expected = r#" - // Model definition for `game::models::Direction` model - Direction: (() => { - return defineComponent( - world, - { - North: RecsType.String, +#[test] +fn test_format_enum_model() { + // Create a mock Composite representing an enum model + let model = Composite { + type_path: "game::models::Direction".to_string(), + r#type: CompositeType::Enum, + generic_args: vec![], + inners: vec![CompositeInner { + index: 0, + name: "North".to_string(), + kind: CompositeInnerKind::Data, + token: Token::CoreBasic(cainome::parser::tokens::CoreBasic { + type_path: "()".to_string(), + }), + }], + is_event: false, + alias: None, + }; + + let namespace = "game"; + let formatted = TypescriptPlugin::format_model(namespace, &model); + + let expected = r#" + // Model definition for `game::models::Direction` model + Direction: (() => { + return defineComponent( + world, + { + North: ()Definition, + }, + { + metadata: { + namespace: "game", + name: "Direction", + types: [], + customTypes: ["()"], }, - { - metadata: { - namespace: "game", - name: "Direction", - types: ["()"], - customTypes: [], - }, - } - ); - })(), + } + ); + })(), "#; - assert_eq!(formatted.trim(), expected.trim()); - } - // // Helper function to create mock DojoData for testing - fn create_mock_dojo_data() -> DojoData { - DojoData { - world: DojoWorld { name: 0x01.to_string() }, - models: HashMap::new(), - contracts: HashMap::new(), - } + // Remove all spaces and compare + assert_eq!(formatted.replace([' ', '\n'], "").trim(), expected.replace([' ', '\n'], "").trim()); +} + +// // Helper function to create mock DojoData for testing +fn create_mock_dojo_data() -> DojoData { + DojoData { + world: DojoWorld { name: 0x01.to_string() }, + models: HashMap::new(), + contracts: HashMap::new(), } } From 2d04f58f3be987abe11fcfda8cec4a2ab66fd317 Mon Sep 17 00:00:00 2001 From: glihm Date: Wed, 24 Jul 2024 21:56:09 -0600 Subject: [PATCH 11/12] chore: bump cainome --- Cargo.lock | 46 +++++++++++++++++++++++----------------------- Cargo.toml | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84bca0e296..5f41c86f00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2082,14 +2082,14 @@ checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0" [[package]] name = "cainome" version = "0.2.3" -source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" +source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.2#3aec6d1465e24af3765d3b9220cc233199a6aa14" dependencies = [ "anyhow", "async-trait", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-rs-macro 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-rs-macro 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "camino", "clap", "clap_complete", @@ -2132,7 +2132,7 @@ dependencies = [ [[package]] name = "cainome-cairo-serde" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" +source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.2#3aec6d1465e24af3765d3b9220cc233199a6aa14" dependencies = [ "serde", "starknet 0.11.0", @@ -2152,7 +2152,7 @@ dependencies = [ [[package]] name = "cainome-parser" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" +source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.2#3aec6d1465e24af3765d3b9220cc233199a6aa14" dependencies = [ "convert_case 0.6.0", "quote", @@ -2178,11 +2178,11 @@ dependencies = [ [[package]] name = "cainome-rs" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" +source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.2#3aec6d1465e24af3765d3b9220cc233199a6aa14" dependencies = [ "anyhow", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "camino", "prettyplease 0.2.20", "proc-macro2", @@ -2214,12 +2214,12 @@ dependencies = [ [[package]] name = "cainome-rs-macro" version = "0.1.0" -source = "git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution#338e30b0bd3f3a5a414daa268f866a6b1251647e" +source = "git+https://github.com/cartridge-gg/cainome?tag=v0.3.2#3aec6d1465e24af3765d3b9220cc233199a6aa14" dependencies = [ "anyhow", - "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", - "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome-cairo-serde 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-parser 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", + "cainome-rs 0.1.0 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "proc-macro2", "quote", "serde_json", @@ -4208,7 +4208,7 @@ version = "1.0.0-alpha.2" dependencies = [ "assert_matches", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "camino", "chrono", "convert_case 0.6.0", @@ -4236,7 +4236,7 @@ version = "1.0.0-alpha.2" dependencies = [ "anyhow", "assert_fs", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "cairo-lang-compiler", "cairo-lang-debug", "cairo-lang-defs", @@ -4352,7 +4352,7 @@ dependencies = [ name = "dojo-types" version = "1.0.0-alpha.2" dependencies = [ - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "crypto-bigint", "hex", "itertools 0.12.1", @@ -4373,7 +4373,7 @@ dependencies = [ "assert_fs", "assert_matches", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "cairo-lang-filesystem", "cairo-lang-project", "cairo-lang-starknet", @@ -7738,7 +7738,7 @@ dependencies = [ "alloy", "anyhow", "assert_matches", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "dojo-metrics", "dojo-test-utils", "dojo-world", @@ -12487,7 +12487,7 @@ dependencies = [ "assert_fs", "async-trait", "bigdecimal 0.4.5", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-filesystem", @@ -12547,7 +12547,7 @@ dependencies = [ "assert_fs", "async-trait", "bigdecimal 0.4.5", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-filesystem", @@ -14153,7 +14153,7 @@ dependencies = [ "anyhow", "async-trait", "base64 0.21.7", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "camino", "chrono", "crypto-bigint", @@ -14278,7 +14278,7 @@ version = "1.0.0-alpha.2" dependencies = [ "anyhow", "async-trait", - "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?branch=fix/inner-composite-resolution)", + "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?tag=v0.3.2)", "chrono", "crypto-bigint", "dojo-test-utils", diff --git a/Cargo.toml b/Cargo.toml index 554d720efd..a8dd04ec03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,7 @@ inherits = "release" lto = "fat" [workspace.dependencies] -cainome = { git = "https://github.com/cartridge-gg/cainome", branch = "fix/inner-composite-resolution", features = [ "abigen-rs" ] } +cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.3.2", features = [ "abigen-rs" ] } common = { path = "crates/common" } # metrics From 05c75cf511195ae6d33f39faf9ddc4048b6a37f5 Mon Sep 17 00:00:00 2001 From: glihm Date: Wed, 24 Jul 2024 22:07:28 -0600 Subject: [PATCH 12/12] chore: move deps to dev deps --- crates/dojo-bindgen/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/dojo-bindgen/Cargo.toml b/crates/dojo-bindgen/Cargo.toml index 8498eb10a8..747a70a796 100644 --- a/crates/dojo-bindgen/Cargo.toml +++ b/crates/dojo-bindgen/Cargo.toml @@ -7,7 +7,6 @@ repository.workspace = true version.workspace = true [dependencies] -assert_matches.workspace = true async-trait.workspace = true camino.workspace = true chrono.workspace = true @@ -16,7 +15,6 @@ serde.workspace = true serde_json.workspace = true starknet.workspace = true thiserror.workspace = true -tokio.workspace = true # Some issue with CI on windows, need to be investigated. # https://github.com/dojoengine/dojo/actions/runs/7736050751/job/21092743552?pr=1501#step:6:249 @@ -26,5 +24,7 @@ cainome.workspace = true dojo-world = { path = "../dojo-world", features = [ "manifest" ] } [dev-dependencies] +assert_matches.workspace = true dojo-test-utils = { path = "../dojo-test-utils", features = [ "build-examples" ] } scarb = { workspace = true } +tokio.workspace = true