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

Code before:
fn convert_rule(rule: ast::Rule<ConnectorSelection>) -> Rule {
    let routing_type = match &rule.connector_selection {
        ConnectorSelection::Priority(_) => RoutingType::Priority,
        ConnectorSelection::VolumeSplit(_) => RoutingType::VolumeSplit,
    };

    Rule {
        name: rule.name,
        routing_type,
        output: convert_output(rule.connector_selection),
        statements: rule.statements.into_iter().map(convert_if_stmt).collect(),
    }
}

fn convert_if_stmt(stmt: ast::IfStatement) -> IfStatement {
    IfStatement {
        condition: stmt.condition.into_iter().map(convert_comparison).collect(),
        nested: stmt
            .nested
            .map(|v| v.into_iter().map(convert_if_stmt).collect()),
    }
}

fn convert_comparison(c: ast::Comparison) -> Comparison {
    Comparison {
        lhs: c.lhs,
        comparison: convert_comparison_type(c.comparison),
        value: convert_value(c.value),
        metadata: c.metadata,
    }
}

fn convert_comparison_type(ct: ast::ComparisonType) -> ComparisonType {
    match ct {
        ast::ComparisonType::Equal => ComparisonType::Equal,
        ast::ComparisonType::NotEqual => ComparisonType::NotEqual,
        ast::ComparisonType::LessThan => ComparisonType::LessThan,
        ast::ComparisonType::LessThanEqual => ComparisonType::LessThanEqual,
        ast::ComparisonType::GreaterThan => ComparisonType::GreaterThan,
        ast::ComparisonType::GreaterThanEqual => ComparisonType::GreaterThanEqual,
    }
}

#[allow(clippy::unimplemented)]
fn convert_value(v: ast::ValueType) -> ValueType {
    use ast::ValueType::*;
    match v {
        Number(n) => ValueType::Number(n.get_amount_as_i64().try_into().unwrap_or_default()),
        EnumVariant(e) => ValueType::EnumVariant(e),
        MetadataVariant(m) => ValueType::MetadataVariant(MetadataValue {
            key: m.key,

Review comment (bug):
Since you are collecting this as `Result<Vec<_>>` and not `Vec<Result<_,_>>`, failure of even one `convert_if_stmt` would result `Err` irrespective of other rules succeeding. is this expected? 

Code after:
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self {
            globals: HashMap::new(),
            default_selection: convert_output(p.default_selection),
            rules,
            metadata: Some(p.metadata),
        })
    }
}

fn convert_rule(rule: ast::Rule<ConnectorSelection>) -> RoutingResult<Rule> {
    let routing_type = match &rule.connector_selection {
        ConnectorSelection::Priority(_) => RoutingType::Priority,
        ConnectorSelection::VolumeSplit(_) => RoutingType::VolumeSplit,
    };

    Ok(Rule {
        name: rule.name,
        routing_type,
        output: convert_output(rule.connector_selection),
        statements: rule
            .statements
            .into_iter()
            .map(convert_if_stmt)
            .collect::<RoutingResult<Vec<IfStatement>>>()?,
    })
}

fn convert_if_stmt(stmt: ast::IfStatement) -> RoutingResult<IfStatement> {
    Ok(IfStatement {
        condition: stmt
            .condition
            .into_iter()
            .map(convert_comparison)
            .collect::<RoutingResult<Vec<Comparison>>>()?,

        nested: stmt
            .nested
            .map(|v| {
                v.into_iter()
                    .map(convert_if_stmt)
                    .collect::<RoutingResult<Vec<IfStatement>>>()
            })
            .transpose()?,
    })
}

fn convert_comparison(c: ast::Comparison) -> RoutingResult<Comparison> {
    Ok(Comparison {
        lhs: c.lhs,