tinc_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 tinc_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(crate::Mode::Prost, ExternPaths::new(crate::Mode::Prost), PathSet::default());
44 let compiler = Compiler::new(®istry);
45 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
46 Err(
47 InvalidSyntax {
48 message: "missing this",
49 syntax: "<this>.bool()",
50 },
51 )
52 "#);
53
54 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[])), @r"
55 Ok(
56 Constant(
57 ConstantCompiledExpr {
58 value: Bool(
59 false,
60 ),
61 },
62 ),
63 )
64 ");
65
66 insta::assert_debug_snapshot!(Bool.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
67 cel_parser::parse("1 + 1").unwrap(), ])), @r#"
69 Err(
70 InvalidSyntax {
71 message: "takes no arguments",
72 syntax: "<this>.bool()",
73 },
74 )
75 "#);
76 }
77}