Use RangeBounds for is_within_range (#29763)

This commit is contained in:
Andrew Fitzgerald 2023-02-01 09:17:12 -08:00 committed by GitHub
parent dad1610742
commit d944c657a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 17 deletions

View File

@ -352,7 +352,7 @@ pub fn build_args<'a>(version: &'_ str) -> App<'a, '_> {
Arg::with_name("num_conflict_groups") Arg::with_name("num_conflict_groups")
.long("num-conflict-groups") .long("num-conflict-groups")
.takes_value(true) .takes_value(true)
.validator(|arg| is_within_range(arg, 1, usize::MAX - 1)) .validator(|arg| is_within_range(arg, 1..))
.help("The number of unique destination accounts per transactions 'chunk'. Lower values will result in more transaction conflicts.") .help("The number of unique destination accounts per transactions 'chunk'. Lower values will result in more transaction conflicts.")
) )
} }

View File

@ -7,7 +7,7 @@ use {
pubkey::{Pubkey, MAX_SEED_LEN}, pubkey::{Pubkey, MAX_SEED_LEN},
signature::{read_keypair_file, Signature}, signature::{read_keypair_file, Signature},
}, },
std::{fmt::Display, str::FromStr}, std::{fmt::Display, ops::RangeBounds, str::FromStr},
}; };
fn is_parsable_generic<U, T>(string: T) -> Result<(), String> fn is_parsable_generic<U, T>(string: T) -> Result<(), String>
@ -35,18 +35,16 @@ where
// Return an error if string cannot be parsed as numeric type T, and value not within specified // Return an error if string cannot be parsed as numeric type T, and value not within specified
// range // range
pub fn is_within_range<T>(string: String, range_min: T, range_max: T) -> Result<(), String> pub fn is_within_range<T, R>(string: String, range: R) -> Result<(), String>
where where
T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add<Output = T> + From<usize>, T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add<Output = T> + From<usize>,
T::Err: Display, T::Err: Display,
R: RangeBounds<T> + std::fmt::Debug,
{ {
match string.parse::<T>() { match string.parse::<T>() {
Ok(input) => { Ok(input) => {
let range = range_min..range_max + 1.into();
if !range.contains(&input) { if !range.contains(&input) {
Err(format!( Err(format!("input '{input:?}' out of range {range:?}"))
"input '{input:?}' out of range ({range_min:?}..{range_max:?}]"
))
} else { } else {
Ok(()) Ok(())
} }

View File

@ -7,7 +7,7 @@ use {
pubkey::{Pubkey, MAX_SEED_LEN}, pubkey::{Pubkey, MAX_SEED_LEN},
signature::{read_keypair_file, Signature}, signature::{read_keypair_file, Signature},
}, },
std::{fmt::Display, str::FromStr}, std::{fmt::Display, ops::RangeBounds, str::FromStr},
}; };
fn is_parsable_generic<U, T>(string: T) -> Result<(), String> fn is_parsable_generic<U, T>(string: T) -> Result<(), String>
@ -35,18 +35,16 @@ where
// Return an error if string cannot be parsed as numeric type T, and value not within specified // Return an error if string cannot be parsed as numeric type T, and value not within specified
// range // range
pub fn is_within_range<T>(string: String, range_min: T, range_max: T) -> Result<(), String> pub fn is_within_range<T, R>(string: String, range: R) -> Result<(), String>
where where
T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add<Output = T> + From<usize>, T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add<Output = T> + From<usize>,
T::Err: Display, T::Err: Display,
R: RangeBounds<T> + std::fmt::Debug,
{ {
match string.parse::<T>() { match string.parse::<T>() {
Ok(input) => { Ok(input) => {
let range = range_min..range_max + 1.into();
if !range.contains(&input) { if !range.contains(&input) {
Err(format!( Err(format!("input '{input:?}' out of range {range:?}"))
"input '{input:?}' out of range ({range_min:?}..{range_max:?}]"
))
} else { } else {
Ok(()) Ok(())
} }

View File

@ -686,7 +686,7 @@ impl StakeSubCommands for App<'_, '_> {
.long("num-rewards-epochs") .long("num-rewards-epochs")
.takes_value(true) .takes_value(true)
.value_name("NUM") .value_name("NUM")
.validator(|s| is_within_range(s, 1, 10)) .validator(|s| is_within_range(s, 1..=10))
.default_value_if("with_rewards", None, "1") .default_value_if("with_rewards", None, "1")
.requires("with_rewards") .requires("with_rewards")
.help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"), .help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"),

View File

@ -338,7 +338,7 @@ impl VoteSubCommands for App<'_, '_> {
.long("num-rewards-epochs") .long("num-rewards-epochs")
.takes_value(true) .takes_value(true)
.value_name("NUM") .value_name("NUM")
.validator(|s| is_within_range(s, 1, 10)) .validator(|s| is_within_range(s, 1..=10))
.default_value_if("with_rewards", None, "1") .default_value_if("with_rewards", None, "1")
.requires("with_rewards") .requires("with_rewards")
.help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"), .help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"),

View File

@ -964,7 +964,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
.value_name("MILLISECS") .value_name("MILLISECS")
.hidden(true) .hidden(true)
.takes_value(true) .takes_value(true)
.validator(|s| is_within_range(s, 1, MAX_BATCH_SEND_RATE_MS)) .validator(|s| is_within_range(s, 1..=MAX_BATCH_SEND_RATE_MS))
.default_value(&default_args.rpc_send_transaction_batch_ms) .default_value(&default_args.rpc_send_transaction_batch_ms)
.help("The rate at which transactions sent via rpc service are sent in batch."), .help("The rate at which transactions sent via rpc service are sent in batch."),
) )
@ -1000,7 +1000,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
.value_name("NUMBER") .value_name("NUMBER")
.hidden(true) .hidden(true)
.takes_value(true) .takes_value(true)
.validator(|s| is_within_range(s, 1, MAX_TRANSACTION_BATCH_SIZE)) .validator(|s| is_within_range(s, 1..=MAX_TRANSACTION_BATCH_SIZE))
.default_value(&default_args.rpc_send_transaction_batch_size) .default_value(&default_args.rpc_send_transaction_batch_size)
.help("The size of transactions to be sent in batch."), .help("The size of transactions to be sent in batch."),
) )