Repository: juspay/hyperswitch
File: crates/router/src/core/payments/helpers.rs (Rust)

Code before:
        VaultFetchAction::FetchCardDetailsFromLocker
    } else {
        standard_flow()
    }
}

pub async fn is_ucs_enabled(state: &SessionState, config_key: &str) -> bool {
    let db = state.store.as_ref();
    let is_enabled = db
        .find_config_by_key_unwrap_or(config_key, Some("false".to_string()))
        .await
        .map_err(|err| {
            logger::error!(error = ?err, "Failed to fetch {config_key:?} UCS enabled config from DB");
        })
        .ok()
        .and_then(|config| {
            config.config.parse::<bool>()
                .map_err(|err| {
                    logger::error!(error = ?err, "Failed to parse {config_key:?} UCS enabled config");
                })
                .ok()
        })
        .unwrap_or(false);

    is_enabled
}

pub async fn should_execute_based_on_rollout(
    state: &SessionState,
    config_key: &str,
) -> RouterResult<bool> {
    let db = state.store.as_ref();

    match db.find_config_by_key(config_key).await {
        Ok(rollout_config) => match rollout_config.config.parse::<f64>() {
            Ok(rollout_percent) => {
                if !(0.0..=1.0).contains(&rollout_percent) {
                    logger::warn!(
                        rollout_percent,
                        "Rollout percent out of bounds. Must be between 0.0 and 1.0"
                    );
                    return Ok(false);
                }

                let sampled_value: f64 = rand::thread_rng().gen_range(0.0..1.0);
                Ok(sampled_value < rollout_percent)
            }
            Err(err) => {
                logger::error!(error = ?err, "Failed to parse rollout percent");
                Ok(false)
            }

Review comment (bug):
Nits:

1. You can remove the `ok()` calls.
2. You can use the `Display` impl of `config_key`, since it's a `&str`.
3. If you want to, you can log the value of `config.config` when we fail to parse it (as `bool`), so that we know the value that was present at that point of time.

```suggestion
pub async fn is_ucs_enabled(state: &SessionState, config_key: &str) -> bool {
    let db = state.store.as_ref();
    let is_enabled = db
        .find_config_by_key_unwrap_or(config_key, Some("false".to_string()))
        .await
        .map_err(|error| {
            logger::error!(?error, "Failed to fetch `{config_key}` UCS enabled config from DB");
        })
        .and_then(|config| {
            config.config.parse::<bool>()
                .map_err(|error| {
                    logger::error!(?error, "Failed to parse `{config_key}` UCS enabled config");
                })
        })
        .unwrap_or(false);

    is_enabled
}
```

Code after:
        VaultFetchAction::FetchCardDetailsFromLocker
    } else {
        standard_flow()
    }
}

pub async fn is_ucs_enabled(state: &SessionState, config_key: &str) -> bool {
    let db = state.store.as_ref();
    let is_enabled = db
        .find_config_by_key_unwrap_or(config_key, Some("false".to_string()))
        .await
        .map_err(|error| {
            logger::error!(
                ?error,
                "Failed to fetch `{config_key}` UCS enabled config from DB"
            );
        })
        .and_then(|config| {
            config.config.parse::<bool>().map_err(|error| {
                logger::error!(?error, "Failed to parse `{config_key}` UCS enabled config");
            })
        })
        .unwrap_or(false);

    is_enabled
}

pub async fn should_execute_based_on_rollout(
    state: &SessionState,
    config_key: &str,
) -> RouterResult<bool> {
    let db = state.store.as_ref();

    match db.find_config_by_key(config_key).await {
        Ok(rollout_config) => match rollout_config.config.parse::<f64>() {
            Ok(rollout_percent) => {
                if !(0.0..=1.0).contains(&rollout_percent) {
                    logger::warn!(
                        rollout_percent,
                        "Rollout percent out of bounds. Must be between 0.0 and 1.0"
                    );
                    return Ok(false);
                }

                let sampled_value: f64 = rand::thread_rng().gen_range(0.0..1.0);
                Ok(sampled_value < rollout_percent)
            }
            Err(err) => {
                logger::error!(error = ?err, "Failed to parse rollout percent");
                Ok(false)
            }