|
1 |
| -use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin; |
2 |
| -use safe_regex::regex; |
3 |
| -use serde::{Deserialize, Serialize}; |
4 | 1 | use std::fmt::{Display, Error as FmtError, Formatter};
|
5 |
| -use std::str::{from_utf8, FromStr}; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use regex_lite::Regex; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin; |
| 8 | + |
| 9 | +use crate::serializers::serde_string; |
6 | 10 |
|
7 | 11 | use super::amount::Amount;
|
8 | 12 | use super::denom::{BaseDenom, PrefixedDenom};
|
9 | 13 | use super::error::Error;
|
10 |
| -use crate::serializers::serde_string; |
11 | 14 |
|
12 | 15 | /// A `Coin` type with fully qualified `PrefixedDenom`.
|
13 | 16 | pub type PrefixedCoin = Coin<PrefixedDenom>;
|
@@ -61,24 +64,21 @@ where
|
61 | 64 | {
|
62 | 65 | type Err = Error;
|
63 | 66 |
|
64 |
| - #[allow(clippy::assign_op_pattern)] |
| 67 | + // #[allow(clippy::assign_op_pattern)] |
65 | 68 | fn from_str(coin_str: &str) -> Result<Self, Error> {
|
66 | 69 | // Denominations can be 3 ~ 128 characters long and support letters, followed by either
|
67 | 70 | // a letter, a number or a separator ('/', ':', '.', '_' or '-').
|
68 | 71 | // Loosely copy the regex from here:
|
69 | 72 | // https://github.com/cosmos/cosmos-sdk/blob/v0.45.5/types/coin.go#L760-L762
|
70 |
| - let matcher = regex!(br"([0-9]+)([a-zA-Z0-9/:\\._\x2d]+)"); |
71 |
| - |
72 |
| - let (m1, m2) = matcher |
73 |
| - .match_slices(coin_str.as_bytes()) |
74 |
| - .ok_or_else(|| Error::invalid_coin(coin_str.to_string()))?; |
| 73 | + let regex = Regex::new(r"(?<amount>[0-9]+)(?<denom>[a-zA-Z0-9/:\\._\x2d]+)") |
| 74 | + .expect("failed to compile regex"); |
75 | 75 |
|
76 |
| - let amount = from_utf8(m1).map_err(Error::utf8_decode)?.parse()?; |
| 76 | + let captures = regex.captures(coin_str).ok_or_else(|| { |
| 77 | + Error::invalid_coin(format!("{coin_str} (expected format: <amount><denom>)")) |
| 78 | + })?; |
77 | 79 |
|
78 |
| - let denom = from_utf8(m2) |
79 |
| - .map_err(Error::utf8_decode)? |
80 |
| - .parse() |
81 |
| - .map_err(Into::into)?; |
| 80 | + let amount = captures["amount"].parse()?; |
| 81 | + let denom = captures["denom"].parse().map_err(Into::into)?; |
82 | 82 |
|
83 | 83 | Ok(Coin { amount, denom })
|
84 | 84 | }
|
|
0 commit comments