Skip to content

Commit

Permalink
Fix/bindgen types improvements (#2832)
Browse files Browse the repository at this point in the history
* feat: bindgen types improvements

* feat: js ByteArray function input

* feat: bindgen enums improvements
  • Loading branch information
MartianGreed authored Dec 20, 2024
1 parent c0b8033 commit 93168a1
Show file tree
Hide file tree
Showing 11 changed files with 599 additions and 309 deletions.
9 changes: 0 additions & 9 deletions crates/dojo/bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ impl Buffer {
}
}

/// Inserts string at the specified index.
///
/// * `s` - The string to insert.
/// * `pos` - The position to insert the string at.
/// * `idx` - The index of the string to insert at.
pub fn insert_at_index(&mut self, s: String, idx: usize) {
self.0.insert(idx, s);
}

/// Finds position of the given string in the inner vec.
///
/// * `pos` - The string to search for.
Expand Down
21 changes: 9 additions & 12 deletions crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ pub const CAIRO_U256: &str = "u256";
pub const CAIRO_U256_STRUCT: &str = "U256";
pub const CAIRO_I128: &str = "i128";
pub const CAIRO_BOOL: &str = "bool";
pub const CAIRO_OPTION: &str = "Option";

pub(crate) const CAIRO_OPTION_DEFAULT_VALUE: &str = "new CairoOption(CairoOptionVariant.None)";

pub const JS_BOOLEAN: &str = "boolean";
pub const JS_STRING: &str = "string";
pub const JS_BIGNUMBERISH: &str = "BigNumberish";

pub(crate) const BIGNUMNERISH_IMPORT: &str = "import type { BigNumberish } from 'starknet';\n";
pub(crate) const CAIRO_OPTION_IMPORT: &str = "import type { CairoOption } from 'starknet';\n";
pub(crate) const CAIRO_ENUM_IMPORT: &str = "import type { CairoCustomEnum } from 'starknet';\n";
pub(crate) const BIGNUMNERISH_IMPORT: &str = "import { BigNumberish } from 'starknet';\n";
pub(crate) const CAIRO_OPTION_IMPORT: &str = "import { CairoOption } from 'starknet';\n";
pub(crate) const CAIRO_ENUM_IMPORT: &str = "import { CairoCustomEnum } from 'starknet';\n";
pub(crate) const CAIRO_OPTION_TYPE_PATH: &str = "core::option::Option";
pub(crate) const SN_IMPORT_SEARCH: &str = "} from 'starknet';";
pub(crate) const CAIRO_OPTION_TOKEN: &str = "CairoOption,";
pub(crate) const CAIRO_OPTION_TOKEN: &str = "CairoOption, CairoOptionVariant,";
pub(crate) const CAIRO_ENUM_TOKEN: &str = "CairoCustomEnum,";

pub(crate) const REMOVE_FIELD_ORDER_TYPE_DEF: &str = "type RemoveFieldOrder<T> = T extends object
? Omit<
{
[K in keyof T]: T[K] extends object ? RemoveFieldOrder<T[K]> : T[K];
},
'fieldOrder'
>
: T;";
pub(crate) const WITH_FIELD_ORDER_TYPE_DEF: &str =
"type WithFieldOrder<T> = T & { fieldOrder: string[] };\n";
16 changes: 5 additions & 11 deletions crates/dojo/bindgen/src/plugins/typescript/generator/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ 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::typescript::generator::JsPrimitiveType;
use crate::plugins::{BindgenModelGenerator, Buffer};

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;

impl TsEnumGenerator {
Expand All @@ -24,10 +20,6 @@ impl TsEnumGenerator {
buffer.insert_after(format!(" {CAIRO_ENUM_TOKEN}"), SN_IMPORT_SEARCH, "{", 1);
}
}
if !buffer.has(CAIRO_ENUM_TYPE_IMPL) {
let pos = buffer.pos(SN_IMPORT_SEARCH).unwrap();
buffer.insert_at_index(CAIRO_ENUM_TYPE_IMPL.to_owned(), pos + 1);
}
}
}

Expand All @@ -44,14 +36,16 @@ impl BindgenModelGenerator for TsEnumGenerator {
export type {name} = {{
{variants}
}}
export type {name}Enum = TypedCairoEnum<{name}>;
export type {name}Enum = CairoCustomEnum;
",
path = token.type_path,
name = token.type_name(),
variants = token
.inners
.iter()
.map(|inner| { format!("\t{}: {};", inner.name, JsType::from(&inner.token)) })
.map(|inner| {
format!("\t{}: {};", inner.name, JsPrimitiveType::from(&inner.token))
})
.collect::<Vec<String>>()
.join("\n")
)
Expand Down
116 changes: 0 additions & 116 deletions crates/dojo/bindgen/src/plugins/typescript/generator/erc.rs

This file was deleted.

Loading

0 comments on commit 93168a1

Please sign in to comment.