Skip to main content

tonneau_build/codegen/cel/functions/
has.rs

1use super::Function;
2use crate::codegen::cel::compiler::{CompileError, CompiledExpr, CompilerCtx};
3
4#[derive(Debug, Clone, Default)]
5pub(crate) struct Has;
6
7// has(field-arg)
8impl Function for Has {
9    fn name(&self) -> &'static str {
10        "has"
11    }
12
13    fn syntax(&self) -> &'static str {
14        "has(<field accessor>)"
15    }
16
17    fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
18        if ctx.this.is_some() {
19            return Err(CompileError::syntax("function has no this", self));
20        };
21
22        if ctx.args.len() != 1 {
23            return Err(CompileError::syntax("invalid arguments", self));
24        }
25
26        let arg = ctx.resolve(&ctx.args[0]);
27
28        Ok(CompiledExpr::constant(arg.is_ok()))
29    }
30}
31
32#[cfg(test)]
33#[cfg(feature = "prost")]
34#[cfg_attr(coverage_nightly, coverage(off))]
35mod tests {
36    use tonneau_cel::CelValue;
37
38    use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
39    use crate::codegen::cel::functions::{Function, Has};
40    use crate::extern_paths::ExternPaths;
41    use crate::path_set::PathSet;
42    use crate::types::ProtoTypeRegistry;
43
44    #[test]
45    fn test_has_syntax() {
46        let registry = ProtoTypeRegistry::new(
47            crate::Mode::Prost,
48            ExternPaths::new(crate::Mode::Prost),
49            PathSet::default(),
50        );
51        let mut compiler = Compiler::new(&registry);
52        insta::assert_debug_snapshot!(Has.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
53        Err(
54            InvalidSyntax {
55                message: "invalid arguments",
56                syntax: "has(<field accessor>)",
57            },
58        )
59        "#);
60
61        insta::assert_debug_snapshot!(Has.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("hi".into()))), &[])), @r#"
62        Err(
63            InvalidSyntax {
64                message: "function has no this",
65                syntax: "has(<field accessor>)",
66            },
67        )
68        "#);
69
70        insta::assert_debug_snapshot!(Has.compile(CompilerCtx::new(compiler.child(), None, &[
71            cel_parser::parse("x").unwrap(),
72        ])), @r"
73        Ok(
74            Constant(
75                ConstantCompiledExpr {
76                    value: Bool(
77                        false,
78                    ),
79                },
80            ),
81        )
82        ");
83
84        compiler.add_variable("x", CompiledExpr::constant(CelValue::Null));
85
86        insta::assert_debug_snapshot!(Has.compile(CompilerCtx::new(compiler.child(), None, &[
87            cel_parser::parse("x").unwrap(),
88        ])), @r"
89        Ok(
90            Constant(
91                ConstantCompiledExpr {
92                    value: Bool(
93                        true,
94                    ),
95                },
96            ),
97        )
98        ");
99    }
100}