Skip to content

Commit

Permalink
Revert "Use Coin's FromStr impl in FromStr impl of Coins"
Browse files Browse the repository at this point in the history
This reverts commit 39dc511.
  • Loading branch information
chipshort committed Jun 21, 2023
1 parent 606137c commit 3544d7e
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/std/src/coins.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::type_name;
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -80,9 +81,29 @@ impl FromStr for Coins {
return Ok(Self::default());
}

// TODO: use FromStr impl for Coin once it's merged
let parse_coin_str = |s: &str| -> StdResult<Coin> {
for (i, c) in s.chars().enumerate() {
if !c.is_ascii_digit() {
let amount = Uint128::from_str(&s[..i]).map_err(|_| {
StdError::generic_err(
"Parsing Coin: Missing amount or non-digit characters in amount",
)
})?;
let denom = String::from(&s[i..]);
return Ok(Coin { amount, denom });
}
}

Err(StdError::parse_err(
type_name::<Coin>(),
format!("invalid coin string: {s}"),
))
};

Ok(s.split(',')
.map(Coin::from_str)
.collect::<Result<Vec<_>, _>>()?
.map(parse_coin_str)
.collect::<StdResult<Vec<_>>>()?
.try_into()?)
}
}
Expand Down

0 comments on commit 3544d7e

Please sign in to comment.