|
11 | 11 |
|
12 | 12 | use alloy_sol_macro_expander::expand::expand; |
13 | 13 | use alloy_sol_macro_input::{SolInput, SolInputKind}; |
14 | | -use eyre::{Context, Ok, OptionExt, Result}; |
| 14 | +use eyre::{Context, OptionExt, Result}; |
15 | 15 | use foundry_common::fs; |
16 | 16 | use proc_macro2::{Span, TokenStream}; |
17 | 17 | use std::{ |
@@ -39,7 +39,7 @@ impl SolMacroGen { |
39 | 39 | #path |
40 | 40 | }; |
41 | 41 |
|
42 | | - let sol_input: SolInput = syn::parse2(tokens).wrap_err("Failed to parse SolInput {e}")?; |
| 42 | + let sol_input: SolInput = syn::parse2(tokens).wrap_err("failed to parse input")?; |
43 | 43 |
|
44 | 44 | Ok(sol_input) |
45 | 45 | } |
@@ -69,24 +69,35 @@ impl MultiSolMacroGen { |
69 | 69 |
|
70 | 70 | pub fn generate_bindings(&mut self) -> Result<()> { |
71 | 71 | for instance in &mut self.instances { |
72 | | - let input = instance.get_sol_input()?.normalize_json()?; |
| 72 | + Self::generate_binding(instance).wrap_err_with(|| { |
| 73 | + format!( |
| 74 | + "failed to generate bindings for {}:{}", |
| 75 | + instance.path.display(), |
| 76 | + instance.name |
| 77 | + ) |
| 78 | + })?; |
| 79 | + } |
73 | 80 |
|
74 | | - let SolInput { attrs: _attrs, path: _path, kind } = input; |
| 81 | + Ok(()) |
| 82 | + } |
75 | 83 |
|
76 | | - let tokens = match kind { |
77 | | - SolInputKind::Sol(mut file) => { |
78 | | - let sol_attr: syn::Attribute = syn::parse_quote! { |
79 | | - #[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)] |
80 | | - }; |
81 | | - file.attrs.push(sol_attr); |
82 | | - expand(file).wrap_err("Failed to expand SolInput")? |
83 | | - } |
84 | | - _ => unreachable!(), |
85 | | - }; |
| 84 | + fn generate_binding(instance: &mut SolMacroGen) -> Result<()> { |
| 85 | + let input = instance.get_sol_input()?.normalize_json()?; |
86 | 86 |
|
87 | | - instance.expansion = Some(tokens); |
88 | | - } |
| 87 | + let SolInput { attrs: _, path: _, kind } = input; |
89 | 88 |
|
| 89 | + let tokens = match kind { |
| 90 | + SolInputKind::Sol(mut file) => { |
| 91 | + let sol_attr: syn::Attribute = syn::parse_quote! { |
| 92 | + #[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)] |
| 93 | + }; |
| 94 | + file.attrs.push(sol_attr); |
| 95 | + expand(file).wrap_err("failed to expand")? |
| 96 | + } |
| 97 | + _ => unreachable!(), |
| 98 | + }; |
| 99 | + |
| 100 | + instance.expansion = Some(tokens); |
90 | 101 | Ok(()) |
91 | 102 | } |
92 | 103 |
|
@@ -139,27 +150,28 @@ edition = "2021" |
139 | 150 | )?; |
140 | 151 |
|
141 | 152 | // Write src |
| 153 | + let parse_error = |name: &str| { |
| 154 | + format!("failed to parse generated tokens as an AST for {name};\nthis is likely a bug") |
| 155 | + }; |
142 | 156 | for instance in &self.instances { |
143 | | - let name = instance.name.to_lowercase(); |
144 | | - let contents = instance.expansion.as_ref().unwrap().to_string(); |
| 157 | + let contents = instance.expansion.as_ref().unwrap(); |
145 | 158 |
|
146 | | - if !single_file { |
147 | | - let path = src.join(format!("{name}.rs")); |
148 | | - let file = syn::parse_file(&contents)?; |
149 | | - let contents = prettyplease::unparse(&file); |
150 | | - |
151 | | - fs::write(path.clone(), contents).wrap_err("Failed to write file")?; |
152 | | - writeln!(&mut lib_contents, "pub mod {name};")?; |
153 | | - } else { |
| 159 | + let name = instance.name.to_lowercase(); |
| 160 | + let path = src.join(format!("{name}.rs")); |
| 161 | + let file = syn::parse2(contents.clone()) |
| 162 | + .wrap_err_with(|| parse_error(&format!("{}:{}", path.display(), name)))?; |
| 163 | + let contents = prettyplease::unparse(&file); |
| 164 | + if single_file { |
154 | 165 | write!(&mut lib_contents, "{contents}")?; |
| 166 | + } else { |
| 167 | + fs::write(path, contents).wrap_err("failed to write to file")?; |
| 168 | + writeln!(&mut lib_contents, "pub mod {name};")?; |
155 | 169 | } |
156 | 170 | } |
157 | 171 |
|
158 | 172 | let lib_path = src.join("lib.rs"); |
159 | | - let lib_file = syn::parse_file(&lib_contents)?; |
160 | | - |
| 173 | + let lib_file = syn::parse_file(&lib_contents).wrap_err_with(|| parse_error("lib.rs"))?; |
161 | 174 | let lib_contents = prettyplease::unparse(&lib_file); |
162 | | - |
163 | 175 | fs::write(lib_path, lib_contents).wrap_err("Failed to write lib.rs")?; |
164 | 176 |
|
165 | 177 | Ok(()) |
|
0 commit comments