@@ -2,14 +2,12 @@ use std::collections::HashSet;
22
33use fuel_abi_types:: abi:: full_program:: FullTypeDeclaration ;
44use itertools:: Itertools ;
5- use quote:: quote;
5+ use quote:: { quote, ToTokens } ;
66
77use crate :: {
88 error:: Result ,
99 program_bindings:: {
10- custom_types:: { enums:: expand_custom_enum, structs:: expand_custom_struct} ,
11- generated_code:: GeneratedCode ,
12- utils:: sdk_provided_custom_types_lookup,
10+ custom_types:: { enums:: expand_custom_enum, structs:: expand_custom_struct} , generated_code:: GeneratedCode , resolved_type:: TypeResolver , utils:: sdk_provided_custom_types_lookup
1311 } ,
1412 utils:: TypePath ,
1513} ;
@@ -40,6 +38,8 @@ pub(crate) fn generate_types<'a, T: IntoIterator<Item = &'a FullTypeDeclaration>
4038 . map ( |ttype : & FullTypeDeclaration | {
4139 if shared_types. contains ( ttype) {
4240 reexport_the_shared_type ( ttype, no_std)
41+ } else if ttype. is_alias_type ( ) {
42+ expand_alias_type ( ttype, no_std)
4343 } else if ttype. is_struct_type ( ) {
4444 expand_custom_struct ( ttype, no_std)
4545 } else {
@@ -51,13 +51,62 @@ pub(crate) fn generate_types<'a, T: IntoIterator<Item = &'a FullTypeDeclaration>
5151 } )
5252}
5353
54+ fn expand_alias_type ( ttype : & FullTypeDeclaration , no_std : bool ) -> Result < GeneratedCode > {
55+ let type_path = ttype. alias_type_path ( ) . expect ( "This must be an alias type" ) ;
56+ let type_path_ident = syn:: Ident :: new (
57+ type_path. ident ( ) . unwrap ( ) . to_string ( ) . as_str ( ) ,
58+ proc_macro2:: Span :: call_site ( ) ,
59+ ) ;
60+
61+ let alias_of = ttype. alias_of . as_ref ( ) . unwrap ( ) ;
62+ //eprintln!("alias_of: {:?}", alias_of);
63+
64+ // let mut raw_type_str = alias_of.name.as_str();
65+
66+ // if let Some(stripped) = raw_type_str.strip_prefix("struct ") {
67+ // raw_type_str = stripped;
68+ // } else if let Some(stripped) = raw_type_str.strip_prefix("enum ") {
69+ // raw_type_str = stripped;
70+ // }
71+
72+ let resolver = TypeResolver :: default ( ) ;
73+ let resolved_alias = resolver. resolve ( alias_of) ?;
74+ //eprintln!("resolved_alias: {:?}", resolved_alias);
75+ // panic!();
76+
77+ // let alias_of_path: syn::Type =
78+ // syn::parse_str(raw_type_str).expect("Failed to parse type");
79+
80+ let alias_of_path = resolved_alias. to_token_stream ( ) ;
81+
82+ // eprintln!("type_path: {:?}", type_path);
83+ // panic!();
84+
85+ let type_mod = type_path. parent ( ) ;
86+
87+ let top_lvl_mod = TypePath :: default ( ) ;
88+ let from_current_mod_to_top_level = top_lvl_mod. relative_path_from ( & type_mod) ;
89+
90+ let path = from_current_mod_to_top_level. append ( type_path) ;
91+
92+ let the_reexport = quote ! { pub type #type_path_ident = #alias_of_path; } ;
93+
94+ Ok ( GeneratedCode :: new ( the_reexport, Default :: default ( ) , no_std) . wrap_in_mod ( type_mod) )
95+ }
96+
5497/// Instead of generating bindings for `ttype` this fn will just generate a `pub use` pointing to
5598/// the already generated equivalent shared type.
5699fn reexport_the_shared_type ( ttype : & FullTypeDeclaration , no_std : bool ) -> Result < GeneratedCode > {
57100 // e.g. some_library::another_mod::SomeStruct
58- let type_path = ttype
59- . custom_type_path ( )
60- . expect ( "This must be a custom type due to the previous filter step" ) ;
101+ let type_path = if ttype. is_custom_type ( ) {
102+ ttype
103+ . custom_type_path ( )
104+ . expect ( "This must be a custom type due to the previous filter step" )
105+ } else if ttype. is_alias_type ( ) {
106+ ttype. alias_type_path ( ) . expect ( "This must be an alias type" )
107+ } else {
108+ unreachable ! ( )
109+ } ;
61110
62111 let type_mod = type_path. parent ( ) ;
63112
@@ -85,6 +134,10 @@ fn reexport_the_shared_type(ttype: &FullTypeDeclaration, no_std: bool) -> Result
85134// implementation details of the contract's Vec type and are not directly
86135// used in the SDK.
87136fn should_skip_codegen ( type_decl : & FullTypeDeclaration ) -> bool {
137+ if type_decl. is_alias_type ( ) {
138+ return false ;
139+ }
140+
88141 if !type_decl. is_custom_type ( ) {
89142 return true ;
90143 }
0 commit comments