Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sozo): adjust typescript bindgen to v1 #2202

Merged
merged 13 commits into from
Jul 25, 2024
106 changes: 94 additions & 12 deletions crates/dojo-bindgen/src/plugins/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
export interface {name} {{
{native_fields}
}}

export const {name}Definition = {{
{fields}
}};
Expand All @@ -134,12 +133,22 @@
// 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::<core::integer::u32>"
|| token.type_path == "core::option::Option::<core::integer::u8>"
|| token.type_path == "core::option::Option::<core::integer::u16>"
|| token.type_path == "core::option::Option::<core::integer::u64>"
|| token.type_path == "core::option::Option::<core::integer::u128>"
|| token.type_path == "core::option::Option::<core::integer::u256>"

Check warning on line 141 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L136-L141

Added lines #L136 - L141 were not covered by tests
{
return String::new(); // Return an empty string for these enums
}

Check warning on line 145 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L143-L145

Added lines #L143 - L145 were not covered by tests
let name = TypescriptPlugin::map_type(&Token::Composite(token.clone()));

let mut result = format!(
"
// Type definition for `{}` enum
type {} = ",
export type {} = ",

Check warning on line 151 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L151

Added line #L151 was not covered by tests
token.type_path, name
);

Expand All @@ -165,10 +174,10 @@

result += format!(
"
export const {name}Definition = {{
type: RecsType.String,{}
}};
",
export const {name}Definition = {{
type: RecsType.String,{}
}};
",

Check warning on line 180 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L177-L180

Added lines #L177 - L180 were not covered by tests
if !token.inners.is_empty() {
"\n value: RecsType.String".to_string()
} else {
Expand Down Expand Up @@ -314,7 +323,20 @@
// 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())

Check warning on line 336 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L326-L336

Added lines #L326 - L336 were not covered by tests
{
return String::new();
}

Check warning on line 339 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L338-L339

Added lines #L338 - L339 were not covered by tests
Comment on lines +347 to +360
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohayo sensei! Optimize the filtering mechanism for systems.

Consider using a set for cleaner and more efficient filtering of system names.

let excluded_systems: HashSet<&str> = [
    "contract_name",
    "namespace",
    "tag",
    "name_hash",
    "selector",
    "dojo_init",
    "namespace_hash",
].iter().cloned().collect();

if excluded_systems.contains(system.name.as_str()) {
    return String::new();
}

fn map_type(token: &Token) -> String {
match token {
Token::CoreBasic(_) => TypescriptPlugin::map_type(token)
Expand All @@ -329,6 +351,18 @@
let args = system
.inputs
.iter()
.filter(|arg| {
![
"contract_name",
"namespace",
"tag",
"name_hash",
"selector",
"dojo_init",
"namespace_hash",
]
.contains(&arg.0.as_str())
})

Check warning on line 365 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L354-L365

Added lines #L354 - L365 were not covered by tests
.map(|arg| format!("{}: {}", arg.0, map_type(&arg.1)))
.collect::<Vec<String>>()
.join(", ");
Expand Down Expand Up @@ -395,6 +429,18 @@
let calldata = system
.inputs
.iter()
.filter(|arg| {
![
"contract_name",
"namespace",
"tag",
"name_hash",
"selector",
"dojo_init",
"namespace_hash",
]
.contains(&arg.0.as_str())
})

Check warning on line 443 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L432-L443

Added lines #L432 - L443 were not covered by tests
.map(|arg| handle_arg_recursive(&arg.0, &arg.1, handled_tokens))
.collect::<Vec<String>>()
.join(",\n ");
Expand All @@ -408,7 +454,8 @@
props.account,
contract_name,
\"{system_name}\",
[{calldata}]
[{calldata}],
\"{namespace}\"

Check warning on line 458 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L457-L458

Added lines #L457 - L458 were not covered by tests
);
}} catch (error) {{
console.error(\"Error executing spawn:\", error);
Expand All @@ -423,14 +470,19 @@
// formatted args to use our mapped types
args = args,
// calldata for execute
calldata = calldata
calldata = calldata,
namespace = TypescriptPlugin::get_namespace_from_tag(&namespace)

Check warning on line 474 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L474

Added line #L474 was not covered by tests
)
}

// 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)
}

Check warning on line 482 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L481-L482

Added lines #L481 - L482 were not covered by tests

fn get_namespace_from_tag(tag: &str) -> String {
tag.split('-').next().unwrap_or(tag).to_string()

Check warning on line 485 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L484-L485

Added lines #L484 - L485 were not covered by tests
}

// Handles a contract definition and its underlying systems
Expand Down Expand Up @@ -458,8 +510,25 @@
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)
})

Check warning on line 525 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L513-L525

Added lines #L513 - L525 were not covered by tests
.map(|system| {
TypescriptPlugin::format_system(system.to_function().unwrap(), handled_tokens)
TypescriptPlugin::format_system(
system.to_function().unwrap(),
handled_tokens,
contract.tag.clone(),
)

Check warning on line 531 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L527-L531

Added lines #L527 - L531 were not covered by tests
})
.collect::<Vec<String>>()
.join("\n\n ");
Expand All @@ -485,6 +554,19 @@
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)
})

Check warning on line 569 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L557-L569

Added lines #L557 - L569 were not covered by tests
.map(|system| { system.to_function().unwrap().name.to_string() })
.collect::<Vec<String>>()
.join(", ")
Expand Down
Loading