Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bin) : max_values Macro for CLI args parsing #5379

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions bin/reth/src/args/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ impl FromStr for MaxU32 {
}
}

macro_rules! max_values {
($name:ident, $ty:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
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<Self, Self::Err> {
if s.eq_ignore_ascii_case("max") {
Ok($name(<$ty>::MAX))
} else {
s.parse::<$ty>().map($name)
}
}
}
};
}
max_values!(U32, u32);
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -80,4 +105,19 @@ mod tests {
let val = "123".parse::<MaxU32>().unwrap();
assert_eq!(val, MaxU32(123));
}

max_values!(TestU32, u32);
DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
max_values!(TestU64, u64);

#[test]
fn parse_max() {
assert_eq!("max".parse::<TestU32>().unwrap().0, u32::MAX);
assert_eq!("max".parse::<TestU64>().unwrap().0, u64::MAX);
}

#[test]
fn parse_numeric_values() {
assert_eq!("123".parse::<TestU32>().unwrap().0, 123);
assert_eq!("456".parse::<TestU64>().unwrap().0, 456);
}
}
Loading