From a100fb3e46cb5f40d6580ece884effacae37b0c7 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sun, 12 Nov 2023 04:10:00 -0800 Subject: [PATCH] feat(bin) : refactor ZeroAsNone struct to a Macro (#5397) Co-authored-by: Matthias Seitz --- bin/reth/src/args/rpc_server_args.rs | 10 ++-- bin/reth/src/args/types.rs | 69 ++++++++++++++++------------ bin/reth/src/cli/mod.rs | 2 +- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index 66937d153e..0a1ec745cb 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -2,7 +2,7 @@ use crate::{ args::{ - types::{MaxU32, ZeroAsNone}, + types::{MaxU32, ZeroAsNoneU64}, GasPriceOracleArgs, }, cli::{ @@ -157,12 +157,12 @@ pub struct RpcServerArgs { pub rpc_max_tracing_requests: u32, /// Maximum number of blocks that could be scanned per filter request. (0 = entire chain) - #[arg(long, value_name = "COUNT", default_value_t = ZeroAsNone::new(constants::DEFAULT_MAX_BLOCKS_PER_FILTER))] - pub rpc_max_blocks_per_filter: ZeroAsNone, + #[arg(long, value_name = "COUNT", default_value_t = ZeroAsNoneU64::new(constants::DEFAULT_MAX_BLOCKS_PER_FILTER))] + pub rpc_max_blocks_per_filter: ZeroAsNoneU64, /// Maximum number of logs that can be returned in a single response. (0 = no limit) - #[arg(long, value_name = "COUNT", default_value_t = ZeroAsNone::new(constants::DEFAULT_MAX_LOGS_PER_RESPONSE as u64))] - pub rpc_max_logs_per_response: ZeroAsNone, + #[arg(long, value_name = "COUNT", default_value_t = ZeroAsNoneU64::new(constants::DEFAULT_MAX_LOGS_PER_RESPONSE as u64))] + pub rpc_max_logs_per_response: ZeroAsNoneU64, /// Maximum gas limit for `eth_call` and call tracing RPC methods. #[arg( diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index 3dcd8f55d4..4696e5b323 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -2,40 +2,50 @@ use std::{fmt, num::ParseIntError, str::FromStr}; -/// A helper type that maps `0` to `None` when parsing CLI arguments. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ZeroAsNone(pub Option); +/// A macro that generates types that maps "0" to "None" when parsing CLI arguments. -impl ZeroAsNone { - /// Returns the inner value. - pub const fn new(value: u64) -> Self { - Self(Some(value)) - } +macro_rules! zero_as_none { + ($type_name:ident, $inner_type:ty) => { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + /// A helper type that maps `0` to `None` when parsing CLI arguments. + pub struct $type_name(pub Option<$inner_type>); - /// Returns the inner value or `u64::MAX` if `None`. - pub fn unwrap_or_max(self) -> u64 { - self.0.unwrap_or(u64::MAX) - } -} + impl $type_name { + /// Returns the inner value. + pub const fn new(value: $inner_type) -> Self { + Self(Some(value)) + } -impl fmt::Display for ZeroAsNone { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0 { - Some(value) => write!(f, "{}", value), - None => write!(f, "0"), + /// Returns the inner value or `$inner_type::MAX` if `None`. + pub fn unwrap_or_max(self) -> $inner_type { + self.0.unwrap_or(<$inner_type>::MAX) + } } - } + + impl std::fmt::Display for $type_name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + Some(value) => write!(f, "{}", value), + None => write!(f, "0"), + } + } + } + + impl std::str::FromStr for $type_name { + type Err = std::num::ParseIntError; + + fn from_str(s: &str) -> Result { + let value = s.parse::<$inner_type>()?; + Ok(Self(if value == 0 { None } else { Some(value) })) + } + } + }; } -impl FromStr for ZeroAsNone { - type Err = std::num::ParseIntError; - - fn from_str(s: &str) -> Result { - let value = s.parse::()?; - Ok(Self(if value == 0 { None } else { Some(value) })) - } -} +zero_as_none!(ZeroAsNoneU64, u64); +zero_as_none!(ZeroAsNoneU32, u32); +/// A macro that generates types that map "max" to "MAX" when parsing CLI arguments. macro_rules! max_values { ($name:ident, $ty:ident) => { #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -78,14 +88,15 @@ macro_rules! max_values { } max_values!(MaxU32, u32); max_values!(MaxU64, u64); + #[cfg(test)] mod tests { use super::*; #[test] fn test_zero_parse() { - let val = "0".parse::().unwrap(); - assert_eq!(val, ZeroAsNone(None)); + let val = "0".parse::().unwrap(); + assert_eq!(val, ZeroAsNoneU64(None)); assert_eq!(val.unwrap_or_max(), u64::MAX); } } diff --git a/bin/reth/src/cli/mod.rs b/bin/reth/src/cli/mod.rs index d48afb627c..1361b53d2d 100644 --- a/bin/reth/src/cli/mod.rs +++ b/bin/reth/src/cli/mod.rs @@ -363,7 +363,7 @@ mod tests { let end = format!("reth/logs/{}", SUPPORTED_CHAINS[0]); assert!(log_dir.as_ref().ends_with(end), "{:?}", log_dir); - let mut iter = SUPPORTED_CHAINS.into_iter(); + let mut iter = SUPPORTED_CHAINS.iter(); iter.next(); for chain in iter { let mut reth = Cli::<()>::try_parse_from(["reth", "node", "--chain", chain]).unwrap();