From 907094972c6c75a78bfda878795b26357b20250b Mon Sep 17 00:00:00 2001 From: Owen Nelson Date: Wed, 10 Jul 2024 09:43:03 -0700 Subject: [PATCH] Bridge: add itertools for partition_map OSS Review: itertools This popular extension crate adds new methods for iterators. Useful in this case as it cleans up a previous call to `fold()` designed to partition a collection into 2 collections that can't be expressed cleanly with a plain `partition()`. The selected version is not the newest, but it aligns with a selected version already included in the project transitively. --- bridge/Cargo.lock | 1 + bridge/svix-bridge/Cargo.toml | 1 + bridge/svix-bridge/src/main.rs | 27 +++++++++++---------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/bridge/Cargo.lock b/bridge/Cargo.lock index 537de404f..7fe70f320 100644 --- a/bridge/Cargo.lock +++ b/bridge/Cargo.lock @@ -4161,6 +4161,7 @@ dependencies = [ "deno_core", "enum_dispatch", "http 0.2.12", + "itertools", "once_cell", "opentelemetry", "opentelemetry-otlp", diff --git a/bridge/svix-bridge/Cargo.toml b/bridge/svix-bridge/Cargo.toml index cd36be910..322ef1b08 100644 --- a/bridge/svix-bridge/Cargo.toml +++ b/bridge/svix-bridge/Cargo.toml @@ -9,6 +9,7 @@ anyhow = "1" clap = { version = "4.2.4", features = ["env", "derive"] } axum = { version = "0.6", features = ["macros"] } enum_dispatch = "0.3" +itertools = "0.12.1" http = "0.2" once_cell = "1.18.0" opentelemetry = "0.22.0" diff --git a/bridge/svix-bridge/src/main.rs b/bridge/svix-bridge/src/main.rs index 6e3889766..9d6bd71f2 100644 --- a/bridge/svix-bridge/src/main.rs +++ b/bridge/svix-bridge/src/main.rs @@ -5,6 +5,7 @@ use std::{ }; use clap::Parser; +use itertools::{Either, Itertools}; use once_cell::sync::Lazy; use opentelemetry_otlp::WithExportConfig; use opentelemetry_sdk::{ @@ -361,22 +362,16 @@ async fn main() -> Result<()> { if cfg.receivers.is_empty() { tracing::warn!("No receivers configured.") } - let (webhook_receivers, poller_receivers) = cfg.receivers.into_iter().fold( - ( - Vec::::new(), - Vec::::new(), - ), - |mut acc, either| match either { - EitherReceiver::Webhook(x) => { - acc.0.push(x); - acc - } - EitherReceiver::Poller(y) => { - acc.1.push(y); - acc - } - }, - ); + let (webhook_receivers, poller_receivers): ( + Vec, + Vec, + ) = cfg + .receivers + .into_iter() + .partition_map(|either| match either { + EitherReceiver::Webhook(x) => Either::Left(x), + EitherReceiver::Poller(y) => Either::Right(y), + }); let webhook_receivers_fut = webhook_receiver::run(cfg.http_listen_address, webhook_receivers, xform_tx.clone());