Skip to content

Commit

Permalink
add type_check sol attr
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Dec 30, 2023
1 parent bb879ec commit dbff597
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/sol-macro/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct SolAttrs {

pub bytecode: Option<LitStr>,
pub deployed_bytecode: Option<LitStr>,

pub type_check: Option<LitStr>,
}

impl SolAttrs {
Expand Down Expand Up @@ -146,6 +148,8 @@ impl SolAttrs {

bytecode => bytes()?,
deployed_bytecode => bytes()?,

type_check => lit()?,
};
Ok(())
})?;
Expand Down Expand Up @@ -322,5 +326,10 @@ mod tests {
#[sol(bytecode = "xyz")] => Err("expected hex literal"),
#[sol(bytecode = "123")] => Err("expected even number of hex digits"),
}

type_check {
#[sol(type_check = "my_function")] => Ok(sol_attrs! {type_check: parse_quote!("my_function")} ),
#[sol(type_check = "my_function1")] #[sol(type_check = "my_function2")] => Err(DUPLICATE_ERROR),
}
}
}
11 changes: 9 additions & 2 deletions crates/sol-macro/src/expand/udt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ use syn::Result;
pub(super) fn expand(cx: &ExpCtxt<'_>, udt: &ItemUdt) -> Result<TokenStream> {
let ItemUdt { name, ty, attrs, .. } = udt;

let (_sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
let (sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
cx.type_derives(&mut attrs, Some(ty), true);

let type_check = if let Some(lit_str) = sol_attrs.type_check {
let func_path: syn::Path = syn::parse_str(&lit_str.value()).unwrap();
quote! { #func_path }
} else {
quote! { ::alloy_sol_types::private::just_ok }
};

let underlying = expand_type(ty);
let tokens = quote! {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand Down Expand Up @@ -90,7 +97,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, udt: &ItemUdt) -> Result<TokenStream> {
#[inline]
fn type_check(token: &Self::Token<'_>) -> ::alloy_sol_types::Result<()> {
<#underlying as ::alloy_sol_types::SolType>::type_check(token)?;
::alloy_sol_types::private::just_ok(token)
#type_check(token)
}

#[inline]
Expand Down
2 changes: 2 additions & 0 deletions crates/sol-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ mod json;
/// will emit a `static` item with the specified bytes.
/// - `deployed_bytecode = <hex string literal>`: specifies the deployed bytecode of a contract.
/// This will emit a `static` item with the specified bytes.
/// - `type_check = <string literal>`: specifies a function to be used to check an User Defined
/// Type.
///
/// ### Structs and enums
///
Expand Down

0 comments on commit dbff597

Please sign in to comment.