use crate::catalog_meta::{DeprecatedBy, ProcArgMeta};
use crate::function_registry::{function_metas, FunctionMeta, CATALOG_ONLY_FUNCTIONS};
use crate::procedure_registry::BUILTIN_PROCEDURE_SIGNATURES;
use indexmap::IndexMap;
use relate_core::PreviewFeatures;
use relate_front::KNOWN_FUNCTIONS;
use relate_model::CypherValue;
type Row = IndexMap<String, CypherValue>;
const FUNCTION_ROLES_EXECUTION: &[&str] = &[
"PUBLIC",
"admin",
"architect",
"editor",
"publisher",
"reader",
];
const PROCEDURE_ROLES_EXECUTION: &[&str] = &["PUBLIC", "admin"];
const ADMIN_ROLES_EXECUTION: &[&str] = &["admin"];
const PROCEDURE_ROLES_BOOSTED: &[&str] = &["admin"];
fn role_list(roles: &[&str]) -> CypherValue {
crate::catalog_meta::string_list(roles.iter().map(|r| r.to_string()))
}
fn arg_meta_value(a: &ProcArgMeta) -> CypherValue {
let mut m = IndexMap::new();
m.insert("name".to_string(), CypherValue::Str(a.name.to_string()));
m.insert(
"description".to_string(),
CypherValue::Str(a.description.to_string()),
);
m.insert("type".to_string(), CypherValue::Str(a.type_str.to_string()));
m.insert("isDeprecated".to_string(), CypherValue::Bool(a.deprecated));
if let Some(d) = a.default {
m.insert("default".to_string(), CypherValue::Str(d.to_string()));
}
CypherValue::Map(m)
}
fn render_param_list(args: &[ProcArgMeta]) -> String {
args.iter()
.map(|a| match a.default {
Some(d) => format!("{} = {} :: {}", a.name, d, a.type_str),
None => format!("{} :: {}", a.name, a.type_str),
})
.collect::<Vec<_>>()
.join(", ")
}
fn render_function_signature(name: &str, args: &[ProcArgMeta], return_type: &str) -> String {
format!("{}({}) :: {}", name, render_param_list(args), return_type)
}
fn render_procedure_signature(name: &str, args: &[ProcArgMeta], returns: &[ProcArgMeta]) -> String {
let params = render_param_list(args);
if returns.is_empty() {
format!("{name}({params})")
} else {
let rets = returns
.iter()
.map(|r| format!("{} :: {}", r.name, r.type_str))
.collect::<Vec<_>>()
.join(", ");
format!("{name}({params}) :: ({rets})")
}
}
pub(crate) fn advertised_function_names(
preview: PreviewFeatures,
) -> impl Iterator<Item = &'static str> {
KNOWN_FUNCTIONS
.iter()
.copied()
.chain(relate_core::temporal::clock_spellings())
.chain(CATALOG_ONLY_FUNCTIONS.iter().copied())
.filter(move |&raw| relate_front::validate::function_passes_name_gate(raw, preview))
}
pub fn function_catalog_rows(preview: PreviewFeatures) -> Vec<Row> {
let mut metas: Vec<&'static FunctionMeta> = advertised_function_names(preview)
.flat_map(|raw| {
let metas: Vec<&'static FunctionMeta> = function_metas(raw).collect();
assert!(
!metas.is_empty(),
"no function_registry row for advertised `{raw}`"
);
metas
})
.collect();
metas.sort_by(|a, b| a.name.cmp(b.name));
metas.into_iter().map(function_row).collect()
}
fn deprecated_by_cell(deprecated_by: Option<DeprecatedBy>) -> CypherValue {
deprecated_by
.map(|deprecated| CypherValue::Str(deprecated.display().to_string()))
.unwrap_or(CypherValue::Null)
}
fn function_row(meta: &FunctionMeta) -> Row {
let mut r = Row::new();
r.insert("name".to_string(), CypherValue::Str(meta.name.to_string()));
r.insert(
"category".to_string(),
CypherValue::Str(meta.category.as_str().to_string()),
);
r.insert("isBuiltIn".to_string(), CypherValue::Bool(true));
r.insert(
"aggregating".to_string(),
CypherValue::Bool(meta.is_aggregating()),
);
r.insert(
"description".to_string(),
CypherValue::Str(meta.description.to_string()),
);
let signature = meta
.special_signature
.map(|tail| format!("{}{}", meta.name, tail))
.unwrap_or_else(|| render_function_signature(meta.name, meta.args, meta.return_type));
r.insert("signature".to_string(), CypherValue::Str(signature));
r.insert(
"argumentDescription".to_string(),
CypherValue::List(meta.args.iter().map(arg_meta_value).collect()),
);
r.insert(
"returnDescription".to_string(),
CypherValue::Str(meta.return_type.to_string()),
);
r.insert(
"rolesExecution".to_string(),
role_list(FUNCTION_ROLES_EXECUTION),
);
r.insert("rolesBoostedExecution".to_string(), role_list(&[]));
r.insert(
"isDeprecated".to_string(),
CypherValue::Bool(meta.deprecated_by.is_some()),
);
r.insert(
"deprecatedBy".to_string(),
deprecated_by_cell(meta.deprecated_by),
);
r
}
pub fn procedure_catalog_rows(preview: PreviewFeatures) -> Vec<Row> {
let mut listed: Vec<&'static crate::procedure_registry::ProcSignature> =
BUILTIN_PROCEDURE_SIGNATURES
.iter()
.filter(|signature| signature.listed)
.filter(|signature| {
crate::procedure_gate::procedure_preview_feature(signature.name)
.is_none_or(|feature| preview.enabled(feature))
})
.collect();
listed.sort_unstable_by(|a, b| a.name.cmp(b.name));
listed.into_iter().map(procedure_row).collect()
}
fn procedure_row(signature: &crate::procedure_registry::ProcSignature) -> Row {
let mut r = Row::new();
r.insert(
"name".to_string(),
CypherValue::Str(signature.name.to_string()),
);
r.insert(
"description".into(),
CypherValue::Str(signature.description.to_string()),
);
r.insert("mode".into(), CypherValue::Str(signature.mode.to_string()));
r.insert(
"worksOnSystem".into(),
CypherValue::Bool(signature.works_on_system),
);
r.insert("admin".into(), CypherValue::Bool(signature.admin));
r.insert(
"signature".into(),
CypherValue::Str(render_procedure_signature(
signature.name,
signature.args,
signature.returns,
)),
);
r.insert(
"argumentDescription".into(),
CypherValue::List(signature.args.iter().map(arg_meta_value).collect()),
);
r.insert(
"returnDescription".into(),
CypherValue::List(signature.returns.iter().map(arg_meta_value).collect()),
);
r.insert(
"rolesExecution".into(),
role_list(if signature.admin {
ADMIN_ROLES_EXECUTION
} else {
PROCEDURE_ROLES_EXECUTION
}),
);
r.insert(
"rolesBoostedExecution".into(),
role_list(PROCEDURE_ROLES_BOOSTED),
);
let deprecated = signature.deprecated_by.is_some();
r.insert("isDeprecated".into(), CypherValue::Bool(deprecated));
let mut option = IndexMap::new();
option.insert("deprecated".into(), CypherValue::Bool(deprecated));
r.insert("option".into(), CypherValue::Map(option));
r.insert(
"deprecatedBy".into(),
deprecated_by_cell(signature.deprecated_by),
);
r
}
#[cfg(test)]
mod tests;