tonneau_build/codegen/cel/functions/
is_uri.rs1use syn::parse_quote;
2use tonneau_cel::CelValue;
3
4use super::Function;
5use crate::codegen::cel::compiler::{
6 CompileError, CompiledExpr, CompilerCtx, ConstantCompiledExpr,
7};
8use crate::codegen::cel::types::CelType;
9use crate::types::{ProtoType, ProtoValueType};
10
11#[derive(Debug, Clone, Default)]
12pub(crate) struct IsUri;
13
14impl Function for IsUri {
16 fn name(&self) -> &'static str {
17 "isUri"
18 }
19
20 fn syntax(&self) -> &'static str {
21 "<this>.isUri()"
22 }
23
24 fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
25 let Some(this) = &ctx.this else {
26 return Err(CompileError::syntax("missing this", self));
27 };
28
29 if !ctx.args.is_empty() {
30 return Err(CompileError::syntax("does not take any arguments", self));
31 }
32
33 let this = this.clone().into_cel()?;
34
35 match this {
36 CompiledExpr::Constant(ConstantCompiledExpr { value }) => {
37 Ok(CompiledExpr::constant(CelValue::cel_is_uri(value)?))
38 }
39 this => Ok(CompiledExpr::runtime(
40 CelType::Proto(ProtoType::Value(ProtoValueType::Bool)),
41 parse_quote! {{
42 ::tonneau::__private::cel::CelValue::cel_is_uri(
43 #this,
44 )?
45 }},
46 )),
47 }
48 }
49}
50
51#[cfg(test)]
52#[cfg(feature = "prost")]
53#[cfg_attr(coverage_nightly, coverage(off))]
54mod tests {
55 use syn::parse_quote;
56 use tonneau_cel::CelValue;
57
58 use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
59 use crate::codegen::cel::functions::{Function, IsUri};
60 use crate::codegen::cel::types::CelType;
61 use crate::extern_paths::ExternPaths;
62 use crate::path_set::PathSet;
63 use crate::types::{ProtoType, ProtoTypeRegistry, ProtoValueType};
64
65 #[test]
66 fn test_is_uri_syntax() {
67 let registry = ProtoTypeRegistry::new(
68 crate::Mode::Prost,
69 ExternPaths::new(crate::Mode::Prost),
70 PathSet::default(),
71 );
72 let compiler = Compiler::new(®istry);
73 insta::assert_debug_snapshot!(IsUri.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
74 Err(
75 InvalidSyntax {
76 message: "missing this",
77 syntax: "<this>.isUri()",
78 },
79 )
80 "#);
81
82 insta::assert_debug_snapshot!(IsUri.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("http://google.com".into()))), &[])), @r"
83 Ok(
84 Constant(
85 ConstantCompiledExpr {
86 value: Bool(
87 true,
88 ),
89 },
90 ),
91 )
92 ");
93
94 insta::assert_debug_snapshot!(IsUri.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("https://[fe80::4c1a:18ff:fe0b:7946]".into()))), &[])), @r"
95 Ok(
96 Constant(
97 ConstantCompiledExpr {
98 value: Bool(
99 true,
100 ),
101 },
102 ),
103 )
104 ");
105
106 insta::assert_debug_snapshot!(IsUri.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("not-a-uri".into()))), &[])), @r"
107 Ok(
108 Constant(
109 ConstantCompiledExpr {
110 value: Bool(
111 false,
112 ),
113 },
114 ),
115 )
116 ");
117
118 insta::assert_debug_snapshot!(IsUri.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
119 cel_parser::parse("1 + 1").unwrap(), ])), @r#"
121 Err(
122 InvalidSyntax {
123 message: "does not take any arguments",
124 syntax: "<this>.isUri()",
125 },
126 )
127 "#);
128 }
129
130 #[test]
131 #[cfg(not(valgrind))]
132 fn test_is_uri_runtime() {
133 let registry = ProtoTypeRegistry::new(
134 crate::Mode::Prost,
135 ExternPaths::new(crate::Mode::Prost),
136 PathSet::default(),
137 );
138 let compiler = Compiler::new(®istry);
139
140 let string_value = CompiledExpr::runtime(
141 CelType::Proto(ProtoType::Value(ProtoValueType::String)),
142 parse_quote!(input),
143 );
144
145 let output = IsUri
146 .compile(CompilerCtx::new(compiler.child(), Some(string_value), &[]))
147 .unwrap();
148
149 insta::assert_snapshot!(postcompile::compile_str!(
150 postcompile::config! {
151 test: true,
152 dependencies: vec![
153 postcompile::Dependency::path("tonneau", "../tonneau"),
154 ],
155 },
156 quote::quote! {
157 fn is_uri(input: &str) -> Result<bool, ::tonneau::__private::cel::CelError<'_>> {
158 Ok(#output)
159 }
160
161 #[test]
162 fn test_is_hostname() {
163 assert_eq!(is_uri("http://google.com").unwrap(), true);
164 assert_eq!(is_uri("not-a-uri").unwrap(), false);
165 }
166 },
167 ));
168 }
169}