Repository: juspay/hyperswitch
File: crates/router/src/core/payment_methods/cards.rs (Rust)

Code before:
        let mut skip_pre_routing: HashMap<
            common_enums::PaymentMethod,
            HashSet<common_enums::PaymentMethodType>,
        > = HashMap::new();

        for rule in merchant_cfg.skip_rules.iter() {
            skip_pre_routing
                .entry(rule.payment_method)
                .or_default()
                .extend(rule.payment_method_types.iter().copied());
        }

        let routing_enabled_pms = &router_consts::ROUTING_ENABLED_PAYMENT_METHODS;
        let routing_enabled_pm_types = &router_consts::ROUTING_ENABLED_PAYMENT_METHOD_TYPES;

        let mut chosen = api::SessionConnectorDatas::new(Vec::new());
        for intermediate in &response {
            let skip = skip_pre_routing
                .get(&intermediate.payment_method)
                .map(|config_pmt| config_pmt.contains(&intermediate.payment_method_type))
                .unwrap_or(false);

            let pm_allowed = routing_enabled_pms.contains(&intermediate.payment_method);
            let pmt_allowed = routing_enabled_pm_types.contains(&intermediate.payment_method_type);

            if (pm_allowed || pmt_allowed) && !skip {
                let connector_data = helpers::get_connector_data_with_token(
                    &state,
                    intermediate.connector.to_string(),
                    None,
                    intermediate.payment_method_type,
                )
                .change_context(errors::ApiErrorResponse::InternalServerError)
                .attach_printable("invalid connector name received")?;

                chosen.push(api::SessionConnectorData {
                    payment_method_sub_type: intermediate.payment_method_type,
                    payment_method_type: intermediate.payment_method,
                    connector: connector_data,
                    business_sub_label: None,
                });
            }
        }

        let sfr = SessionFlowRoutingInput {
            state: &state,
            country: billing_address.clone().and_then(|ad| ad.country),
            key_store: platform.get_processor().get_key_store(),
            merchant_account: platform.get_processor().get_account(),
            payment_attempt,
            payment_intent,

Review comment (suggestion):
Nit: 
This entire logic could moved into a function as in future more case match can come and this keep growing. Also we are using the opposite of the same logic to retain the response, so moving to a function would help.

example `should_perform_prerouting` could be used here and !`should_perform_prerouting` could be used in the retain logic instead of adding early return

Code after:
        let routing_enabled_pm_types = &router_consts::ROUTING_ENABLED_PAYMENT_METHOD_TYPES;

        let mut chosen = api::SessionConnectorDatas::new(Vec::new());
        for intermediate in &response {
            if perform_pre_routing(
                routing_enabled_pms,
                routing_enabled_pm_types,
                &intermediate.payment_method,
                &intermediate.payment_method_type,
                &skip_pre_routing,
            ) {
                let connector_data = helpers::get_connector_data_with_token(
                    &state,
                    intermediate.connector.to_string(),
                    None,
                    intermediate.payment_method_type,
                )
                .change_context(errors::ApiErrorResponse::InternalServerError)
                .attach_printable("invalid connector name received")?;

                chosen.push(api::SessionConnectorData {
                    payment_method_sub_type: intermediate.payment_method_type,
                    payment_method_type: intermediate.payment_method,
                    connector: connector_data,
                    business_sub_label: None,
                });
            }
        }

        let sfr = SessionFlowRoutingInput {
            state: &state,
            country: billing_address.clone().and_then(|ad| ad.country),
            key_store: platform.get_processor().get_key_store(),
            merchant_account: platform.get_processor().get_account(),
            payment_attempt,
            payment_intent,
            chosen,
        };
        let (result, routing_approach) = routing::perform_session_flow_routing(
            sfr,
            &business_profile,
            &enums::TransactionType::Payment,
        )
        .await
        .change_context(errors::ApiErrorResponse::InternalServerError)
        .attach_printable("error performing session flow routing")?;

        response.retain(|intermediate| {
            if !perform_pre_routing(
                routing_enabled_pms,
                routing_enabled_pm_types,