tinc_build/codegen/cel/functions/
dyn_.rs

1use syn::parse_quote;
2
3use super::Function;
4use crate::codegen::cel::compiler::{CompileError, CompiledExpr, CompilerCtx};
5use crate::codegen::cel::types::CelType;
6
7#[derive(Debug, Clone, Default)]
8pub(crate) struct Dyn;
9
10impl Function for Dyn {
11    fn name(&self) -> &'static str {
12        "dyn"
13    }
14
15    fn syntax(&self) -> &'static str {
16        "dyn(<expr>)"
17    }
18
19    fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
20        if ctx.this.is_some() {
21            return Err(CompileError::syntax("has this", self));
22        };
23
24        if ctx.args.len() != 1 {
25            return Err(CompileError::syntax("needs exactly 1 argument", self));
26        }
27
28        let result = ctx.resolve(&ctx.args[0])?;
29
30        let ty = match &result {
31            CompiledExpr::Constant(_) => CelType::CelValue,
32            CompiledExpr::Runtime(r) => r.ty.clone(),
33        };
34
35        Ok(CompiledExpr::runtime(ty, parse_quote!(#result)))
36    }
37}