From 22a450822ea808f22309b6e266af679b804fc13e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 27 Dec 2023 20:49:03 +0100 Subject: [PATCH] chore: add enum helper functions (#5865) --- crates/rpc/rpc-builder/src/lib.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index b2c7ff3af9..7d848c1eea 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -151,7 +151,7 @@ use jsonrpsee::{ Methods, RpcModule, }; use serde::{Deserialize, Serialize, Serializer}; -use strum::{AsRefStr, EnumVariantNames, ParseError, VariantNames}; +use strum::{AsRefStr, EnumIter, EnumVariantNames, IntoStaticStr, ParseError, VariantNames}; use tower::layer::util::{Identity, Stack}; use tower_http::cors::CorsLayer; use tracing::{instrument, trace}; @@ -739,7 +739,19 @@ impl fmt::Display for RpcModuleSelection { } /// Represents RPC modules that are supported by reth -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, AsRefStr, EnumVariantNames, Deserialize)] +#[derive( + Debug, + Clone, + Copy, + Eq, + PartialEq, + Hash, + AsRefStr, + IntoStaticStr, + EnumVariantNames, + EnumIter, + Deserialize, +)] #[serde(rename_all = "snake_case")] #[strum(serialize_all = "kebab-case")] pub enum RethRpcModule { @@ -777,6 +789,18 @@ impl RethRpcModule { pub const fn all_variants() -> &'static [&'static str] { Self::VARIANTS } + + /// Returns all variants of the enum + pub fn modules() -> impl IntoIterator { + use strum::IntoEnumIterator; + Self::iter() + } + + /// Returns the string representation of the module. + #[inline] + pub fn as_str(&self) -> &'static str { + self.into() + } } impl FromStr for RethRpcModule {