feat(bin) : refactor ZeroAsNone struct to a Macro (#5397)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
DoTheBestToGetTheBest
2023-11-12 04:10:00 -08:00
committed by GitHub
parent 63f67d97fd
commit a100fb3e46
3 changed files with 46 additions and 35 deletions

View File

@ -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(

View File

@ -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<u64>);
/// 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<Self, Self::Err> {
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<Self, Self::Err> {
let value = s.parse::<u64>()?;
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::<ZeroAsNone>().unwrap();
assert_eq!(val, ZeroAsNone(None));
let val = "0".parse::<ZeroAsNoneU64>().unwrap();
assert_eq!(val, ZeroAsNoneU64(None));
assert_eq!(val.unwrap_or_max(), u64::MAX);
}
}

View File

@ -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();