tonneau_build/codegen/cel/functions/
int.rs1use syn::parse_quote;
2use tonneau_cel::CelValue;
3
4use super::Function;
5use crate::codegen::cel::compiler::{
6 CompileError, CompiledExpr, CompilerCtx, ConstantCompiledExpr, RuntimeCompiledExpr,
7};
8use crate::codegen::cel::types::CelType;
9
10#[derive(Debug, Clone, Default)]
11pub(crate) struct Int;
12
13impl Function for Int {
14 fn name(&self) -> &'static str {
15 "int"
16 }
17
18 fn syntax(&self) -> &'static str {
19 "<this>.int()"
20 }
21
22 fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
23 let Some(this) = ctx.this else {
24 return Err(CompileError::syntax("missing this", self));
25 };
26
27 if !ctx.args.is_empty() {
28 return Err(CompileError::syntax("takes no arguments", self));
29 }
30
31 match this.into_cel()? {
32 CompiledExpr::Constant(ConstantCompiledExpr { value }) => {
33 Ok(CompiledExpr::constant(CelValue::cel_to_int(value)?))
34 }
35 CompiledExpr::Runtime(RuntimeCompiledExpr { expr, .. }) => Ok(CompiledExpr::runtime(
36 CelType::CelValue,
37 parse_quote!(::tonneau::__private::cel::CelValue::cel_to_int(#expr)?),
38 )),
39 }
40 }
41}
42
43#[cfg(test)]
44#[cfg(feature = "prost")]
45#[cfg_attr(coverage_nightly, coverage(off))]
46mod tests {
47 use syn::parse_quote;
48 use tonneau_cel::CelValue;
49
50 use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
51 use crate::codegen::cel::functions::{Function, Int};
52 use crate::codegen::cel::types::CelType;
53 use crate::extern_paths::ExternPaths;
54 use crate::path_set::PathSet;
55 use crate::types::{ProtoType, ProtoTypeRegistry, ProtoValueType};
56
57 #[test]
58 fn test_int_syntax() {
59 let registry = ProtoTypeRegistry::new(
60 crate::Mode::Prost,
61 ExternPaths::new(crate::Mode::Prost),
62 PathSet::default(),
63 );
64 let compiler = Compiler::new(®istry);
65 insta::assert_debug_snapshot!(Int.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
66 Err(
67 InvalidSyntax {
68 message: "missing this",
69 syntax: "<this>.int()",
70 },
71 )
72 "#);
73
74 insta::assert_debug_snapshot!(Int.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("13".into()))), &[])), @r"
75 Ok(
76 Constant(
77 ConstantCompiledExpr {
78 value: Number(
79 I64(
80 13,
81 ),
82 ),
83 },
84 ),
85 )
86 ");
87
88 insta::assert_debug_snapshot!(Int.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
89 cel_parser::parse("1 + 1").unwrap(), ])), @r#"
91 Err(
92 InvalidSyntax {
93 message: "takes no arguments",
94 syntax: "<this>.int()",
95 },
96 )
97 "#);
98 }
99
100 #[test]
101 #[cfg(not(valgrind))]
102 fn test_int_runtime() {
103 let registry = ProtoTypeRegistry::new(
104 crate::Mode::Prost,
105 ExternPaths::new(crate::Mode::Prost),
106 PathSet::default(),
107 );
108 let compiler = Compiler::new(®istry);
109
110 let string_value = CompiledExpr::runtime(
111 CelType::Proto(ProtoType::Value(ProtoValueType::String)),
112 parse_quote!(input),
113 );
114
115 let output = Int
116 .compile(CompilerCtx::new(compiler.child(), Some(string_value), &[]))
117 .unwrap();
118
119 insta::assert_snapshot!(postcompile::compile_str!(
120 postcompile::config! {
121 test: true,
122 dependencies: vec![
123 postcompile::Dependency::path("tonneau", "../tonneau"),
124 ],
125 },
126 quote::quote! {
127 fn to_int(input: &str) -> Result<::tonneau::__private::cel::CelValue<'_>, ::tonneau::__private::cel::CelError<'_>> {
128 Ok(#output)
129 }
130
131 #[test]
132 fn test_to_int() {
133 assert_eq!(to_int("55").unwrap(), ::tonneau::__private::cel::CelValueConv::conv(55));
134 }
135 },
136 ));
137 }
138}