Skip to content

Commit 2bdc448

Browse files
authored
Merge pull request #156 from sr-gi/f64_gtz_deserialize
sim-cli: improves capacity_multiplier deserializer
2 parents b231fcd + 8af3793 commit 2bdc448

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

sim-cli/src/main.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55
use tokio::sync::Mutex;
66

77
use anyhow::anyhow;
8+
use clap::builder::TypedValueParser;
89
use clap::Parser;
910
use log::LevelFilter;
1011
use sim_lib::{
@@ -22,6 +23,22 @@ pub const ACTIVITY_MULTIPLIER: f64 = 2.0;
2223
/// Default batch size to flush result data to disk
2324
const DEFAULT_PRINT_BATCH_SIZE: u32 = 500;
2425

26+
/// Deserializes a f64 as long as it is positive and greater than 0.
27+
fn deserialize_f64_greater_than_zero(x: String) -> Result<f64, String> {
28+
match x.parse::<f64>() {
29+
Ok(x) => {
30+
if x > 0.0 {
31+
Ok(x)
32+
} else {
33+
Err(format!(
34+
"capacity_multiplier must be higher than 0. {x} received."
35+
))
36+
}
37+
}
38+
Err(e) => Err(e.to_string()),
39+
}
40+
}
41+
2542
#[derive(Parser)]
2643
#[command(version, about)]
2744
struct Cli {
@@ -42,7 +59,7 @@ struct Cli {
4259
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT, value_parser = clap::builder::RangedU64ValueParser::<u64>::new().range(1..u64::MAX))]
4360
expected_pmt_amt: u64,
4461
/// Multiplier of the overall network capacity used by the random activity generator
45-
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER)]
62+
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER, value_parser = clap::builder::StringValueParser::new().try_map(deserialize_f64_greater_than_zero))]
4663
capacity_multiplier: f64,
4764
/// Do not create an output file containing the simulations results
4865
#[clap(long, default_value_t = false)]

0 commit comments

Comments
 (0)