From 5beb105a7cddeb2a8bf355d0d8b78da29d3763cd Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:42:47 -0800 Subject: [PATCH] feat(bin) : max_values Macro for CLI args parsing (#5379) Co-authored-by: Matthias Seitz --- bin/reth/src/args/types.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bin/reth/src/args/types.rs b/bin/reth/src/args/types.rs index d193e2dffeae..fb57d0e651d2 100644 --- a/bin/reth/src/args/types.rs +++ b/bin/reth/src/args/types.rs @@ -1,6 +1,6 @@ //! Additional helper types for CLI parsing. -use std::{fmt, str::FromStr}; +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)] @@ -36,6 +36,34 @@ impl FromStr for ZeroAsNone { } } +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 { + 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!(MaxU32, u32); +max_values!(MaxU64, u64); #[cfg(test)] mod tests { use super::*;