From 921332a2753f54c79adf2ec6b0bbfc993a3d9fc0 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 04:48:57 -0800 Subject: [PATCH 1/8] Update types.rs --- bin/reth/src/args/types.rs | 67 +++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index d193e2dffeae..dd64088ce304 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -2,19 +2,28 @@ use std::{fmt, str::FromStr}; -/// A helper type that maps `0` to `None` when parsing CLI arguments. +/// A helper type that maps `u32::MAX` to `None` when parsing CLI arguments. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ZeroAsNone(pub Option); +pub struct MaxAsNone(pub Option); -impl ZeroAsNone { +impl MaxAsNone { /// Returns the inner value. - pub const fn new(value: u64) -> Self { + pub const fn new(value: u32) -> Self { Self(Some(value)) } - /// Returns the inner value or `u64::MAX` if `None`. - pub fn unwrap_or_max(self) -> u64 { - self.0.unwrap_or(u64::MAX) + /// Returns the inner value or `u32::MAX` if `None`. + pub fn unwrap_or_max(self) -> u32 { + self.0.unwrap_or(u32::MAX) + } +} + +impl fmt::Display for MaxAsNone { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0 { + Some(value) => write!(f, "{}", value), + None => write!(f, "max"), + } } } @@ -26,7 +35,6 @@ impl fmt::Display for ZeroAsNone { } } } - impl FromStr for ZeroAsNone { type Err = std::num::ParseIntError; @@ -36,6 +44,42 @@ impl FromStr for ZeroAsNone { } } +/// A helper type that maps `0` to `None` when parsing CLI arguments. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ZeroAsNone(pub Option); + +impl ZeroAsNone { + /// Returns the inner value. + pub const fn new(value: u64) -> Self { + Self(Some(value)) + } + + /// Returns the inner value or `u64::MAX` if `None`. + pub fn unwrap_or_max(self) -> u64 { + self.0.unwrap_or(u64::MAX) + } +} + +impl FromStr for MaxAsNone { + type Err = std::num::ParseIntError; + + fn from_str(s: &str) -> Result { + if s == "max" { + Ok(Self(None)) + } else { + s.parse::().map( + |value| { + if value == u32::MAX { + Self(None) + } else { + Self(Some(value)) + } + }, + ) + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -46,4 +90,11 @@ mod tests { assert_eq!(val, ZeroAsNone(None)); assert_eq!(val.unwrap_or_max(), u64::MAX); } + + #[test] + fn test_max_parse() { + let val = "max".parse::().unwrap(); + assert_eq!(val, MaxAsNone(None)); + assert_eq!(val.unwrap_or_max(), u32::MAX); + } } From a642e02b519100dccfed9687a370621acb10c1fc Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 06:22:41 -0800 Subject: [PATCH 2/8] Update types.rs --- bin/reth/src/args/types.rs | 69 ++++++++++++++------------------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index dd64088ce304..c6d8366de05b 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -1,29 +1,20 @@ //! Additional helper types for CLI parsing. -use std::{fmt, str::FromStr}; +use std::{fmt, num::ParseIntError, str::FromStr}; -/// A helper type that maps `u32::MAX` to `None` when parsing CLI arguments. +/// A helper type that maps `0` to `None` when parsing CLI arguments. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct MaxAsNone(pub Option); +pub struct ZeroAsNone(pub Option); -impl MaxAsNone { +impl ZeroAsNone { /// Returns the inner value. - pub const fn new(value: u32) -> Self { + pub const fn new(value: u64) -> Self { Self(Some(value)) } - /// Returns the inner value or `u32::MAX` if `None`. - pub fn unwrap_or_max(self) -> u32 { - self.0.unwrap_or(u32::MAX) - } -} - -impl fmt::Display for MaxAsNone { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0 { - Some(value) => write!(f, "{}", value), - None => write!(f, "max"), - } + /// Returns the inner value or `u64::MAX` if `None`. + pub fn unwrap_or_max(self) -> u64 { + self.0.unwrap_or(u64::MAX) } } @@ -35,6 +26,7 @@ impl fmt::Display for ZeroAsNone { } } } + impl FromStr for ZeroAsNone { type Err = std::num::ParseIntError; @@ -44,38 +36,24 @@ impl FromStr for ZeroAsNone { } } -/// A helper type that maps `0` to `None` when parsing CLI arguments. +/// A helper type for parsing "max" as `u32::MAX`. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ZeroAsNone(pub Option); - -impl ZeroAsNone { - /// Returns the inner value. - pub const fn new(value: u64) -> Self { - Self(Some(value)) - } +pub struct MaxU32(pub u32); - /// Returns the inner value or `u64::MAX` if `None`. - pub fn unwrap_or_max(self) -> u64 { - self.0.unwrap_or(u64::MAX) +impl fmt::Display for MaxU32 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) } } -impl FromStr for MaxAsNone { - type Err = std::num::ParseIntError; +impl FromStr for MaxU32 { + type Err = ParseIntError; fn from_str(s: &str) -> Result { if s == "max" { - Ok(Self(None)) + Ok(MaxU32(u32::MAX)) } else { - s.parse::().map( - |value| { - if value == u32::MAX { - Self(None) - } else { - Self(Some(value)) - } - }, - ) + s.parse::().map(MaxU32) } } } @@ -93,8 +71,13 @@ mod tests { #[test] fn test_max_parse() { - let val = "max".parse::().unwrap(); - assert_eq!(val, MaxAsNone(None)); - assert_eq!(val.unwrap_or_max(), u32::MAX); + let val = "max".parse::().unwrap(); + assert_eq!(val, MaxU32(u32::MAX)); + } + + #[test] + fn test_number_parse() { + let val = "123".parse::().unwrap(); + assert_eq!(val, MaxU32(123)); } } From 41f9fb0d75d90810f3bf5b7d287fe2f2690e9874 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:09:03 -0800 Subject: [PATCH 3/8] Update types.rs --- bin/reth/src/args/types.rs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index c6d8366de05b..3618d53e85d0 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -58,6 +58,31 @@ impl FromStr for MaxU32 { } } +macro_rules! max_values { + ($name:ident, $ty:ident) => { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub(crate) struct $name(pub(crate) $ty); + + impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } + } + + impl FromStr for $name { + type Err = ParseIntError; + + fn from_str(s: &str) -> Result { + if s.eq_ignore_ascii_case("max") { + Ok($name(<$ty>::MAX)) + } else { + s.parse::<$ty>().map($name) + } + } + } + }; +} +max_values!(U32, u32); #[cfg(test)] mod tests { use super::*; @@ -80,4 +105,19 @@ mod tests { let val = "123".parse::().unwrap(); assert_eq!(val, MaxU32(123)); } + + max_values!(TestU32, u32); + max_values!(TestU64, u64); + + #[test] + fn parse_max() { + assert_eq!("max".parse::().unwrap().0, u32::MAX); + assert_eq!("max".parse::().unwrap().0, u64::MAX); + } + + #[test] + fn parse_numeric_values() { + assert_eq!("123".parse::().unwrap().0, 123); + assert_eq!("456".parse::().unwrap().0, 456); + } } From f3bbee66f224625f2d2f4ca57fa9ca991ce84389 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:13:16 -0800 Subject: [PATCH 4/8] docs --- bin/reth/src/args/types.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index 3618d53e85d0..8d03b7e8677a 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -61,6 +61,8 @@ impl FromStr for MaxU32 { macro_rules! max_values { ($name:ident, $ty:ident) => { #[derive(Debug, Clone, Copy, PartialEq, Eq)] + /// A helper type for parsing "max" as the maximum value of the specified type. + pub(crate) struct $name(pub(crate) $ty); impl fmt::Display for $name { From d6637b9192a8bf9e264df54c00d3acf248bbd11a Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:28:09 -0800 Subject: [PATCH 5/8] Update types.rs --- bin/reth/src/args/types.rs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index 8d03b7e8677a..449a8588f05f 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -85,6 +85,7 @@ macro_rules! max_values { }; } max_values!(U32, u32); +max_values!(U64, u64); #[cfg(test)] mod tests { use super::*; @@ -107,19 +108,4 @@ mod tests { let val = "123".parse::().unwrap(); assert_eq!(val, MaxU32(123)); } - - max_values!(TestU32, u32); - max_values!(TestU64, u64); - - #[test] - fn parse_max() { - assert_eq!("max".parse::().unwrap().0, u32::MAX); - assert_eq!("max".parse::().unwrap().0, u64::MAX); - } - - #[test] - fn parse_numeric_values() { - assert_eq!("123".parse::().unwrap().0, 123); - assert_eq!("456".parse::().unwrap().0, 456); - } } From 753a8de3c22d93d1c59076b1bf7750cb76c8f37f Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:32:24 -0800 Subject: [PATCH 6/8] Update types.rs --- bin/reth/src/args/types.rs | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index 449a8588f05f..e79c18277cef 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -36,28 +36,6 @@ impl FromStr for ZeroAsNone { } } -/// A helper type for parsing "max" as `u32::MAX`. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct MaxU32(pub u32); - -impl fmt::Display for MaxU32 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl FromStr for MaxU32 { - type Err = ParseIntError; - - fn from_str(s: &str) -> Result { - if s == "max" { - Ok(MaxU32(u32::MAX)) - } else { - s.parse::().map(MaxU32) - } - } -} - macro_rules! max_values { ($name:ident, $ty:ident) => { #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -96,16 +74,4 @@ mod tests { assert_eq!(val, ZeroAsNone(None)); assert_eq!(val.unwrap_or_max(), u64::MAX); } - - #[test] - fn test_max_parse() { - let val = "max".parse::().unwrap(); - assert_eq!(val, MaxU32(u32::MAX)); - } - - #[test] - fn test_number_parse() { - let val = "123".parse::().unwrap(); - assert_eq!(val, MaxU32(123)); - } } From 1416326b2bdfe9f144c22bd5ed3c63b7b862656e Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:32:49 -0800 Subject: [PATCH 7/8] Update bin/reth/src/args/types.rs Co-authored-by: Matthias Seitz --- bin/reth/src/args/types.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index e79c18277cef..fdb3adf7a4a1 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -62,7 +62,8 @@ macro_rules! max_values { } }; } -max_values!(U32, u32); +max_values!(MaxU32, u32); +max_values!(MaxU64, u64); max_values!(U64, u64); #[cfg(test)] mod tests { From 2f5e46a4975cee3dd28085bc7837071ca4cb0977 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:34:15 -0800 Subject: [PATCH 8/8] Update types.rs --- bin/reth/src/args/types.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index fdb3adf7a4a1..fb57d0e651d2 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -64,7 +64,6 @@ macro_rules! max_values { } max_values!(MaxU32, u32); max_values!(MaxU64, u64); -max_values!(U64, u64); #[cfg(test)] mod tests { use super::*;