Skip to content

Commit

Permalink
Merge pull request #156 from sr-gi/f64_gtz_deserialize
Browse files Browse the repository at this point in the history
sim-cli: improves capacity_multiplier deserializer
  • Loading branch information
carlaKC authored Nov 2, 2023
2 parents b231fcd + 8af3793 commit 2bdc448
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use tokio::sync::Mutex;

use anyhow::anyhow;
use clap::builder::TypedValueParser;
use clap::Parser;
use log::LevelFilter;
use sim_lib::{
Expand All @@ -22,6 +23,22 @@ pub const ACTIVITY_MULTIPLIER: f64 = 2.0;
/// Default batch size to flush result data to disk
const DEFAULT_PRINT_BATCH_SIZE: u32 = 500;

/// Deserializes a f64 as long as it is positive and greater than 0.
fn deserialize_f64_greater_than_zero(x: String) -> Result<f64, String> {
match x.parse::<f64>() {
Ok(x) => {
if x > 0.0 {
Ok(x)
} else {
Err(format!(
"capacity_multiplier must be higher than 0. {x} received."
))
}
}
Err(e) => Err(e.to_string()),
}
}

#[derive(Parser)]
#[command(version, about)]
struct Cli {
Expand All @@ -42,7 +59,7 @@ struct Cli {
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT, value_parser = clap::builder::RangedU64ValueParser::<u64>::new().range(1..u64::MAX))]
expected_pmt_amt: u64,
/// Multiplier of the overall network capacity used by the random activity generator
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER)]
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER, value_parser = clap::builder::StringValueParser::new().try_map(deserialize_f64_greater_than_zero))]
capacity_multiplier: f64,
/// Do not create an output file containing the simulations results
#[clap(long, default_value_t = false)]
Expand Down

0 comments on commit 2bdc448

Please sign in to comment.