use syn::parse_quote;
use tinc_cel::CelValue;
use super::Function;
use crate::codegen::cel::compiler::{CompileError, CompiledExpr, CompilerCtx, ConstantCompiledExpr};
use crate::codegen::cel::types::CelType;
use crate::types::{ProtoType, ProtoValueType};
#[derive(Debug, Clone, Default)]
pub(crate) struct IsUuid;
impl Function for IsUuid {
fn name(&self) -> &'static str {
"isUuid"
}
fn syntax(&self) -> &'static str {
"<this>.isUuid()"
}
fn compile(&self, ctx: CompilerCtx) -> Result<CompiledExpr, CompileError> {
let Some(this) = &ctx.this else {
return Err(CompileError::syntax("missing this", self));
};
if !ctx.args.is_empty() {
return Err(CompileError::syntax("does not take any arguments", self));
}
let this = this.clone().into_cel()?;
match this {
CompiledExpr::Constant(ConstantCompiledExpr { value }) => {
Ok(CompiledExpr::constant(CelValue::cel_is_uuid(value)?))
}
this => Ok(CompiledExpr::runtime(
CelType::Proto(ProtoType::Value(ProtoValueType::Bool)),
parse_quote! {{
::tinc::__private::cel::CelValue::cel_is_uuid(
#this,
)?
}},
)),
}
}
}
#[cfg(test)]
#[cfg(feature = "prost")]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use syn::parse_quote;
use tinc_cel::CelValue;
use crate::codegen::cel::compiler::{CompiledExpr, Compiler, CompilerCtx};
use crate::codegen::cel::functions::{Function, IsUuid};
use crate::codegen::cel::types::CelType;
use crate::types::{ProtoType, ProtoTypeRegistry, ProtoValueType};
#[test]
fn test_is_uuid_syntax() {
let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
let compiler = Compiler::new(®istry);
insta::assert_debug_snapshot!(IsUuid.compile(CompilerCtx::new(compiler.child(), None, &[])), @r#"
Err(
InvalidSyntax {
message: "missing this",
syntax: "<this>.isUuid()",
},
)
"#);
insta::assert_debug_snapshot!(IsUuid.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("xdd".into()))), &[])), @r"
Ok(
Constant(
ConstantCompiledExpr {
value: Bool(
false,
),
},
),
)
");
insta::assert_debug_snapshot!(IsUuid.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::String("e470fd2a-f176-40cd-9ea1-9858deade3e1".into()))), &[])), @r"
Ok(
Constant(
ConstantCompiledExpr {
value: Bool(
true,
),
},
),
)
");
insta::assert_debug_snapshot!(IsUuid.compile(CompilerCtx::new(compiler.child(), Some(CompiledExpr::constant(CelValue::List(Default::default()))), &[
cel_parser::parse("1 + 1").unwrap(), ])), @r#"
Err(
InvalidSyntax {
message: "does not take any arguments",
syntax: "<this>.isUuid()",
},
)
"#);
}
#[test]
#[cfg(not(valgrind))]
fn test_is_uuid_runtime() {
let registry = ProtoTypeRegistry::new(crate::Mode::Prost, crate::extern_paths::ExternPaths::new(crate::Mode::Prost));
let compiler = Compiler::new(®istry);
let string_value =
CompiledExpr::runtime(CelType::Proto(ProtoType::Value(ProtoValueType::String)), parse_quote!(input));
let output = IsUuid
.compile(CompilerCtx::new(compiler.child(), Some(string_value), &[]))
.unwrap();
insta::assert_snapshot!(postcompile::compile_str!(
postcompile::config! {
test: true,
dependencies: vec![
postcompile::Dependency::workspace("tinc"),
],
},
quote::quote! {
fn is_uuid(input: &str) -> Result<bool, ::tinc::__private::cel::CelError<'_>> {
Ok(#output)
}
#[test]
fn test_is_hostname() {
assert_eq!(is_uuid("e470fd2a-f176-40cd-9ea1-9858deade3e1").unwrap(), true);
assert_eq!(is_uuid("not-a-uri").unwrap(), false);
}
},
));
}
}