use crate::catalog_meta::{DeprecatedBy, ProcArgMeta};
mod aggregate;
mod graph;
mod list;
mod numeric;
mod predicate;
mod scalar;
mod spatial;
mod string;
mod temporal;
mod trig;
mod vector;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FunctionCategory {
Aggregating,
Database,
Graph,
List,
Logarithmic,
Numeric,
Path,
Predicate,
Scalar,
Spatial,
String,
Temporal,
Trigonometric,
Uuid,
Vector,
}
impl FunctionCategory {
pub(crate) const fn as_str(self) -> &'static str {
match self {
FunctionCategory::Aggregating => "Aggregating",
FunctionCategory::Database => "Database",
FunctionCategory::Graph => "Graph",
FunctionCategory::List => "List",
FunctionCategory::Logarithmic => "Logarithmic",
FunctionCategory::Numeric => "Numeric",
FunctionCategory::Path => "Path",
FunctionCategory::Predicate => "Predicate",
FunctionCategory::Scalar => "Scalar",
FunctionCategory::Spatial => "Spatial",
FunctionCategory::String => "String",
FunctionCategory::Temporal => "Temporal",
FunctionCategory::Trigonometric => "Trigonometric",
FunctionCategory::Uuid => "UUID",
FunctionCategory::Vector => "Vector",
}
}
}
#[derive(Clone, Copy)]
pub(crate) struct FunctionMeta {
pub(crate) name: &'static str,
pub(crate) category: FunctionCategory,
pub(crate) description: &'static str,
pub(crate) special_signature: Option<&'static str>,
pub(crate) args: &'static [ProcArgMeta],
pub(crate) return_type: &'static str,
pub(crate) deprecated_by: Option<DeprecatedBy>,
}
impl FunctionMeta {
pub(crate) const fn described(
name: &'static str,
category: FunctionCategory,
description: &'static str,
args: &'static [ProcArgMeta],
return_type: &'static str,
) -> Self {
Self {
name,
category,
description,
special_signature: None,
args,
return_type,
deprecated_by: None,
}
}
pub(crate) fn is_aggregating(&self) -> bool {
matches!(self.category, FunctionCategory::Aggregating)
}
}
const FUNCTION_META_TABLES: &[&[FunctionMeta]] = &[
scalar::ENTRIES,
list::ENTRIES,
numeric::ENTRIES,
trig::ENTRIES,
string::ENTRIES,
temporal::instant::ENTRIES,
temporal::truncate::ENTRIES,
temporal::gql::ENTRIES,
temporal::duration::ENTRIES,
temporal::epoch::ENTRIES,
temporal::format::ENTRIES,
aggregate::ENTRIES,
predicate::ENTRIES,
vector::ENTRIES,
spatial::ENTRIES,
graph::ENTRIES,
];
fn all_function_metas() -> impl Iterator<Item = &'static FunctionMeta> {
FUNCTION_META_TABLES.iter().flat_map(|table| table.iter())
}
pub(crate) fn function_metas(name: &str) -> impl Iterator<Item = &'static FunctionMeta> + use<'_> {
all_function_metas().filter(move |meta| meta.name.eq_ignore_ascii_case(name))
}
pub(crate) const CATALOG_ONLY_FUNCTIONS: &[&str] = &relate_core::GRAPH_SELECTOR_FUNCTION_NAMES;
pub fn builtin_function_deprecation(name: &str) -> Option<DeprecatedBy> {
function_metas(name).find_map(|meta| meta.deprecated_by)
}
#[cfg(test)]
mod tests {
use crate::catalog_meta::DeprecationForm;
use super::*;
#[test]
fn graph_selector_arities_match_the_catalog() {
for (name, arity) in relate_core::GRAPH_SELECTOR_FUNCTIONS {
let metas: Vec<&FunctionMeta> = function_metas(name).collect();
assert_eq!(metas.len(), 1, "{name} has no catalog entry");
assert_eq!(metas[0].args.len(), arity, "{name}");
assert!(CATALOG_ONLY_FUNCTIONS.contains(&name), "{name}");
}
}
#[test]
fn function_deprecation_lookup_reads_the_function_catalog() {
let replacement = builtin_function_deprecation("ID").expect("id is deprecated");
assert_eq!(
replacement.display(),
"elementId or consider using an application-generated id"
);
assert_eq!(replacement.form(), DeprecationForm::Identifier("elementId"));
assert!(builtin_function_deprecation("elementId").is_none());
assert!(builtin_function_deprecation("no.such.function").is_none());
}
#[test]
fn the_metadata_tables_cover_exactly_the_advertised_registry() {
let every_gate = relate_front::validate::every_name_gate();
let mut advertised: Vec<String> =
crate::show_catalog::advertised_function_names(every_gate)
.map(str::to_string)
.collect();
advertised.sort_unstable();
let mut catalogued: Vec<String> = all_function_metas()
.map(|meta| meta.name.to_ascii_lowercase())
.collect();
catalogued.sort_unstable();
catalogued.dedup();
assert_eq!(catalogued, advertised);
}
#[test]
fn arity_sibling_rows_share_one_deprecation() {
let mut by_name: Vec<(String, Option<DeprecatedBy>)> = all_function_metas()
.map(|meta| (meta.name.to_ascii_lowercase(), meta.deprecated_by))
.collect();
by_name.sort_unstable_by(|a, b| a.0.cmp(&b.0));
for pair in by_name.windows(2) {
if pair[0].0 == pair[1].0 {
assert_eq!(
pair[0].1, pair[1].1,
"{}'s arity rows disagree on their replacement",
pair[0].0
);
}
}
}
#[test]
fn aggregate_alias_display_columns_match_the_registry_keys() {
for &(alias, canonical, display) in relate_front::agg::AGGREGATE_ALIASES {
let metas: Vec<&FunctionMeta> = function_metas(canonical).collect();
assert_eq!(metas.len(), 1, "{alias} -> {canonical} has no unique row");
assert_eq!(
metas[0].name, display,
"{canonical}'s catalogue name disagrees with the alias table's display column"
);
}
}
}