tonneau_build/codegen/cel/functions/
bytes.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 Bytes;
12
13impl Function for Bytes {
14 fn name(&self) -> &'static str {
15 "bytes"
16 }
17
18 fn syntax(&self) -> &'static str {
19 "<this>.bytes()"
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_bytes(value)?))
34 }
35 CompiledExpr::Runtime(RuntimeCompiledExpr { expr, .. }) => Ok(CompiledExpr::runtime(
36 CelType::CelValue,
37 parse_quote!(::tonneau::__private::cel::CelValue::cel_to_bytes(#expr)?),
38 )),
39 }
40 }
41}
42
43#[cfg(test)]
44#[cfg(feature = "prost")]
45#[cfg_attr(coverage_nightly, coverage(off))]
46mod tests {
47 use quote::quote;
48 use syn::parse_quote;
49 use tonneau_cel::CelValue;
50
51 use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
52 use crate::codegen::cel::functions::{Bytes, Function};
53 use crate::codegen::cel::types::CelType;
54 use crate::extern_paths::ExternPaths;
55 use crate::path_set::PathSet;
56 use crate::types::{ProtoType, ProtoTypeRegistry, ProtoValueType};
57
58 #[test]
59 fn test_bytes_syntax() {
60 let registry = ProtoTypeRegistry::new(
61 crate::Mode::Prost,
62 ExternPaths::new(crate::Mode::Prost),
63 PathSet::default(),
64 );
65 let compiler = Compiler::new(®istry);
66 insta::assert_debug_snapshot!(Bytes.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
67 Err(
68 InvalidSyntax {
69 message: "missing this",
70 syntax: "<this>.bytes()",
71 },
72 )
73 "#);
74
75 insta::assert_debug_snapshot!(Bytes.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("hi".into()))), &[])), @r"
76 Ok(
77 Constant(
78 ConstantCompiledExpr {
79 value: Bytes(
80 Borrowed(
81 [
82 104,
83 105,
84 ],
85 ),
86 ),
87 },
88 ),
89 )
90 ");
91
92 insta::assert_debug_snapshot!(Bytes.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>.bytes()",
99 },
100 )
101 "#);
102 }
103
104 #[test]
105 #[cfg(not(valgrind))]
106 fn test_bytes_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 = Bytes
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! {
131 fn to_bytes(input: &str) -> Result<::tonneau::__private::cel::CelValue<'_>, ::tonneau::__private::cel::CelError<'_>> {
132 Ok(#output)
133 }
134
135 #[test]
136 fn test_to_bytes() {
137 assert_eq!(to_bytes("some string").unwrap(), ::tonneau::__private::cel::CelValueConv::conv(b"some string"));
138 }
139 },
140 ));
141 }
142}