pub mod conversions;
pub mod ext;
pub mod extensions;
pub mod kubernetes;
pub mod optional;
pub mod strings;
pub mod temporal;
use cel::common::ast::CallExpr;
use walrus::InstrSeqBuilder;
use crate::compiler::context::{CompilerContext, CompilerEnv};
pub fn compile_named_function(
func_name: &str,
call_expr: &CallExpr,
body: &mut InstrSeqBuilder,
env: &CompilerEnv,
ctx: &CompilerContext,
module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
if optional::compile_optional_function(func_name, call_expr, body, env, ctx, module)? {
return Ok(());
}
match func_name {
"bind"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "cel")
) =>
{
ext::bind::compile_cel_bind(call_expr, body, env, ctx, module)
}
"block"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "cel")
) =>
{
ext::block::compile_cel_block(call_expr, body, env, ctx, module)
}
"index"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "cel")
) =>
{
ext::block::compile_cel_index(call_expr, body, env, ctx, module)
}
"iterVar"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "cel")
) =>
{
ext::block::compile_cel_iter_var(call_expr, body, env, ctx, module)
}
"accuVar"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "cel")
) =>
{
ext::block::compile_cel_accu_var(call_expr, body, env, ctx, module)
}
"exists" | "all" | "existsOne" | "exists_one"
if call_expr.args.len() == 3 && call_expr.target.is_some() =>
{
ext::comprehensions::compile_two_var_comprehension(
func_name, call_expr, body, env, ctx, module,
)
}
"transformList" | "transformMap" | "transformMapEntry"
if (call_expr.args.len() == 3 || call_expr.args.len() == 4)
&& call_expr.target.is_some() =>
{
ext::comprehensions::compile_two_var_comprehension(
func_name, call_expr, body, env, ctx, module,
)
}
"contains" | "intersects" | "equivalent"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "sets")
) =>
{
ext::sets::compile_ext_sets_function(func_name, call_expr, body, env, ctx, module)
}
"size" | "startsWith" | "endsWith" | "contains" | "matches" => {
strings::compile_string_function(func_name, call_expr, body, env, ctx, module)
}
"replace" | "extract" | "extractAll"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "regex")
) =>
{
ext::regex::compile_ext_regex_function(func_name, call_expr, body, env, ctx, module)
}
"lowerAscii" | "upperAscii" | "trim" | "charAt" | "replace" | "split" | "substring"
| "format" | "quote" => {
ext::strings::compile_ext_string_function(func_name, call_expr, body, env, ctx, module)
}
"reverse" => {
use crate::compiler::helpers::compile_call_unary;
compile_call_unary(
call_expr,
"reverse",
ferricel_types::functions::RuntimeFunction::ReversePoly,
body,
env,
ctx,
module,
)
}
"range"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "lists")
) =>
{
ext::lists::compile_list_range(call_expr, body, env, ctx, module)
}
"encode" | "decode"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "base64")
) =>
{
ext::encoders::compile_ext_encoder_function(
func_name, call_expr, body, env, ctx, module,
)
}
"greatest" | "least" | "ceil" | "floor" | "round" | "trunc" | "abs" | "sign" | "isInf"
| "isNaN" | "isFinite" | "bitOr" | "bitAnd" | "bitXor" | "bitNot" | "bitShiftLeft"
| "bitShiftRight" | "sqrt"
if matches!(
&call_expr.target,
Some(t) if matches!(&t.expr, cel::common::ast::Expr::Ident(name) if name == "math")
) =>
{
ext::math::compile_ext_math_function(func_name, call_expr, body, env, ctx, module)
}
"join" | "distinct" | "flatten" | "slice" | "sort" | "sortBy" | "first" | "last" => {
ext::lists::compile_ext_list_function(func_name, call_expr, body, env, ctx, module)
}
"indexOf" | "lastIndexOf" => {
ext::poly::compile_index_of_dispatch(func_name, call_expr, body, env, ctx, module)
}
"timestamp" | "duration" | "getFullYear" | "getMonth" | "getDate" | "getDayOfMonth"
| "getDayOfWeek" | "getDayOfYear" | "getHours" | "getMinutes" | "getSeconds"
| "getMilliseconds" => {
temporal::compile_temporal_function(func_name, call_expr, body, env, ctx, module)
}
"string" | "int" | "uint" | "double" | "bytes" | "bool" | "type" | "dyn" => {
conversions::compile_conversion_function(func_name, call_expr, body, env, ctx, module)
}
"isSorted" | "sum" | "min" | "max" => kubernetes::lists::compile_k8s_list_function(
func_name, call_expr, body, env, ctx, module,
),
"find" | "findAll" => kubernetes::regex::compile_k8s_regex_function(
func_name, call_expr, body, env, ctx, module,
),
"url" | "isURL" | "getScheme" | "getHost" | "getHostname" | "getPort"
| "getEscapedPath" | "getQuery" => {
kubernetes::url::compile_k8s_url_function(func_name, call_expr, body, env, ctx, module)
}
"cidr" | "isCIDR" | "masked" | "prefixLength" | "containsIP" | "containsCIDR" => {
kubernetes::cidr::compile_k8s_cidr_function(
func_name, call_expr, body, env, ctx, module,
)
}
"ip" if call_expr.target.is_some() && call_expr.args.is_empty() => {
kubernetes::cidr::compile_k8s_cidr_function(
func_name, call_expr, body, env, ctx, module,
)
}
"ip"
| "isIP"
| "isCanonical"
| "family"
| "isUnspecified"
| "isLoopback"
| "isLinkLocalMulticast"
| "isLinkLocalUnicast"
| "isGlobalUnicast" => {
kubernetes::ip::compile_k8s_ip_function(func_name, call_expr, body, env, ctx, module)
}
"isSemver" | "semver" | "major" | "minor" | "patch" => {
kubernetes::semver::compile_k8s_semver_function(
func_name, call_expr, body, env, ctx, module,
)
}
"isLessThan" | "isGreaterThan" | "compareTo" => {
kubernetes::quantity::compile_k8s_quantity_function(
func_name, call_expr, body, env, ctx, module,
)
}
"quantity" | "isQuantity" | "sign" | "isInteger" | "asInteger" | "asApproximateFloat"
| "add" | "sub" => kubernetes::quantity::compile_k8s_quantity_function(
func_name, call_expr, body, env, ctx, module,
),
"named"
| "validate"
| "dns1123Label"
| "dns1123Subdomain"
| "dns1035Label"
| "qualifiedName"
| "dns1123LabelPrefix"
| "dns1123SubdomainPrefix"
| "dns1035LabelPrefix"
| "labelValue"
| "uri"
| "uuid"
| "byte"
| "date"
| "datetime" => kubernetes::format::compile_k8s_format_function(
func_name, call_expr, body, env, ctx, module,
),
_ => extensions::compile_extension_call(call_expr, body, env, ctx, module),
}
}