Skip to content

Commit

Permalink
feat(rpc-types-engine): use strum for ClientCode (#1386)
Browse files Browse the repository at this point in the history
* feat(rpc-types-engine): use strum for ClientCode

* fix: derive feature
  • Loading branch information
DaniPopes authored Sep 26, 2024
1 parent 6d274cd commit 754431d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion crates/rpc-types-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand Down
52 changes: 12 additions & 40 deletions crates/rpc-types-engine/src/identification.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Client identification: <https://github.com/ethereum/execution-apis/blob/main/src/engine/identification.md>

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<str>, fmt::Display and serde::Serialize
#[derive(strum::EnumString)] // FromStr, TryFrom<&str>
#[non_exhaustive]
pub enum ClientCode {
/// Besu
BU,
Expand Down Expand Up @@ -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.
Expand All @@ -76,32 +64,16 @@ impl ClientCode {
}
}

impl FromStr for ClientCode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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)
}
}

Expand Down

0 comments on commit 754431d

Please sign in to comment.