zen_expression/functions/
registry.rs1use crate::functions::defs::FunctionDefinition;
2use crate::functions::{DeprecatedFunction, FunctionKind, InternalFunction};
3use nohash_hasher::{BuildNoHashHasher, IsEnabled};
4use std::cell::RefCell;
5use std::collections::HashMap;
6use std::rc::Rc;
7use strum::IntoEnumIterator;
8
9impl IsEnabled for InternalFunction {}
10impl IsEnabled for DeprecatedFunction {}
11
12pub struct FunctionRegistry {
13 internal_functions:
14 HashMap<InternalFunction, Rc<dyn FunctionDefinition>, BuildNoHashHasher<InternalFunction>>,
15 deprecated_functions: HashMap<
16 DeprecatedFunction,
17 Rc<dyn FunctionDefinition>,
18 BuildNoHashHasher<DeprecatedFunction>,
19 >,
20}
21
22impl FunctionRegistry {
23 thread_local!(
24 static INSTANCE: RefCell<FunctionRegistry> = RefCell::new(FunctionRegistry::new_internal())
25 );
26
27 pub fn get_definition(kind: &FunctionKind) -> Option<Rc<dyn FunctionDefinition>> {
28 match kind {
29 FunctionKind::Internal(internal) => {
30 Self::INSTANCE.with_borrow(|i| i.internal_functions.get(&internal).cloned())
31 }
32 FunctionKind::Deprecated(deprecated) => {
33 Self::INSTANCE.with_borrow(|i| i.deprecated_functions.get(&deprecated).cloned())
34 }
35 FunctionKind::Closure(_) => None,
36 }
37 }
38
39 fn new_internal() -> Self {
40 let internal_functions = InternalFunction::iter()
41 .map(|i| (i.clone(), (&i).into()))
42 .collect();
43
44 let deprecated_functions = DeprecatedFunction::iter()
45 .map(|i| (i.clone(), (&i).into()))
46 .collect();
47
48 Self {
49 internal_functions,
50 deprecated_functions,
51 }
52 }
53}