Skip to content

Commit

Permalink
fix(storagext-cli): parse numbers with underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte committed Nov 18, 2024
1 parent f91ecb7 commit 0181cb8
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions cli/polka-storage/storagext-cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,28 @@ use url::Url;

use crate::{missing_keypair_error, operation_takes_a_while, OutputFormat};

/// Removes the `_` from a the input before calling [`parse`](std::std::FromStr::parse).
fn parse_without_underscore<T>(s: &str) -> Result<T, T::Err>
where
T: std::str::FromStr,
{
s.replace('_', "").parse()
}

#[derive(Debug, Subcommand)]
#[command(name = "market", about = "CLI Client to the Market Pallet", version)]
pub(crate) enum MarketCommand {
/// Add balance to an account.
AddBalance {
/// Amount to add to the account.
#[arg(value_parser=parse_without_underscore::<storagext::Currency>)]
amount: storagext::Currency,
},

/// Withdraw balance from an account.
WithdrawBalance {
/// Amount to withdraw from the account.
#[arg(value_parser=parse_without_underscore::<storagext::Currency>)]
amount: storagext::Currency,
},

Expand Down Expand Up @@ -57,12 +73,6 @@ pub(crate) enum MarketCommand {
deal_ids: Vec<DealId>,
},

/// Withdraw balance from an account.
WithdrawBalance {
/// Amount to withdraw from the account.
amount: storagext::Currency,
},

/// Retrieve the balance for a given account.
RetrieveBalance {
/// The target account's ID.
Expand Down Expand Up @@ -296,3 +306,14 @@ impl MarketCommand {
Ok(submission_result)
}
}

#[cfg(test)]
mod test {
use super::parse_without_underscore;

#[test]
fn test_parse() {
let parsed = parse_without_underscore::<u128>("1_000_0").unwrap();
assert_eq!(parsed, 1_000_0);
}
}

0 comments on commit 0181cb8

Please sign in to comment.