From 754431d1cc935021ae3acd3ad3e88406bab81ac1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:23:09 +0200 Subject: [PATCH] feat(rpc-types-engine): use strum for ClientCode (#1386) * feat(rpc-types-engine): use strum for ClientCode * fix: derive feature --- Cargo.toml | 1 + crates/rpc-types-engine/Cargo.toml | 3 +- crates/rpc-types-engine/src/identification.rs | 52 +++++-------------- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42061dd27b0..48f38fee93d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,6 +138,7 @@ thiserror = "1.0" thiserror-no-std = "2.0.2" url = "2.5" derive_more = { version = "1.0.0", default-features = false } +strum = { version = "0.26", default-features = false } http = "1.1.0" jsonwebtoken = "9.3.0" diff --git a/crates/rpc-types-engine/Cargo.toml b/crates/rpc-types-engine/Cargo.toml index 9b58bc0973f..f33d895cd30 100644 --- a/crates/rpc-types-engine/Cargo.toml +++ b/crates/rpc-types-engine/Cargo.toml @@ -27,6 +27,7 @@ alloy-eips = { workspace = true, features = ["serde"] } # misc derive_more = { workspace = true, features = ["display"] } +strum = { workspace = true, features = ["derive"] } # serde alloy-serde = { workspace = true, optional = true } @@ -45,7 +46,7 @@ jsonwebtoken = { workspace = true, optional = true } [features] default = ["jwt", "std", "serde"] -std = ["alloy-consensus/std", "derive_more/std"] +std = ["alloy-consensus/std", "derive_more/std", "strum/std"] serde = ["dep:serde", "dep:alloy-serde"] jwt = ["dep:jsonwebtoken", "dep:rand"] jsonrpsee-types = ["dep:jsonrpsee-types"] diff --git a/crates/rpc-types-engine/src/identification.rs b/crates/rpc-types-engine/src/identification.rs index ffb1eb18ec9..e8fc9b348ba 100644 --- a/crates/rpc-types-engine/src/identification.rs +++ b/crates/rpc-types-engine/src/identification.rs @@ -1,12 +1,14 @@ //! Client identification: -use alloc::string::{String, ToString}; -use core::str::FromStr; +use alloc::string::String; /// This enum defines a standard for specifying a client with just two letters. Clients teams which /// have a code reserved in this list MUST use this code when identifying themselves. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] +#[derive(strum::IntoStaticStr)] // Into<&'static str>, AsRef, fmt::Display and serde::Serialize +#[derive(strum::EnumString)] // FromStr, TryFrom<&str> +#[non_exhaustive] pub enum ClientCode { /// Besu BU, @@ -38,22 +40,8 @@ pub enum ClientCode { impl ClientCode { /// Returns the client identifier as str. - pub const fn as_str(&self) -> &'static str { - match self { - Self::BU => "BU", - Self::EJ => "EJ", - Self::EG => "EG", - Self::GE => "GE", - Self::GR => "GR", - Self::LH => "LH", - Self::LS => "LS", - Self::NM => "NM", - Self::NB => "NB", - Self::TE => "TE", - Self::TK => "TK", - Self::PM => "PM", - Self::RH => "RH", - } + pub fn as_str(&self) -> &'static str { + (*self).into() } /// Returns the human readable client name for the given code. @@ -76,32 +64,16 @@ impl ClientCode { } } -impl FromStr for ClientCode { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "BU" => Ok(Self::BU), - "EJ" => Ok(Self::EJ), - "EG" => Ok(Self::EG), - "GE" => Ok(Self::GE), - "GR" => Ok(Self::GR), - "LH" => Ok(Self::LH), - "LS" => Ok(Self::LS), - "NM" => Ok(Self::NM), - "NB" => Ok(Self::NB), - "TE" => Ok(Self::TE), - "TK" => Ok(Self::TK), - "PM" => Ok(Self::PM), - "RH" => Ok(Self::RH), - s => Err(s.to_string()), - } +#[cfg(feature = "serde")] +impl serde::Serialize for ClientCode { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self.as_str()) } } impl core::fmt::Display for ClientCode { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{}", self.as_str()) + self.as_str().fmt(f) } }