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

feat: add events in typescript bindgen + sort functions by name #2853

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 72 additions & 37 deletions crates/dojo/bindgen/src/plugins/typescript/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,30 @@
fn write(&self, path: &str, data: &DojoData) -> BindgenResult<(PathBuf, Vec<u8>)> {
let models_path = Path::new(path).to_owned();
let models = data.models.values().collect::<Vec<_>>();
let events = data.events.values().collect::<Vec<_>>();
let mut e_composites = events
.iter()
.flat_map(|e| {
let mut composites = Vec::new();
let mut enum_composites =
e.tokens.enums.iter().map(|e| e.to_composite().unwrap()).collect::<Vec<_>>();
let mut struct_composites =
e.tokens.structs.iter().map(|s| s.to_composite().unwrap()).collect::<Vec<_>>();
let mut func_composites = e
.tokens
.functions
.iter()
.map(|f| f.to_composite().unwrap())
.collect::<Vec<_>>();
composites.append(&mut enum_composites);
composites.append(&mut struct_composites);
composites.append(&mut func_composites);
composites

Check warning on line 42 in crates/dojo/bindgen/src/plugins/typescript/writer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L28-L42

Added lines #L28 - L42 were not covered by tests
})
.filter(|c| !(c.type_path.starts_with("dojo::") || c.type_path.starts_with("core::")))
.collect::<Vec<_>>();

let mut composites = models
let mut m_composites = models
.iter()
.flat_map(|m| {
let mut composites: Vec<&Composite> = Vec::new();
Expand All @@ -46,13 +68,14 @@

// Sort models based on their tag to ensure deterministic output.
// models.sort_by(|a, b| a.tag.cmp(&b.tag));
composites.sort_by(|a, b| a.type_path.cmp(&b.type_path));
m_composites.sort_by(|a, b| a.type_path.cmp(&b.type_path));
e_composites.sort_by(|a, b| a.type_path.cmp(&b.type_path));

let code = self
.generators
.iter()
.fold(Buffer::new(), |mut acc, g| {
composites.iter().for_each(|c| {
[m_composites.clone(), e_composites.clone()].concat().iter().for_each(|c| {

Check warning on line 78 in crates/dojo/bindgen/src/plugins/typescript/writer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L78

Added line #L78 was not covered by tests
match g.generate(c, &mut acc) {
Ok(code) => {
if !code.is_empty() {
Expand Down Expand Up @@ -90,46 +113,58 @@
impl BindgenWriter for TsFileContractWriter {
fn write(&self, path: &str, data: &DojoData) -> BindgenResult<(PathBuf, Vec<u8>)> {
let models_path = Path::new(path).to_owned();
let mut functions = data
.contracts
.values()
.collect::<Vec<_>>()
.into_iter()
.flat_map(|c| {
c.systems
.clone()
.into_iter()
.filter(|s| {
let name = s.to_function().unwrap().name.as_str();
![
"contract_name",
"namespace",
"tag",
"name_hash",
"selector",
"dojo_init",
"namespace_hash",
"world",
"dojo_name",
"upgrade",
"world_dispatcher",
]
.contains(&name)
})
.map(|s| match s.to_function() {
Ok(f) => (c, f.clone()),

Check warning on line 143 in crates/dojo/bindgen/src/plugins/typescript/writer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L116-L143

Added lines #L116 - L143 were not covered by tests
Err(_) => {
panic!("Failed to get function out of system {:?}", &s)

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

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L145

Added line #L145 was not covered by tests
}
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
functions.sort_by(|(_, af), (_, bf)| af.name.cmp(&bf.name));

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L147 - L151 were not covered by tests

let code = self
.generators
.iter()
.fold(Buffer::new(), |mut acc, g| {
data.contracts.iter().for_each(|(_, c)| {
c.systems
.iter()
.filter(|s| {
let name = s.to_function().unwrap().name.as_str();
![
"contract_name",
"namespace",
"tag",
"name_hash",
"selector",
"dojo_init",
"namespace_hash",
"world",
"dojo_name",
"upgrade",
"world_dispatcher",
]
.contains(&name)
})
.for_each(|s| match s.to_function() {
Ok(f) => match g.generate(c, f, &mut acc) {
Ok(code) => {
if !code.is_empty() {
acc.push(code)
}
}
Err(_) => {
log::error!("Failed to generate code for system {:?}", s);
}
},
Err(_) => {
log::error!("Failed to get function out of system {:?}", s);
functions.iter().for_each(|(c, f)| {
match g.generate(c, f, &mut acc) {
Ok(code) => {
if !code.is_empty() {
acc.push(code)

Check warning on line 161 in crates/dojo/bindgen/src/plugins/typescript/writer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L157-L161

Added lines #L157 - L161 were not covered by tests
}
})
}
Err(_) => {
log::error!("Failed to generate code for function {:?}", f.name);

Check warning on line 165 in crates/dojo/bindgen/src/plugins/typescript/writer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/writer.rs#L165

Added line #L165 was not covered by tests
}
};
});

acc
Expand Down
Loading