Skip to content

Commit 4817fc3

Browse files
authored
fix(tx-macro): correctly generate uppercase aliases (#3053)
* fix: correctly generate uppercase aliases * clippy
1 parent f3fae7a commit 4817fc3

File tree

4 files changed

+63
-56
lines changed

4 files changed

+63
-56
lines changed

crates/tx-macros/Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ quote = "1.0"
3131
proc-macro2 = "1.0"
3232
darling = "0.21"
3333

34-
alloy-primitives.workspace = true
35-
3634
[features]
37-
arbitrary = [
38-
"alloy-primitives/arbitrary"
39-
]
35+
arbitrary = []
4036
serde = [
41-
"alloy-primitives/serde",
4237
"darling/serde"
4338
]

crates/tx-macros/src/expand.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::parse::{GroupedVariants, VariantKind};
2-
use alloy_primitives::U8;
32
use proc_macro2::TokenStream;
43
use quote::{quote, ToTokens};
54
use syn::{Ident, Path};
@@ -811,25 +810,12 @@ impl Expander {
811810
.iter()
812811
.map(|v| {
813812
let name = &v.name;
814-
let VariantKind::Typed(ty_value) = v.kind else { unreachable!() };
815-
816-
let tx_type = U8::from(ty_value);
817-
let rename = format!("0x{tx_type:x}");
818-
819-
let mut aliases = vec![];
820-
// Add alias for single digit hex values (e.g., "0x0" for "0x00")
821-
if rename.len() == 3 {
822-
aliases.push(format!("0x0{}", rename.chars().last().unwrap()));
823-
}
824-
825-
// Add alias for uppercase values (e.g., "0x7E" for "0x7e")
826-
if rename != rename.to_uppercase() {
827-
aliases.push(rename.to_uppercase());
828-
}
829813

830814
// Custom type or extract from wrapper
831815
let inner_type = v.inner_type();
832816

817+
let (rename, aliases) = v.kind.serde_tag_and_aliases();
818+
833819
quote! {
834820
#[serde(rename = #rename, #(alias = #aliases,)*)]
835821
#name(#inner_type)

crates/tx-macros/src/parse.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ pub(crate) enum VariantKind {
7373
Flattened,
7474
}
7575

76+
impl VariantKind {
77+
/// Returns serde transaction enum tag and aliases.
78+
pub(crate) fn serde_tag_and_aliases(&self) -> (String, Vec<String>) {
79+
let Self::Typed(ty) = self else { return Default::default() };
80+
81+
let tx_type_hex = format!("{ty:x}");
82+
83+
let mut aliases = vec![];
84+
// Add alias for single digit hex values (e.g., "0x0" for "0x00")
85+
if tx_type_hex.len() == 1 {
86+
aliases.push(format!("0x0{}", tx_type_hex));
87+
}
88+
89+
// Add alias for uppercase values (e.g., "0x7E" for "0x7e")
90+
if tx_type_hex != tx_type_hex.to_uppercase() {
91+
aliases.push(format!("0x{}", tx_type_hex.to_uppercase()));
92+
}
93+
94+
(format!("0x{tx_type_hex}"), aliases)
95+
}
96+
}
97+
7698
/// Processed variant information.
7799
#[derive(Debug, Clone)]
78100
pub(crate) struct ProcessedVariant {
@@ -218,3 +240,24 @@ impl GroupedVariants {
218240
self.all.iter().map(|v| &v.ty).collect()
219241
}
220242
}
243+
244+
#[cfg(test)]
245+
mod tests {
246+
use super::*;
247+
248+
#[test]
249+
fn serde_tag() {
250+
assert_eq!(
251+
VariantKind::Typed(126).serde_tag_and_aliases(),
252+
("0x7e".to_string(), vec!["0x7E".to_string()])
253+
);
254+
assert_eq!(
255+
VariantKind::Typed(1).serde_tag_and_aliases(),
256+
("0x1".to_string(), vec!["0x01".to_string()])
257+
);
258+
assert_eq!(
259+
VariantKind::Typed(10).serde_tag_and_aliases(),
260+
("0xa".to_string(), vec!["0x0a".to_string(), "0xA".to_string()])
261+
);
262+
}
263+
}

crates/tx-macros/src/serde.rs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::parse::{GroupedVariants, ProcessedVariant, VariantKind};
2-
use alloy_primitives::U8;
1+
use crate::parse::{GroupedVariants, ProcessedVariant};
32
use proc_macro2::TokenStream;
43
use quote::quote;
54
use syn::{Ident, Path};
@@ -87,44 +86,28 @@ impl<'a> SerdeGenerator<'a> {
8786
self.variants
8887
.typed
8988
.iter()
90-
.filter_map(|v| {
89+
.map(|v| {
9190
let ProcessedVariant { name, ty, kind, serde_attrs, typed: _, doc_attrs: _ } = v;
9291

93-
if let VariantKind::Typed(tx_type) = kind {
94-
let tx_type = U8::from(*tx_type);
95-
let rename = format!("0x{tx_type:x}");
92+
let (rename, aliases) = kind.serde_tag_and_aliases();
9693

97-
let mut aliases = vec![];
98-
// Add alias for single digit hex values (e.g., "0x0" for "0x00")
99-
if rename.len() == 3 {
100-
aliases.push(format!("0x0{}", rename.chars().last().unwrap()));
94+
// Special handling for legacy transactions
95+
let maybe_with = if v.is_legacy() {
96+
let alloy_consensus = &self.alloy_consensus;
97+
let path = quote! {
98+
#alloy_consensus::transaction::signed_legacy_serde
10199
}
100+
.to_string();
101+
quote! { with = #path, }
102+
} else {
103+
quote! {}
104+
};
102105

103-
// Add alias for uppercase values (e.g., "0x7E" for "0x7e")
104-
if rename != rename.to_uppercase() {
105-
aliases.push(rename.to_uppercase());
106-
}
106+
let maybe_other = serde_attrs.clone().unwrap_or_default();
107107

108-
// Special handling for legacy transactions
109-
let maybe_with = if v.is_legacy() {
110-
let alloy_consensus = &self.alloy_consensus;
111-
let path = quote! {
112-
#alloy_consensus::transaction::signed_legacy_serde
113-
}
114-
.to_string();
115-
quote! { with = #path, }
116-
} else {
117-
quote! {}
118-
};
119-
120-
let maybe_other = serde_attrs.clone().unwrap_or_default();
121-
122-
Some(quote! {
123-
#[serde(rename = #rename, #(alias = #aliases,)* #maybe_with #maybe_other)]
124-
#name(#ty)
125-
})
126-
} else {
127-
None
108+
quote! {
109+
#[serde(rename = #rename, #(alias = #aliases,)* #maybe_with #maybe_other)]
110+
#name(#ty)
128111
}
129112
})
130113
.collect()

0 commit comments

Comments
 (0)