Skip to main content

surrealdb_expr/expr/convert/
analyzer_function.rs

1//! Lowering of a stored analyzer function name into an [`expr::Function`].
2//!
3//! Lives in core rather than beside the AST: `surrealdb-sql` sits below core,
4//! so it cannot name `expr` types.
5
6/// Converts a stored analyzer function reference into an executable function.
7pub fn function_from_storage(name: &str) -> crate::expr::Function {
8	if let Some(rest) = name.strip_prefix("mod::") {
9		let mut parts = rest.split("::");
10		let module = parts.next().unwrap_or_default().to_owned();
11		let sub = parts.collect::<Vec<_>>().join("::");
12		let sub = if sub.is_empty() {
13			None
14		} else {
15			Some(sub)
16		};
17		crate::expr::Function::Module(module, sub)
18	} else {
19		let name = name.strip_prefix("fn::").unwrap_or(name);
20		crate::expr::Function::Custom(name.to_owned())
21	}
22}
23
24#[cfg(test)]
25mod tests {
26	use super::*;
27
28	#[test]
29	fn function_from_storage_fn_legacy() {
30		assert!(matches!(
31			function_from_storage("foo::bar"),
32			crate::expr::Function::Custom(s) if s == "foo::bar"
33		));
34	}
35
36	#[test]
37	fn function_from_storage_fn_prefix() {
38		assert!(matches!(
39			function_from_storage("fn::foo::bar"),
40			crate::expr::Function::Custom(s) if s == "foo::bar"
41		));
42	}
43
44	#[test]
45	fn function_from_storage_mod() {
46		assert!(matches!(
47			function_from_storage("mod::demo::math::add"),
48			crate::expr::Function::Module(m, Some(s)) if m == "demo" && s == "math::add"
49		));
50	}
51}