Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add some more debugging to forge bind #9345

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions crates/sol-macro-gen/src/sol_macro_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use alloy_sol_macro_expander::expand::expand;
use alloy_sol_macro_input::{SolInput, SolInputKind};
use eyre::{Context, Ok, OptionExt, Result};
use eyre::{Context, OptionExt, Result};
use foundry_common::fs;
use proc_macro2::{Span, TokenStream};
use std::{
Expand Down Expand Up @@ -39,7 +39,7 @@ impl SolMacroGen {
#path
};

let sol_input: SolInput = syn::parse2(tokens).wrap_err("Failed to parse SolInput {e}")?;
let sol_input: SolInput = syn::parse2(tokens).wrap_err("failed to parse input")?;

Ok(sol_input)
}
Expand Down Expand Up @@ -69,24 +69,35 @@ impl MultiSolMacroGen {

pub fn generate_bindings(&mut self) -> Result<()> {
for instance in &mut self.instances {
let input = instance.get_sol_input()?.normalize_json()?;
Self::generate_binding(instance).wrap_err_with(|| {
format!(
"failed to generate bindings for {}:{}",
instance.path.display(),
instance.name
)
})?;
}

let SolInput { attrs: _attrs, path: _path, kind } = input;
Ok(())
}

let tokens = match kind {
SolInputKind::Sol(mut file) => {
let sol_attr: syn::Attribute = syn::parse_quote! {
#[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)]
};
file.attrs.push(sol_attr);
expand(file).wrap_err("Failed to expand SolInput")?
}
_ => unreachable!(),
};
fn generate_binding(instance: &mut SolMacroGen) -> Result<()> {
let input = instance.get_sol_input()?.normalize_json()?;

instance.expansion = Some(tokens);
}
let SolInput { attrs: _, path: _, kind } = input;

let tokens = match kind {
SolInputKind::Sol(mut file) => {
let sol_attr: syn::Attribute = syn::parse_quote! {
#[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)]
};
file.attrs.push(sol_attr);
expand(file).wrap_err("failed to expand")?
}
_ => unreachable!(),
};

instance.expansion = Some(tokens);
Ok(())
}

Expand Down Expand Up @@ -139,27 +150,28 @@ edition = "2021"
)?;

// Write src
let parse_error = |name: &str| {
format!("failed to parse generated tokens as an AST for {name};\nthis is likely a bug")
};
for instance in &self.instances {
let name = instance.name.to_lowercase();
let contents = instance.expansion.as_ref().unwrap().to_string();
let contents = instance.expansion.as_ref().unwrap();

if !single_file {
let path = src.join(format!("{name}.rs"));
let file = syn::parse_file(&contents)?;
let contents = prettyplease::unparse(&file);

fs::write(path.clone(), contents).wrap_err("Failed to write file")?;
writeln!(&mut lib_contents, "pub mod {name};")?;
} else {
let name = instance.name.to_lowercase();
let path = src.join(format!("{name}.rs"));
let file = syn::parse2(contents.clone())
.wrap_err_with(|| parse_error(&format!("{}:{}", path.display(), name)))?;
let contents = prettyplease::unparse(&file);
if single_file {
write!(&mut lib_contents, "{contents}")?;
} else {
fs::write(path, contents).wrap_err("failed to write to file")?;
writeln!(&mut lib_contents, "pub mod {name};")?;
}
}

let lib_path = src.join("lib.rs");
let lib_file = syn::parse_file(&lib_contents)?;

let lib_file = syn::parse_file(&lib_contents).wrap_err_with(|| parse_error("lib.rs"))?;
let lib_contents = prettyplease::unparse(&lib_file);

fs::write(lib_path, lib_contents).wrap_err("Failed to write lib.rs")?;

Ok(())
Expand Down
Loading