@@ -2,13 +2,14 @@ use std::collections::{HashMap, HashSet};
22
33use fuel_abi_types:: abi:: full_program:: { FullLoggedType , FullTypeDeclaration } ;
44use itertools:: Itertools ;
5- use quote:: quote;
5+ use quote:: { ToTokens , quote} ;
66
77use crate :: {
88 error:: Result ,
99 program_bindings:: {
1010 custom_types:: { enums:: expand_custom_enum, structs:: expand_custom_struct} ,
1111 generated_code:: GeneratedCode ,
12+ resolved_type:: TypeResolver ,
1213 utils:: sdk_provided_custom_types_lookup,
1314 } ,
1415 utils:: TypePath ,
@@ -21,7 +22,7 @@ pub(crate) mod utils;
2122/// Generates Rust code for each type inside `types` if:
2223/// * the type is not present inside `shared_types`, and
2324/// * if it should be generated (see: [`should_skip_codegen`], and
24- /// * if it is a struct or an enum.
25+ /// * if it is a struct or an enum or an alias .
2526///
2627///
2728/// # Arguments
@@ -47,6 +48,8 @@ pub(crate) fn generate_types<'a>(
4748 let log_id = log_ids. get ( & ttype. type_field ) ;
4849 if shared_types. contains ( ttype) {
4950 reexport_the_shared_type ( ttype, no_std)
51+ } else if ttype. is_alias_type ( ) {
52+ expand_alias_type ( ttype, no_std)
5053 } else if ttype. is_struct_type ( ) {
5154 expand_custom_struct ( ttype, no_std, log_id)
5255 } else {
@@ -58,13 +61,62 @@ pub(crate) fn generate_types<'a>(
5861 } )
5962}
6063
64+ fn expand_alias_type ( ttype : & FullTypeDeclaration , no_std : bool ) -> Result < GeneratedCode > {
65+ let type_path = ttype. alias_type_path ( ) . expect ( "This must be an alias type" ) ;
66+ let type_path_ident = syn:: Ident :: new (
67+ type_path. ident ( ) . unwrap ( ) . to_string ( ) . as_str ( ) ,
68+ proc_macro2:: Span :: call_site ( ) ,
69+ ) ;
70+
71+ let alias_of = ttype. alias_of . as_ref ( ) . unwrap ( ) ;
72+ eprintln ! ( "alias_of: {:?}" , alias_of) ;
73+
74+ // let mut raw_type_str = alias_of.name.as_str();
75+
76+ // if let Some(stripped) = raw_type_str.strip_prefix("struct ") {
77+ // raw_type_str = stripped;
78+ // } else if let Some(stripped) = raw_type_str.strip_prefix("enum ") {
79+ // raw_type_str = stripped;
80+ // }
81+
82+ let resolver = TypeResolver :: default ( ) ;
83+ let resolved_alias = resolver. resolve ( alias_of. as_ref ( ) ) ?;
84+ eprintln ! ( "resolved_alias: {:?}" , resolved_alias) ;
85+ // panic!();
86+
87+ // let alias_of_path: syn::Type =
88+ // syn::parse_str(raw_type_str).expect("Failed to parse type");
89+
90+ let alias_of_path = resolved_alias. to_token_stream ( ) ;
91+
92+ // eprintln!("type_path: {:?}", type_path);
93+ // panic!();
94+
95+ let type_mod = type_path. parent ( ) ;
96+
97+ let top_lvl_mod = TypePath :: default ( ) ;
98+ let from_current_mod_to_top_level = top_lvl_mod. relative_path_from ( & type_mod) ;
99+
100+ let _path = from_current_mod_to_top_level. append ( type_path) ;
101+
102+ let the_reexport = quote ! { pub type #type_path_ident = #alias_of_path; } ;
103+
104+ Ok ( GeneratedCode :: new ( the_reexport, Default :: default ( ) , no_std) . wrap_in_mod ( type_mod) )
105+ }
106+
61107/// Instead of generating bindings for `ttype` this fn will just generate a `pub use` pointing to
62108/// the already generated equivalent shared type.
63109fn reexport_the_shared_type ( ttype : & FullTypeDeclaration , no_std : bool ) -> Result < GeneratedCode > {
64110 // e.g. some_library::another_mod::SomeStruct
65- let type_path = ttype
66- . custom_type_path ( )
67- . expect ( "This must be a custom type due to the previous filter step" ) ;
111+ let type_path = if ttype. is_custom_type ( ) {
112+ ttype
113+ . custom_type_path ( )
114+ . expect ( "This must be a custom type due to the previous filter step" )
115+ } else if ttype. is_alias_type ( ) {
116+ ttype. alias_type_path ( ) . expect ( "This must be an alias type" )
117+ } else {
118+ unreachable ! ( )
119+ } ;
68120
69121 let type_mod = type_path. parent ( ) ;
70122
@@ -92,11 +144,17 @@ fn reexport_the_shared_type(ttype: &FullTypeDeclaration, no_std: bool) -> Result
92144// implementation details of the contract's Vec type and are not directly
93145// used in the SDK.
94146pub fn should_skip_codegen ( type_decl : & FullTypeDeclaration ) -> bool {
95- if !type_decl. is_custom_type ( ) {
147+ if !type_decl. is_custom_type ( ) && !type_decl . is_alias_type ( ) {
96148 return true ;
97149 }
98150
99- let type_path = type_decl. custom_type_path ( ) . unwrap ( ) ;
151+ let type_path = if type_decl. is_custom_type ( ) {
152+ type_decl. custom_type_path ( ) . unwrap ( )
153+ } else if type_decl. is_alias_type ( ) {
154+ type_decl. alias_type_path ( ) . unwrap ( )
155+ } else {
156+ unreachable ! ( )
157+ } ;
100158
101159 is_type_sdk_provided ( & type_path) || is_type_unused ( & type_path)
102160}
0 commit comments