Skip to content

Commit 7b7c28d

Browse files
author
Grigoriy Simonov
committed
fix: generic const warning
1 parent b57866d commit 7b7c28d

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

procedural/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,18 @@ fn pascal_ident_to_call(ident: &Ident) -> Ident {
129129
let name = format!("{ident}Call");
130130
Ident::new(&name, ident.span())
131131
}
132+
fn pascal_ident_to_consts_mod(ident: &Ident) -> Ident {
133+
let name = ident.to_string();
134+
let name = cases::snakecase::to_snake_case(&name);
135+
let name = format!("{name}_constants");
136+
Ident::new(&name, ident.span())
137+
}
132138
fn snake_ident_to_pascal(ident: &Ident) -> Ident {
133139
let name = ident.to_string();
134140
let name = cases::pascalcase::to_pascal_case(&name);
135141
Ident::new(&name, ident.span())
136142
}
143+
137144
fn snake_ident_to_screaming(ident: &Ident) -> Ident {
138145
let name = ident.to_string();
139146
let name = cases::screamingsnakecase::to_screaming_snake_case(&name);

procedural/src/solidity_interface.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use syn::{
1717

1818
use crate::{
1919
parse_ident_from_pat, parse_ident_from_path, parse_path, parse_path_segment,
20-
pascal_ident_to_call, snake_ident_to_pascal, snake_ident_to_screaming,
20+
pascal_ident_to_call, pascal_ident_to_consts_mod, snake_ident_to_pascal,
21+
snake_ident_to_screaming,
2122
};
2223

2324
struct Is {
@@ -453,6 +454,7 @@ mod kw {
453454
/// Rust methods are parsed into this structure when Solidity code is generated
454455
struct Method {
455456
name: Ident,
457+
consts_mod_name: Ident,
456458
camel_name: String,
457459
pascal_name: Ident,
458460
screaming_name: Ident,
@@ -466,7 +468,7 @@ struct Method {
466468
enum_attrs: Vec<TokenStream>,
467469
}
468470
impl Method {
469-
fn try_from(value: &mut ImplItemMethod, variant_attrs: &BTreeSet<Ident>) -> syn::Result<Self> {
471+
fn try_from(value: &mut ImplItemMethod, interface_info: &InterfaceInfo) -> syn::Result<Self> {
470472
let mut info = MethodInfo {
471473
rename_selector: None,
472474
hide: false,
@@ -490,7 +492,7 @@ impl Method {
490492
_ => unreachable!(),
491493
};
492494
docs.push(value);
493-
} else if variant_attrs.contains(ident) {
495+
} else if interface_info.enum_variant_attrs.contains(ident) {
494496
let path = &attr.path;
495497
let tokens = &attr.tokens;
496498
extra_enum_attrs.push(quote! {#path #tokens});
@@ -561,6 +563,7 @@ impl Method {
561563

562564
Ok(Self {
563565
name: ident.clone(),
566+
consts_mod_name: pascal_ident_to_consts_mod(&interface_info.name),
564567
camel_name,
565568
pascal_name: snake_ident_to_pascal(ident),
566569
screaming_name: snake_ident_to_screaming(ident),
@@ -611,11 +614,11 @@ impl Method {
611614
let custom_signature = self.expand_custom_signature();
612615
quote! {
613616
const #screaming_name_signature: ::evm_coder::custom_signature::SignatureUnit = #custom_signature;
614-
const #screaming_name: ::evm_coder::types::Bytes4 = {
617+
pub const #screaming_name: ::evm_coder::types::Bytes4 = {
615618
let mut sum = ::evm_coder::sha3_const::Keccak256::new();
616619
let mut pos = 0;
617-
while pos < Self::#screaming_name_signature.len {
618-
sum = sum.update(&[Self::#screaming_name_signature.data[pos]; 1]);
620+
while pos < #screaming_name_signature.len {
621+
sum = sum.update(&[#screaming_name_signature.data[pos]; 1]);
619622
pos += 1;
620623
}
621624
let a = sum.finalize();
@@ -626,8 +629,9 @@ impl Method {
626629

627630
fn expand_interface_id(&self) -> proc_macro2::TokenStream {
628631
let screaming_name = &self.screaming_name;
632+
let consts_mod_name = &self.consts_mod_name;
629633
quote! {
630-
interface_id ^= u32::from_be_bytes(Self::#screaming_name.0);
634+
interface_id ^= u32::from_be_bytes(#consts_mod_name::#screaming_name.0);
631635
}
632636
}
633637

@@ -725,6 +729,7 @@ impl Method {
725729

726730
fn expand_solidity_function(&self) -> proc_macro2::TokenStream {
727731
let camel_name = &self.camel_name;
732+
let consts_mod_name = &self.consts_mod_name;
728733
let mutability = match self.mutability {
729734
Mutability::Mutable => quote! {SolidityMutability::Mutable},
730735
Mutability::View => quote! { SolidityMutability::View },
@@ -747,7 +752,7 @@ impl Method {
747752
SolidityFunction {
748753
docs: &[#(#docs),*],
749754
hide: #hide,
750-
selector: u32::from_be_bytes(Self::#screaming_name.0),
755+
selector: u32::from_be_bytes(#consts_mod_name::#screaming_name.0),
751756
custom_signature: #custom_signature,
752757
name: #camel_name,
753758
mutability: #mutability,
@@ -820,7 +825,7 @@ impl SolidityInterface {
820825

821826
for item in &mut value.items {
822827
if let ImplItem::Method(method) = item {
823-
methods.push(Method::try_from(method, &info.enum_variant_attrs)?);
828+
methods.push(Method::try_from(method, &info)?);
824829
}
825830
}
826831
let mut docs = vec![];
@@ -858,6 +863,7 @@ impl SolidityInterface {
858863
let name = self.name;
859864

860865
let solidity_name = self.info.name.to_string();
866+
let call_constants_mod_name = pascal_ident_to_consts_mod(&self.info.name);
861867
let call_name = pascal_ident_to_call(&self.info.name);
862868
let generics = self.generics;
863869
let gen_ref = generics_reference(&generics);
@@ -952,10 +958,15 @@ impl SolidityInterface {
952958

953959
#expect_selector
954960

955-
impl #gen_ref #call_name #gen_ref {
961+
mod #call_constants_mod_name {
962+
use super::*;
963+
956964
#(
957965
#consts
958966
)*
967+
}
968+
969+
impl #gen_ref #call_name #gen_ref {
959970
/// Return this call ERC165 selector
960971
pub const fn interface_id() -> ::evm_coder::types::Bytes4 {
961972
let mut interface_id = 0;

0 commit comments

Comments
 (0)