1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::collections::HashSet;
pub use abigen_target::{AbigenTarget, ProgramType};
use inflector::Inflector;
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;
use regex::Regex;
use crate::{
error::Result,
program_bindings::{
abi_types::FullTypeDeclaration,
abigen::{abigen_target::ParsedAbigenTarget, bindings::generate_bindings},
custom_types::generate_types,
generated_code::GeneratedCode,
},
utils::ident,
};
mod abigen_target;
mod bindings;
mod configurables;
mod logs;
pub struct Abigen;
impl Abigen {
pub fn generate(targets: Vec<AbigenTarget>, no_std: bool) -> Result<TokenStream> {
let parsed_targets = Self::parse_targets(targets)?;
let generated_code = Self::generate_code(no_std, parsed_targets)?;
let use_statements = generated_code.use_statements_for_uniquely_named_types();
let code = if no_std {
Self::wasm_paths_hotfix(&generated_code.code())
} else {
generated_code.code()
};
Ok(quote! {
#code
#use_statements
})
}
fn wasm_paths_hotfix(code: &TokenStream) -> TokenStream {
[
(r#"::\s*fuels\s*::\s*core"#, "::fuels_core"),
(r#"::\s*fuels\s*::\s*macros"#, "::fuels_macros"),
(r#"::\s*fuels\s*::\s*programs"#, "::fuels_programs"),
(r#"::\s*fuels\s*::\s*signers"#, "::fuels_signers"),
(r#"::\s*fuels\s*::\s*tx"#, "::fuel_tx"),
(r#"::\s*fuels\s*::\s*types"#, "::fuels_types"),
(r#"::\s*std\s*::\s*string"#, "::alloc::string"),
(r#"::\s*std\s*::\s*format"#, "::alloc::format"),
(r#"::\s*std\s*::\s*vec"#, "::alloc::vec"),
(r#"::\s*std\s*::\s*boxed"#, "::alloc::boxed"),
]
.map(|(reg_expr_str, substitute)| (Regex::new(reg_expr_str).unwrap(), substitute))
.into_iter()
.fold(code.to_string(), |code, (regex, wasm_include)| {
regex.replace_all(&code, wasm_include).to_string()
})
.parse()
.expect("Wasm hotfix failed!")
}
fn generate_code(
no_std: bool,
parsed_targets: Vec<ParsedAbigenTarget>,
) -> Result<GeneratedCode> {
let custom_types = Self::filter_custom_types(&parsed_targets);
let shared_types = Self::filter_shared_types(custom_types);
let bindings = Self::generate_all_bindings(parsed_targets, no_std, &shared_types)?;
let shared_types = Self::generate_shared_types(shared_types, no_std)?;
let mod_name = ident("abigen_bindings");
Ok(shared_types.merge(bindings).wrap_in_mod(mod_name))
}
fn generate_all_bindings(
parsed_targets: Vec<ParsedAbigenTarget>,
no_std: bool,
shared_types: &HashSet<FullTypeDeclaration>,
) -> Result<GeneratedCode> {
parsed_targets
.into_iter()
.map(|target| Self::generate_binding(target, no_std, shared_types))
.fold_ok(GeneratedCode::default(), |acc, generated_code| {
acc.merge(generated_code)
})
}
fn generate_binding(
target: ParsedAbigenTarget,
no_std: bool,
shared_types: &HashSet<FullTypeDeclaration>,
) -> Result<GeneratedCode> {
let mod_name = ident(&format!("{}_mod", &target.name.to_snake_case()));
let types = generate_types(&target.source.types, shared_types, no_std)?;
let bindings = generate_bindings(target, no_std)?;
Ok(types.merge(bindings).wrap_in_mod(mod_name))
}
fn parse_targets(targets: Vec<AbigenTarget>) -> Result<Vec<ParsedAbigenTarget>> {
targets
.into_iter()
.map(|target| target.try_into())
.collect()
}
fn generate_shared_types(
shared_types: HashSet<FullTypeDeclaration>,
no_std: bool,
) -> Result<GeneratedCode> {
let types = generate_types(&shared_types, &HashSet::default(), no_std)?;
if types.is_empty() {
Ok(Default::default())
} else {
let mod_name = ident("shared_types");
Ok(types.wrap_in_mod(mod_name))
}
}
fn filter_custom_types(
all_types: &[ParsedAbigenTarget],
) -> impl Iterator<Item = &FullTypeDeclaration> {
all_types
.iter()
.flat_map(|target| &target.source.types)
.filter(|ttype| ttype.is_custom_type())
}
fn filter_shared_types<'a>(
all_custom_types: impl IntoIterator<Item = &'a FullTypeDeclaration>,
) -> HashSet<FullTypeDeclaration> {
all_custom_types.into_iter().duplicates().cloned().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correctly_determines_shared_types() {
let types = ["type_0", "type_1", "type_0"].map(|type_field| FullTypeDeclaration {
type_field: type_field.to_string(),
components: vec![],
type_parameters: vec![],
});
let shared_types = Abigen::filter_shared_types(&types);
assert_eq!(shared_types, HashSet::from([types[0].clone()]))
}
}