Skip to content

Commit

Permalink
feat: recursive types in model definition
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Dec 5, 2024
1 parent 6917660 commit a541768
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 52 deletions.
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ debug = true
inherits = "release"

[workspace.dependencies]
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.8", features = [ "abigen-rs" ] }
cainome-cairo-serde = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.8" }
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.10", features = [ "abigen-rs" ] }
cainome-cairo-serde = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.10" }
dojo-utils = { path = "crates/dojo/utils" }

# metrics
Expand Down
9 changes: 5 additions & 4 deletions crates/dojo/bindgen/src/plugins/typescript/generator/enum.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cainome::parser::tokens::{Composite, CompositeType};

use super::constants::{CAIRO_ENUM_IMPORT, CAIRO_ENUM_TOKEN, SN_IMPORT_SEARCH};
use super::token_is_custom_enum;
use crate::error::BindgenResult;
use crate::plugins::typescript::generator::JsType;
use crate::plugins::{BindgenModelGenerator, Buffer};

use super::constants::{CAIRO_ENUM_IMPORT, CAIRO_ENUM_TOKEN, SN_IMPORT_SEARCH};
use super::token_is_custom_enum;

const CAIRO_ENUM_TYPE_IMPL: &str = "export type TypedCairoEnum<T> = CairoCustomEnum & {\n\tvariant: { [K in keyof T]: T[K] | undefined };\n\tunwrap(): T[keyof T];\n}\n";
const CAIRO_ENUM_TYPE_IMPL: &str = "export type TypedCairoEnum<T> = CairoCustomEnum & \
{\n\tvariant: { [K in keyof T]: T[K] | undefined \
};\n\tunwrap(): T[keyof T];\n}\n";

pub(crate) struct TsEnumGenerator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ impl BindgenModelGenerator for TsInterfaceGenerator {
if token.r#type != CompositeType::Struct || token.inners.is_empty() {
return Ok(String::new());
}
if buffer
.has(format!("// Type definition for `{path}` struct", path = token.type_path).as_str())
{
return Ok(String::new());

Check warning on line 46 in crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs#L46

Added line #L46 was not covered by tests
}

self.check_import(token, buffer);
self.add_input_type(buffer);
Expand Down Expand Up @@ -124,7 +129,8 @@ mod tests {
result,
"// Type definition for `core::test::TestStruct` struct\nexport interface TestStruct \
{\n\tfieldOrder: string[];\n\tfield1: BigNumberish;\n\tfield2: \
BigNumberish;\n\tfield3: BigNumberish;\n}\nexport type InputTestStruct = RemoveFieldOrder<TestStruct>;\n"
BigNumberish;\n\tfield3: BigNumberish;\n}\nexport type InputTestStruct = \
RemoveFieldOrder<TestStruct>;\n"
);
}

Expand Down
Loading

0 comments on commit a541768

Please sign in to comment.