Skip to content

Commit

Permalink
feat(sol-macro): provide a way to override import paths for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 13, 2024
1 parent 5f07b82 commit 3c1ac68
Show file tree
Hide file tree
Showing 19 changed files with 586 additions and 384 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/*"]
members = ["crates/*", "tests/*"]
resolver = "2"

[workspace.package]
Expand Down
21 changes: 18 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#[doc(inline)]
pub use alloy_primitives as primitives;
#[doc(no_inline)]
pub use alloy_primitives::hex;
pub use primitives::hex;

#[cfg(feature = "dyn-abi")]
#[doc(inline)]
Expand All @@ -32,10 +32,25 @@ pub use alloy_json_abi as json_abi;
#[cfg(feature = "sol-types")]
#[doc(inline)]
pub use alloy_sol_types as sol_types;
#[cfg(feature = "sol-types")]
#[cfg(all(doc, feature = "sol-types"))]
#[doc(no_inline)]
pub use alloy_sol_types::sol;
pub use sol_types::sol;

#[cfg(feature = "rlp")]
#[doc(inline)]
pub use alloy_rlp as rlp;

/// [`sol!`](alloy_sol_types::sol!) macro wrapper to route imports to the correct crate.
///
/// See [`sol!`](alloy_sol_types::sol!) for the actual macro documentation.
#[cfg(all(not(doc), feature = "sol-types"))]
#[doc(hidden)]
#[macro_export]
macro_rules! sol {
($($t:tt)*) => {
$crate::sol_types::sol! {
#![sol(alloy_sol_types = $crate::sol_types)]
$($t)*
}
};
}
25 changes: 25 additions & 0 deletions crates/core/tests/sol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg(feature = "sol-types")]

use alloy_core::sol;

sol! {
struct MyStruct {
uint32 a;
uint32 b;
}

function myFunction(uint32 a, uint32 b) returns(uint32);
event MyEvent(uint32 a, uint32 b);
error MyError(uint32 a, uint32 b);

contract MyContract {
struct MyOtherStruct {
uint32 a;
uint32 b;
}

function myOtherFunction(uint32 a, uint32 b) returns(uint32);
event MyOtherEvent(uint32 a, uint32 b);
error MyOtherError(uint32 a, uint32 b);
}
}
19 changes: 19 additions & 0 deletions crates/sol-macro/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub struct SolAttrs {
pub extra_methods: Option<bool>,
pub docs: Option<bool>,

pub alloy_sol_types: Option<Path>,
pub alloy_contract: Option<Path>,

// TODO: Implement
pub rename: Option<LitStr>,
// TODO: Implement
Expand Down Expand Up @@ -121,6 +124,9 @@ impl SolAttrs {
}
};

// `path = <path>`
let path = || meta.value()?.parse::<Path>();

// `path = "<str>"`
let lit = || meta.value()?.parse::<LitStr>();

Expand All @@ -145,6 +151,9 @@ impl SolAttrs {
extra_methods => bool()?,
docs => bool()?,

alloy_sol_types => path()?,
alloy_contract => path()?,

rename => lit()?,
rename_all => CasingStyle::from_lit(&lit()?)?,

Expand Down Expand Up @@ -313,6 +322,16 @@ mod tests {
#[sol(rpc)] => Ok(sol_attrs! { rpc: true }),
#[sol(rpc = true)] => Ok(sol_attrs! { rpc: true }),
#[sol(rpc = false)] => Ok(sol_attrs! { rpc: false }),

#[sol(alloy_sol_types)] => Err("expected `=`"),
#[sol(alloy_sol_types = alloy_core::sol_types)] => Ok(sol_attrs! { alloy_sol_types: parse_quote!(alloy_core::sol_types) }),
#[sol(alloy_sol_types = ::alloy_core::sol_types)] => Ok(sol_attrs! { alloy_sol_types: parse_quote!(::alloy_core::sol_types) }),
#[sol(alloy_sol_types = alloy::sol_types)] => Ok(sol_attrs! { alloy_sol_types: parse_quote!(alloy::sol_types) }),
#[sol(alloy_sol_types = ::alloy::sol_types)] => Ok(sol_attrs! { alloy_sol_types: parse_quote!(::alloy::sol_types) }),

#[sol(alloy_contract)] => Err("expected `=`"),
#[sol(alloy_contract = alloy::contract)] => Ok(sol_attrs! { alloy_contract: parse_quote!(alloy::contract) }),
#[sol(alloy_contract = ::alloy::contract)] => Ok(sol_attrs! { alloy_contract: parse_quote!(::alloy::contract) }),
}

rename {
Expand Down
Loading

0 comments on commit 3c1ac68

Please sign in to comment.