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