Skip to content

Commit

Permalink
fix(forge-bind): allow attrs and mod single_file imports (#8171)
Browse files Browse the repository at this point in the history
* fix(forge-bind): allow attrs and mod single_file imports

* fmt nit

* allow rustdoc::all

* fix: file consistenct check

* fix clippy
  • Loading branch information
yash-atreya committed Jun 15, 2024
1 parent 41a6945 commit 47b2ce2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 55 deletions.
1 change: 1 addition & 0 deletions crates/anvil/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::octal_escapes)]
mod abi;
mod anvil;
mod anvil_api;
Expand Down
88 changes: 33 additions & 55 deletions crates/sol-macro-gen/src/sol_macro_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,15 @@ edition = "2021"
fs::write(cargo_toml_path, toml_contents).wrap_err("Failed to write Cargo.toml")?;

let mut lib_contents = String::new();
if single_file {
write!(
&mut lib_contents,
r#"#![allow(unused_imports, clippy::all)]
//! This module contains the sol! generated bindings for solidity contracts.
//! This is autogenerated code.
//! Do not manually edit these files.
//! These files may be overwritten by the codegen system at any time."#
)?;
} else {
write!(
&mut lib_contents,
r#"#![allow(unused_imports)]
"#
)?;
};
write!(
&mut lib_contents,
r#"#![allow(unused_imports, clippy::all, rustdoc::all)]
//! This module contains the sol! generated bindings for solidity contracts.
//! This is autogenerated code.
//! Do not manually edit these files.
//! These files may be overwritten by the codegen system at any time.
"#
)?;

// Write src
for instance in &self.instances {
Expand Down Expand Up @@ -179,7 +172,7 @@ edition = "2021"

let _ = fs::create_dir_all(bindings_path);

let mut mod_contents = r#"#![allow(clippy::all)]
let mut mod_contents = r#"#![allow(unused_imports, clippy::all, rustdoc::all)]
//! This module contains the sol! generated bindings for solidity contracts.
//! This is autogenerated code.
//! Do not manually edit these files.
Expand All @@ -190,16 +183,14 @@ edition = "2021"
for instance in &self.instances {
let name = instance.name.to_lowercase();
if !single_file {
// Module
write!(
mod_contents,
r#"pub mod {};
"#,
instance.name.to_lowercase()
)?;
let mut contents =
r#"//! This module was autogenerated by the alloy sol!.
//! More information can be found here <https://docs.rs/alloy-sol-macro/latest/alloy_sol_macro/macro.sol.html>.
"#.to_string();
let mut contents = String::new();

write!(contents, "{}", instance.expansion.as_ref().unwrap())?;
let file = syn::parse_file(&contents)?;
Expand All @@ -208,13 +199,8 @@ edition = "2021"
fs::write(bindings_path.join(format!("{}.rs", name)), contents)
.wrap_err("Failed to write file")?;
} else {
let mut contents = format!(
r#"//! This module was autogenerated by the alloy sol!.
//! More information can be found here <https://docs.rs/alloy-sol-macro/latest/alloy_sol_macro/macro.sol.html>.
pub use {}::*;
"#,
name
);
// Single File
let mut contents = String::new();
write!(contents, "{}\n\n", instance.expansion.as_ref().unwrap())?;
write!(mod_contents, "{}", contents)?;
}
Expand Down Expand Up @@ -250,29 +236,23 @@ edition = "2021"
}

let mut super_contents = String::new();
if is_mod {
// mod.rs
write!(
&mut super_contents,
r#"#![allow(clippy::all)]
//! This module contains the sol! generated bindings for solidity contracts.
//! This is autogenerated code.
//! Do not manually edit these files.
//! These files may be overwritten by the codegen system at any time.
"#
)?;
} else {
// lib.rs
write!(
&mut super_contents,
r#"#![allow(unused_imports)]
write!(
&mut super_contents,
r#"#![allow(unused_imports, clippy::all, rustdoc::all)]
//! This module contains the sol! generated bindings for solidity contracts.
//! This is autogenerated code.
//! Do not manually edit these files.
//! These files may be overwritten by the codegen system at any time.
"#
)?;
};
)?;
if !single_file {
for instance in &self.instances {
let name = instance.name.to_lowercase();
let path = crate_path.join(format!("src/{}.rs", name));
let path = if is_mod {
crate_path.join(format!("{}.rs", name))
} else {
crate_path.join(format!("src/{}.rs", name))
};
let tokens = instance
.expansion
.as_ref()
Expand All @@ -281,18 +261,16 @@ edition = "2021"

self.check_file_contents(&path, &tokens)?;

if !is_mod {
write!(
&mut super_contents,
r#"pub mod {};
write!(
&mut super_contents,
r#"pub mod {};
"#,
name
)?;
}
name
)?;
}

let super_path =
if is_mod { crate_path.join("src/mod.rs") } else { crate_path.join("src/lib.rs") };
if is_mod { crate_path.join("mod.rs") } else { crate_path.join("src/lib.rs") };
self.check_file_contents(&super_path, &super_contents)?;
}

Expand Down

0 comments on commit 47b2ce2

Please sign in to comment.