Skip to content

Commit c89eed0

Browse files
committed
Remove dependency on safe-regex for regex-lite
1 parent fdc9255 commit c89eed0

File tree

11 files changed

+131
-259
lines changed

11 files changed

+131
-259
lines changed

Cargo.lock

+8-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/relayer-types/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ serde_json = { version = "1" }
3333
erased-serde = { version = "0.3" }
3434
prost = { version = "0.11" }
3535
bytes = { version = "1.4.0" }
36-
safe-regex = { version = "0.2.5" }
3736
subtle-encoding = { version = "0.5" }
3837
flex-error = { version = "0.4.4" }
3938
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
@@ -42,6 +41,7 @@ itertools = { version = "0.10.3" }
4241
primitive-types = { version = "0.12.1", default-features = false, features = ["serde_no_std"] }
4342
dyn-clone = "1.0.12"
4443
num-rational = "0.4.1"
44+
regex-lite = "0.1.0"
4545

4646
[dependencies.tendermint]
4747
version = "0.33.0"

crates/relayer-types/src/applications/transfer/coin.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin;
2-
use safe_regex::regex;
3-
use serde::{Deserialize, Serialize};
41
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;
610

711
use super::amount::Amount;
812
use super::denom::{BaseDenom, PrefixedDenom};
913
use super::error::Error;
10-
use crate::serializers::serde_string;
1114

1215
/// A `Coin` type with fully qualified `PrefixedDenom`.
1316
pub type PrefixedCoin = Coin<PrefixedDenom>;
@@ -61,24 +64,21 @@ where
6164
{
6265
type Err = Error;
6366

64-
#[allow(clippy::assign_op_pattern)]
67+
// #[allow(clippy::assign_op_pattern)]
6568
fn from_str(coin_str: &str) -> Result<Self, Error> {
6669
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
6770
// a letter, a number or a separator ('/', ':', '.', '_' or '-').
6871
// Loosely copy the regex from here:
6972
// 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");
7575

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+
})?;
7779

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)?;
8282

8383
Ok(Coin { amount, denom })
8484
}

crates/relayer-types/src/applications/transfer/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ define_error! {
135135

136136
InvalidCoin
137137
{ coin: String }
138-
| e | { format_args!("invalid coin string: {}", e.coin) },
138+
| e | { format_args!("invalid coin: {}", e.coin) },
139139

140140
Utf8Decode
141141
[ TraceError<Utf8Error> ]

crates/relayer-types/src/core/ics24_host/identifier.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::convert::{From, Infallible};
22
use std::fmt::{Debug, Display, Error as FmtError, Formatter};
33
use std::str::FromStr;
44

5+
use regex_lite::Regex;
56
use serde::{Deserialize, Serialize};
67

78
use super::validate::*;
@@ -101,8 +102,8 @@ impl ChainId {
101102
/// assert_eq!(ChainId::is_epoch_format("chainA-1"), true);
102103
/// ```
103104
pub fn is_epoch_format(chain_id: &str) -> bool {
104-
let re = safe_regex::regex!(br".+[^-]-{1}[1-9][0-9]*");
105-
re.is_match(chain_id.as_bytes())
105+
let re = Regex::new(r"^.+[^-]-{1}[1-9][0-9]*$").expect("failed to compile regex");
106+
re.is_match(chain_id)
106107
}
107108
}
108109

crates/relayer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ signature = "2.1.0"
5656
anyhow = "1.0"
5757
semver = "1.0"
5858
humantime = "2.1.0"
59-
regex = "1.8.1"
59+
regex-lite = "0.1.0"
6060
moka = "0.11.3"
6161
uuid = { version = "1.4.0", features = ["v4"] }
6262
bs58 = "0.5.0"

crates/relayer/src/config/filter.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,16 @@ impl Serialize for ChannelFilters {
231231
}
232232
}
233233

234-
/// Newtype wrapper for expressing wildcard patterns compiled to a [`regex::Regex`].
234+
/// Newtype wrapper for expressing wildcard patterns compiled to a [`regex_lite::Regex`].
235235
#[derive(Clone, Debug)]
236236
pub struct Wildcard {
237237
pattern: String,
238-
regex: regex::Regex,
238+
regex: regex_lite::Regex,
239239
}
240240

241241
impl Wildcard {
242-
pub fn new(pattern: String) -> Result<Self, regex::Error> {
243-
let escaped = regex::escape(&pattern).replace("\\*", "(?:.*)");
242+
pub fn new(pattern: String) -> Result<Self, regex_lite::Error> {
243+
let escaped = regex_lite::escape(&pattern).replace("\\*", "(?:.*)");
244244
let regex = format!("^{escaped}$").parse()?;
245245
Ok(Self { pattern, regex })
246246
}
@@ -252,7 +252,7 @@ impl Wildcard {
252252
}
253253

254254
impl FromStr for Wildcard {
255-
type Err = regex::Error;
255+
type Err = regex_lite::Error;
256256

257257
fn from_str(pattern: &str) -> Result<Self, Self::Err> {
258258
Self::new(pattern.to_string())

crates/relayer/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use http::uri::InvalidUri;
77
use humantime::format_duration;
88
use ibc_proto::protobuf::Error as TendermintProtoError;
99
use prost::{DecodeError, EncodeError};
10-
use regex::Regex;
10+
use regex_lite::Regex;
1111
use tendermint::abci;
1212
use tendermint::Error as TendermintError;
1313
use tendermint_light_client::builder::error::Error as LightClientBuilderError;

0 commit comments

Comments
 (0)