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/bindgen types improvements #2832

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
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
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)";
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei, new default “new CairoOption(CairoOptionVariant.None).”

If your code typically expects “Some” as a default, confirm that “None” doesn’t break the user’s flow.


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
Loading