Skip to content

Commit

Permalink
fix: do not expand core generics and fix struct def expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Dec 16, 2023
1 parent f230437 commit 5a3cd11
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
8 changes: 8 additions & 0 deletions crates/rs/src/expand/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub struct CairoEnum;

impl CairoEnum {
pub fn expand_decl(composite: &Composite) -> TokenStream2 {
if composite.is_builtin() {
return quote!();
}

let enum_name = utils::str_to_ident(&composite.type_name_or_alias());

let mut variants: Vec<TokenStream2> = vec![];
Expand Down Expand Up @@ -55,6 +59,10 @@ impl CairoEnum {
}

pub fn expand_impl(composite: &Composite) -> TokenStream2 {
if composite.is_builtin() {
return quote!();
}

let name_str = &composite.type_name_or_alias();
let enum_name = utils::str_to_ident(name_str);

Expand Down
8 changes: 8 additions & 0 deletions crates/rs/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub struct CairoStruct;

impl CairoStruct {
pub fn expand_decl(composite: &Composite) -> TokenStream2 {
if composite.is_builtin() {
return quote!();
}

let struct_name = utils::str_to_ident(&composite.type_name_or_alias());

let mut members: Vec<TokenStream2> = vec![];
Expand Down Expand Up @@ -50,6 +54,10 @@ impl CairoStruct {
}

pub fn expand_impl(composite: &Composite) -> TokenStream2 {
if composite.is_builtin() {
return quote!();
}

let struct_name = utils::str_to_ident(&composite.type_name_or_alias());

let mut sizes: Vec<TokenStream2> = vec![];
Expand Down
17 changes: 16 additions & 1 deletion crates/rs/src/expand/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ impl CairoToRust for Token {

s
}
Token::Composite(c) => c.type_name_or_alias(),
Token::Composite(c) => {
let mut s = c.type_name_or_alias();

if c.is_generic() {
s.push('<');
for (i, (_, g)) in c.generic_args.iter().enumerate() {
s.push_str(&g.to_rust_type());
if i < c.generic_args.len() - 1 {
s.push(',');
}
}
s.push('>');
}

s
}
Token::GenericArg(s) => s.clone(),
_ => "__FUNCTION_NOT_SUPPORTED__".to_string(),
}
Expand Down
1 change: 0 additions & 1 deletion crates/rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use quote::quote;
mod expand;
mod macro_inputs;
mod spanned;
mod types;

use crate::expand::utils;
use crate::expand::{CairoContract, CairoEnum, CairoFunction, CairoStruct};
Expand Down
1 change: 0 additions & 1 deletion crates/rs/src/types/mod.rs

This file was deleted.

17 changes: 17 additions & 0 deletions examples/opt_res.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cainome::rs::abigen;
// use starknet::{
// accounts::{Account, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount},
// core::types::{BlockId, BlockTag, FieldElement},
// providers::{jsonrpc::HttpTransport, AnyProvider, JsonRpcClient},
// signers::{LocalWallet, SigningKey},
// };
// use std::sync::Arc;
// use url::Url;

// This example uses an ABI where components introduce several enums with `Event` type name.
// This showcase how the type_aliases parameter can be leveraged to avoid conflicts.

abigen!(MyContract, "./contracts/abi/option_result.abi.json",);

#[tokio::main]
async fn main() {}
17 changes: 17 additions & 0 deletions examples/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cainome::rs::abigen;
// use starknet::{
// accounts::{Account, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount},
// core::types::{BlockId, BlockTag, FieldElement},
// providers::{jsonrpc::HttpTransport, AnyProvider, JsonRpcClient},
// signers::{LocalWallet, SigningKey},
// };
// use std::sync::Arc;
// use url::Url;

// This example uses an ABI where components introduce several enums with `Event` type name.
// This showcase how the type_aliases parameter can be leveraged to avoid conflicts.

abigen!(MyContract, "./contracts/abi/structs.abi.json",);

#[tokio::main]
async fn main() {}

0 comments on commit 5a3cd11

Please sign in to comment.