reth_ethereum_forks/hardfork/
macros.rs

1/// Macro that defines different variants of a chain specific enum. See [`crate::Hardfork`] as an
2/// example.
3#[macro_export]
4macro_rules! hardfork {
5    ($(#[$enum_meta:meta])* $enum:ident { $( $(#[$meta:meta])* $variant:ident ),* $(,)? }) => {
6        $(#[$enum_meta])*
7        #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8        #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
9        pub enum $enum {
10            $( $(#[$meta])* $variant ),*
11        }
12
13        impl $enum {
14            /// Returns variant as `str`.
15            pub const fn name(&self) -> &'static str {
16                match self {
17                    $( $enum::$variant => stringify!($variant), )*
18                }
19            }
20
21            /// Boxes `self` and returns it as `Box<dyn Hardfork>`.
22            pub fn boxed(self) -> Box<dyn Hardfork> {
23                Box::new(self)
24            }
25        }
26
27        impl FromStr for $enum {
28            type Err = String;
29
30            fn from_str(s: &str) -> Result<Self, Self::Err> {
31                match s.to_lowercase().as_str() {
32                    $(
33                        s if s == stringify!($variant).to_lowercase() => Ok($enum::$variant),
34                    )*
35                    _ => return Err(format!("Unknown hardfork: {s}")),
36                }
37            }
38        }
39
40        impl Hardfork for $enum {
41            fn name(&self) -> &'static str {
42                self.name()
43            }
44        }
45
46        impl Display for $enum {
47            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
48                write!(f, "{self:?}")
49            }
50        }
51    }
52}