Skip to content

Commit

Permalink
Simplify impl_enum_scale macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Nov 27, 2024
1 parent 3d1e4ff commit cb57ccd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 171 deletions.
10 changes: 5 additions & 5 deletions rust/chains/tw_polkadot/src/call_encoder/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ impl GenericBalances {
impl_enum_scale!(
#[derive(Clone, Debug)]
pub enum RewardDestination {
Staked,
Stash,
Controller,
Account(AccountId),
None,
Staked = 0x00,
Stash = 0x01,
Controller = 0x02,
Account(AccountId) = 0x03,
None = 0x04,
}
);

Expand Down
4 changes: 2 additions & 2 deletions rust/chains/tw_polkadot/src/call_encoder/polymesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl PolymeshBalances {
impl_enum_scale!(
#[derive(Clone, Debug)]
pub enum Signatory {
Identity(IdentityId),
Account(AccountId),
Identity(IdentityId) = 0x00,
Account(AccountId) = 0x01,
}
);

Expand Down
4 changes: 2 additions & 2 deletions rust/frameworks/tw_substrate/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ impl_enum_scale!(
#[derive(Clone, Copy, Debug, Default)]
pub enum CheckMetadataMode {
#[default]
Disabled,
Enabled,
Disabled = 0x00,
Enabled = 0x01,
}
);

Expand Down
6 changes: 3 additions & 3 deletions rust/frameworks/tw_substrate/src/extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub type BlockNumber = u32;
impl_enum_scale!(
#[derive(Clone, Debug)]
pub enum MultiSignature {
Ed25519(H512),
Sr25519(H512),
//Ecdsa([u8; 65]),
Ed25519(H512) = 0x0,
Sr25519(H512) = 0x1,
//Ecdsa([u8; 65]) = 0x2,
}
);

Expand Down
173 changes: 14 additions & 159 deletions rust/tw_scale/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,173 +63,28 @@ macro_rules! impl_enum_scale {
(
$(#[$enum_meta:meta])*
pub enum $enum_name:ident {
$($unparsed_variants:tt)*
}
$($rest:tt)*
) => {
$crate::impl_enum_scale! {
@normalize
enum {
// Enum type
{
$(#[$enum_meta])*
pub enum $enum_name
}
// Enum variants.
[]
// Variant counter.
[0]
// Variant encoding.
[]
__internal_unparsed_variants {
$($unparsed_variants)*
}
}
}
};
// Parse enum variants wihtout custom index.
(@normalize
enum {
// Enum type
{ $( $enum_type:tt )* }
// Enum variants.
[ $( $enum_variants:tt )* ]
// Variant counter.
[ $( $count_variant:tt )* ]
// Variant encoding.
[ $( $encode_variants:tt )* ]
__internal_unparsed_variants {
$(#[$variant_meta:meta])*
$variant_name:ident $(($variant_tuple_ty:ty))? $({
$(
$variant_field_name:ident : $variant_field_ty:ty
),+ $(,)?
})?,
$($unparsed_variants:tt)*
}
}
) => {
$crate::impl_enum_scale! {
@normalize
enum {
// Enum type
{ $( $enum_type )* }
// Enum variants.
[
$( $enum_variants )*
$(#[$variant_meta])*
$variant_name $(($variant_tuple_ty))? $({
$(
$variant_field_name : $variant_field_ty
),+
})?,
]
// Variant counter.
[ $( $count_variant )* + 1 ]
// Variant encoding.
[
$( $encode_variants )*
{
( $( $count_variant )* ): $variant_name $(($variant_tuple_ty))? $({
$(
$variant_field_name
),+
})?
}
]
__internal_unparsed_variants {
$($unparsed_variants)*
}
}
}
};
// Parse enum variants with custom index.
(@normalize
enum {
// Enum type
{ $( $enum_type:tt )* }
// Enum variants.
[ $( $enum_variants:tt )* ]
// Variant counter.
[ $( $count_variant:tt )* ]
// Variant encoding.
[ $( $encode_variants:tt )* ]
__internal_unparsed_variants {
$(
$(#[$variant_meta:meta])*
$variant_name:ident $(($variant_tuple_ty:ty))? $({
$(
$variant_field_name:ident : $variant_field_ty:ty
),+ $(,)?
})? = $index:expr,
$($unparsed_variants:tt)*
}
})? = $variant_index:expr,
)*
}
$($rest:tt)*
) => {
$crate::impl_enum_scale! {
@normalize
enum {
// Enum type
{ $( $enum_type )* }
// Enum variants.
[
$( $enum_variants )*
$(#[$enum_meta])*
#[repr(u8)]
pub enum $enum_name {
$(
$(#[$variant_meta])*
$variant_name $(($variant_tuple_ty))? $({
$(
$variant_field_name : $variant_field_ty
),+
})? = $index,
]
// Variant counter.
[ $index + 1 ]
// Variant encoding.
[
$( $encode_variants )*
{
( $index ): $variant_name $(($variant_tuple_ty))? $({
$(
$variant_field_name
),+
})?
}
]
__internal_unparsed_variants {
$($unparsed_variants)*
}
}
}
};
// Finished parsing enum variants.
(@normalize
enum {
// Enum type
{
$(#[$enum_meta:meta])*
pub enum $enum_name:ident
}
// Enum variants.
[ $( $enum_variants:tt )* ]
// Variant counter.
[ $( $count_variant:tt )* ]
// Variant encoding.
[
$(
{
( $( $variant_index:tt )* ): $variant_name:ident $(($variant_tuple_ty:ty))? $({
$(
$variant_field_name:ident
),+
})?
}
)*
]
__internal_unparsed_variants { }
}
) => {
$(#[$enum_meta])*
#[repr(u8)]
pub enum $enum_name {
$( $enum_variants)*
})? = $variant_index,
)*
}

impl $crate::ToScale for $enum_name {
Expand All @@ -240,7 +95,7 @@ macro_rules! impl_enum_scale {
$($variant_field_name),+
})? => {
// variant index.
out.push(($( $variant_index)*) as u8);
out.push($variant_index as u8);
// Encode tuple variants.
$(
$crate::replace_expr!($variant_tuple_ty {
Expand Down Expand Up @@ -299,13 +154,13 @@ mod tests {
#[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum TestEnum {
#[default]
Variant0,
Variant1(u8),
Variant0 = 0,
Variant1(u8) = 1,
Variant10 = 10,
Struct {
id: u8,
id2: u8,
},
} = 11,
}
);

Expand Down

0 comments on commit cb57ccd

Please sign in to comment.