tonneau_build/codegen/cel/functions/
uint.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 UInt;
12
13impl Function for UInt {
14 fn name(&self) -> &'static str {
15 "uint"
16 }
17
18 fn syntax(&self) -> &'static str {
19 "<this>.uint()"
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(ConstantCompiledExpr {
34 value: CelValue::cel_to_uint(value)?,
35 }))
36 }
37 CompiledExpr::Runtime(RuntimeCompiledExpr { expr, .. }) => {
38 Ok(CompiledExpr::Runtime(RuntimeCompiledExpr {
39 ty: CelType::CelValue,
40 expr: parse_quote!(::tonneau::__private::cel::CelValue::cel_to_uint(#expr)?),
41 }))
42 }
43 }
44 }
45}
46
47#[cfg(test)]
48#[cfg(feature = "prost")]
49#[cfg_attr(coverage_nightly, coverage(off))]
50mod tests {
51 use syn::parse_quote;
52 use tonneau_cel::CelValue;
53
54 use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
55 use crate::codegen::cel::functions::{Function, UInt};
56 use crate::codegen::cel::types::CelType;
57 use crate::extern_paths::ExternPaths;
58 use crate::path_set::PathSet;
59 use crate::types::{ProtoType, ProtoTypeRegistry, ProtoValueType};
60
61 #[test]
62 fn test_uint_syntax() {
63 let registry = ProtoTypeRegistry::new(
64 crate::Mode::Prost,
65 ExternPaths::new(crate::Mode::Prost),
66 PathSet::default(),
67 );
68 let compiler = Compiler::new(®istry);
69 insta::assert_debug_snapshot!(UInt.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
70 Err(
71 InvalidSyntax {
72 message: "missing this",
73 syntax: "<this>.uint()",
74 },
75 )
76 "#);
77
78 insta::assert_debug_snapshot!(UInt.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("13".into()))), &[])), @r"
79 Ok(
80 Constant(
81 ConstantCompiledExpr {
82 value: Number(
83 U64(
84 13,
85 ),
86 ),
87 },
88 ),
89 )
90 ");
91
92 insta::assert_debug_snapshot!(UInt.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
93 cel_parser::parse("1 + 1").unwrap(), ])), @r#"
95 Err(
96 InvalidSyntax {
97 message: "takes no arguments",
98 syntax: "<this>.uint()",
99 },
100 )
101 "#);
102 }
103
104 #[test]
105 #[cfg(not(valgrind))]
106 fn test_uint_runtime() {
107 let registry = ProtoTypeRegistry::new(
108 crate::Mode::Prost,
109 ExternPaths::new(crate::Mode::Prost),
110 PathSet::default(),
111 );
112 let compiler = Compiler::new(®istry);
113
114 let string_value = CompiledExpr::runtime(
115 CelType::Proto(ProtoType::Value(ProtoValueType::String)),
116 parse_quote!(input),
117 );
118
119 let output = UInt
120 .compile(CompilerCtx::new(compiler.child(), Some(string_value), &[]))
121 .unwrap();
122
123 insta::assert_snapshot!(postcompile::compile_str!(
124 postcompile::config! {
125 test: true,
126 dependencies: vec![
127 postcompile::Dependency::path("tonneau", "../tonneau"),
128 ],
129 },
130 quote::quote! {
131 fn to_int(input: &str) -> Result<::tonneau::__private::cel::CelValue<'_>, ::tonneau::__private::cel::CelError<'_>> {
132 Ok(#output)
133 }
134
135 #[test]
136 fn test_to_int() {
137 assert_eq!(to_int("55").unwrap(), ::tonneau::__private::cel::CelValueConv::conv(55));
138 }
139 },
140 ));
141 }
142}