tonneau_build/codegen/cel/functions/
bool.rs1use super::Function;
2use crate::codegen::cel::compiler::{CompileError, CompiledExpr, CompilerCtx};
3
4#[derive(Debug, Clone, Default)]
5pub(crate) struct Bool;
6
7impl Function for Bool {
8 fn name(&self) -> &'static str {
9 "bool"
10 }
11
12 fn syntax(&self) -> &'static str {
13 "<this>.bool()"
14 }
15
16 fn compile(&self, mut ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
17 let Some(this) = ctx.this.take() else {
18 return Err(CompileError::syntax("missing this", self));
19 };
20
21 if !ctx.args.is_empty() {
22 return Err(CompileError::syntax("takes no arguments", self));
23 }
24
25 Ok(this.into_bool(&ctx))
26 }
27}
28
29#[cfg(test)]
30#[cfg(feature = "prost")]
31#[cfg_attr(coverage_nightly, coverage(off))]
32mod tests {
33 use tonneau_cel::CelValue;
34
35 use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
36 use crate::codegen::cel::functions::{Bool, Function};
37 use crate::extern_paths::ExternPaths;
38 use crate::path_set::PathSet;
39 use crate::types::ProtoTypeRegistry;
40
41 #[test]
42 fn test_bool_syntax() {
43 let registry = ProtoTypeRegistry::new(
44 crate::Mode::Prost,
45 ExternPaths::new(crate::Mode::Prost),
46 PathSet::default(),
47 );
48 let compiler = Compiler::new(®istry);
49 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
50 Err(
51 InvalidSyntax {
52 message: "missing this",
53 syntax: "<this>.bool()",
54 },
55 )
56 "#);
57
58 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[])), @r"
59 Ok(
60 Constant(
61 ConstantCompiledExpr {
62 value: Bool(
63 false,
64 ),
65 },
66 ),
67 )
68 ");
69
70 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
71 cel_parser::parse("1 + 1").unwrap(), ])), @r#"
73 Err(
74 InvalidSyntax {
75 message: "takes no arguments",
76 syntax: "<this>.bool()",
77 },
78 )
79 "#);
80 }
81}