refactor(interop-test): remove easy to replace strum dependency

We only use `strum` for the interop-tests but we add 3 dependencies to a full build of the repository for it, including a proc-macro which needs to be pipelined in front of other crates which makes them hard to parallelize. Remove it in favor of fairly trivial reimplementation of `FromStr`.

Pull-Request: #3513.
This commit is contained in:
Thomas Eizinger 2023-02-28 16:04:59 +11:00 committed by GitHub
parent 52a32ffac1
commit 2ca93e12c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 47 deletions

29
Cargo.lock generated
View File

@ -1975,7 +1975,6 @@ dependencies = [
"log",
"rand 0.8.5",
"redis",
"strum",
"tokio",
]
@ -4140,12 +4139,6 @@ dependencies = [
"base64 0.21.0",
]
[[package]]
name = "rustversion"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70"
[[package]]
name = "rw-stream-sink"
version = "0.3.0"
@ -4463,28 +4456,6 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]]
name = "stun"
version = "0.4.4"

View File

@ -14,5 +14,4 @@ libp2p = { path = "../", features = ["websocket", "quic", "mplex", "yamux", "tc
log = "0.4"
rand = "0.8.5"
redis = { version = "0.22.1", default-features = false, features = ["tokio-comp"] }
strum = { version = "0.24.1", features = ["derive"] }
tokio = { version = "1.24.1", features = ["full"] }

View File

@ -17,14 +17,13 @@ use libp2p::{
OutboundUpgradeExt, PeerId, Swarm, Transport as _,
};
use redis::AsyncCommands;
use strum::EnumString;
#[tokio::main]
async fn main() -> Result<()> {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
let transport_param: Transport = from_env("transport").context("unsupported transport")?;
let transport_param: Transport = from_env("transport")?;
let ip = env::var("ip").context("ip environment variable is not set")?;
@ -172,7 +171,7 @@ fn secure_channel_protocol_from_env<C: AsyncRead + AsyncWrite + Unpin + Send + '
MapSecOutputFn<C>,
>,
> {
let either_sec_upgrade = match from_env("security").context("unsupported secure channel")? {
let either_sec_upgrade = match from_env("security")? {
SecProtocol::Noise => Either::Left(
noise::NoiseAuthenticated::xx(identity).context("failed to intialise noise")?,
),
@ -199,17 +198,14 @@ fn factor_peer_id<C>(
}
fn muxer_protocol_from_env() -> Result<Either<yamux::YamuxConfig, mplex::MplexConfig>> {
Ok(
match from_env("muxer").context("unsupported multiplexer")? {
Muxer::Yamux => Either::Left(yamux::YamuxConfig::default()),
Muxer::Mplex => Either::Right(mplex::MplexConfig::new()),
},
)
Ok(match from_env("muxer")? {
Muxer::Yamux => Either::Left(yamux::YamuxConfig::default()),
Muxer::Mplex => Either::Right(mplex::MplexConfig::new()),
})
}
/// Supported transports by rust-libp2p.
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "kebab-case")]
#[derive(Clone, Debug)]
pub enum Transport {
Tcp,
QuicV1,
@ -217,22 +213,58 @@ pub enum Transport {
Ws,
}
impl FromStr for Transport {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
"tcp" => Self::Tcp,
"quic-v1" => Self::QuicV1,
"webrtc" => Self::Webrtc,
"ws" => Self::Ws,
other => bail!("unknown transport {other}"),
})
}
}
/// Supported stream multiplexers by rust-libp2p.
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "kebab-case")]
#[derive(Clone, Debug)]
pub enum Muxer {
Mplex,
Yamux,
}
impl FromStr for Muxer {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
"mplex" => Self::Mplex,
"yamux" => Self::Yamux,
other => bail!("unknown muxer {other}"),
})
}
}
/// Supported security protocols by rust-libp2p.
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "kebab-case")]
#[derive(Clone, Debug)]
pub enum SecProtocol {
Noise,
Tls,
}
impl FromStr for SecProtocol {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
"noise" => Self::Noise,
"tls" => Self::Tls,
other => bail!("unknown security protocol {other}"),
})
}
}
#[derive(NetworkBehaviour)]
struct Behaviour {
ping: ping::Behaviour,
@ -242,8 +274,7 @@ struct Behaviour {
/// Helper function to get a ENV variable into an test parameter like `Transport`.
pub fn from_env<T>(env_var: &str) -> Result<T>
where
T: FromStr,
T::Err: std::error::Error + Send + Sync + 'static,
T: FromStr<Err = anyhow::Error>,
{
env::var(env_var)
.with_context(|| format!("{env_var} environment variable is not set"))?