use cel::common::ast::CallExpr;
use ferricel_types::functions::RuntimeFunction;
use walrus::InstrSeqBuilder;
use crate::compiler::{
context::{CompilerContext, CompilerEnv},
expr::compile_expr,
helpers::{compile_call_binary, compile_call_ternary},
};
pub fn compile_k8s_regex_function(
func_name: &str,
call_expr: &CallExpr,
body: &mut InstrSeqBuilder,
env: &CompilerEnv,
ctx: &CompilerContext,
module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
match func_name {
"find" => compile_call_binary(
call_expr,
func_name,
RuntimeFunction::K8sRegexFind,
body,
env,
ctx,
module,
),
"findAll" => {
let arg_count = if call_expr.target.is_some() {
call_expr.args.len()
} else {
call_expr.args.len().saturating_sub(1)
};
match arg_count {
1 => {
if let Some(target) = &call_expr.target {
if call_expr.args.len() != 1 {
anyhow::bail!("findAll() method expects 1 argument");
}
compile_expr(&target.expr, body, env, ctx, module)?;
compile_expr(&call_expr.args[0].expr, body, env, ctx, module)?;
} else {
if call_expr.args.len() != 2 {
anyhow::bail!("findAll() function expects 2 arguments");
}
compile_expr(&call_expr.args[0].expr, body, env, ctx, module)?;
compile_expr(&call_expr.args[1].expr, body, env, ctx, module)?;
}
body.i64_const(-1);
body.call(env.get(RuntimeFunction::CreateInt));
body.call(env.get(RuntimeFunction::K8sRegexFindAllN));
Ok(())
}
2 => compile_call_ternary(
call_expr,
func_name,
RuntimeFunction::K8sRegexFindAllN,
body,
env,
ctx,
module,
),
n => anyhow::bail!("findAll() expects 1 or 2 arguments, got {}", n),
}
}
_ => anyhow::bail!("Unknown Kubernetes regex function: {}", func_name),
}
}