reth_node_core/args/
types.rs1use std::{fmt, num::ParseIntError, str::FromStr};
4
5macro_rules! zero_as_none {
7 ($type_name:ident, $inner_type:ty) => {
8 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
9 pub struct $type_name(pub Option<$inner_type>);
11
12 impl $type_name {
13 pub const fn new(value: $inner_type) -> Self {
15 Self(Some(value))
16 }
17
18 pub fn unwrap_or_max(self) -> $inner_type {
20 self.0.unwrap_or(<$inner_type>::MAX)
21 }
22 }
23
24 impl std::fmt::Display for $type_name {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 match self.0 {
27 Some(value) => write!(f, "{}", value),
28 None => write!(f, "0"),
29 }
30 }
31 }
32
33 impl From<$inner_type> for $type_name {
34 #[inline]
35 fn from(value: $inner_type) -> Self {
36 Self(if value == 0 { None } else { Some(value) })
37 }
38 }
39
40 impl std::str::FromStr for $type_name {
41 type Err = std::num::ParseIntError;
42
43 fn from_str(s: &str) -> Result<Self, Self::Err> {
44 let value = s.parse::<$inner_type>()?;
45 Ok(Self::from(value))
46 }
47 }
48 };
49}
50
51zero_as_none!(ZeroAsNoneU64, u64);
52zero_as_none!(ZeroAsNoneU32, u32);
53
54macro_rules! max_values {
56 ($name:ident, $ty:ident) => {
57 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
58 pub struct $name(pub $ty);
61
62 impl $name {
63 pub const fn get(&self) -> $ty {
65 self.0
66 }
67 }
68
69 impl fmt::Display for $name {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 write!(f, "{}", self.0)
72 }
73 }
74
75 impl From<$ty> for $name {
76 #[inline]
77 fn from(value: $ty) -> Self {
78 Self(value)
79 }
80 }
81
82 impl FromStr for $name {
83 type Err = ParseIntError;
84
85 fn from_str(s: &str) -> Result<Self, Self::Err> {
86 if s.eq_ignore_ascii_case("max") {
87 Ok($name(<$ty>::MAX))
88 } else {
89 s.parse::<$ty>().map($name)
90 }
91 }
92 }
93 };
94}
95max_values!(MaxU32, u32);
96max_values!(MaxU64, u64);
97
98#[derive(Debug, Clone)]
100pub struct MaxOr<T> {
101 inner: T,
103}
104
105impl<T> MaxOr<T>
106where
107 T: clap::builder::TypedValueParser,
108 T::Value: Into<u64>,
109{
110 pub const fn new(inner: T) -> Self {
112 Self { inner }
113 }
114}
115
116impl<T> clap::builder::TypedValueParser for MaxOr<T>
117where
118 T: clap::builder::TypedValueParser,
119 T::Value: Into<u64>,
120{
121 type Value = u64;
122
123 fn parse_ref(
124 &self,
125 cmd: &clap::Command,
126 arg: Option<&clap::Arg>,
127 value: &std::ffi::OsStr,
128 ) -> Result<Self::Value, clap::Error> {
129 if value.to_str().is_some_and(|s| s.eq_ignore_ascii_case("max")) {
130 Ok(u64::MAX)
131 } else {
132 self.inner.parse_ref(cmd, arg, value).map(Into::into)
133 }
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140 use clap::Parser;
141
142 #[derive(Parser, Debug)]
144 struct NodeCommand {
145 #[arg(long, value_parser = MaxOr::new(clap::value_parser!(u64)))]
146 max_value: u64,
147 }
148
149 #[test]
150 fn test_zero_parse() {
151 let val = "0".parse::<ZeroAsNoneU64>().unwrap();
152 assert_eq!(val, ZeroAsNoneU64(None));
153 assert_eq!(val.unwrap_or_max(), u64::MAX);
154 }
155
156 #[test]
157 fn test_from_u64() {
158 let original = 1u64;
159 let expected = ZeroAsNoneU64(Some(1u64));
160 assert_eq!(ZeroAsNoneU64::from(original), expected);
161
162 let original = 0u64;
163 let expected = ZeroAsNoneU64(None);
164 assert_eq!(ZeroAsNoneU64::from(original), expected);
165 }
166
167 #[test]
168 fn parse_max_value() {
169 let cmd: NodeCommand = NodeCommand::try_parse_from(["reth", "--max-value", "max"]).unwrap();
170 assert_eq!(cmd.max_value, u64::MAX);
171
172 let cmd: NodeCommand = NodeCommand::try_parse_from(["reth", "--max-value", "42"]).unwrap();
173 assert_eq!(cmd.max_value, 42);
174
175 let result = NodeCommand::try_parse_from(["reth", "--max-value", "invalid"]);
176 assert!(result.is_err());
177 }
178}