mf_expression/functions/
registry.rs1use crate::functions::defs::FunctionDefinition;
6use crate::functions::{DeprecatedFunction, FunctionKind, InternalFunction};
7use crate::functions::mf_function::MfFunctionRegistry;
8use nohash_hasher::{BuildNoHashHasher, IsEnabled};
9use std::cell::RefCell;
10use std::collections::HashMap;
11use std::rc::Rc;
12use strum::IntoEnumIterator;
13
14impl IsEnabled for InternalFunction {}
15impl IsEnabled for DeprecatedFunction {}
16
17pub struct FunctionRegistry {
21 internal_functions: HashMap<
23 InternalFunction,
24 Rc<dyn FunctionDefinition>,
25 BuildNoHashHasher<InternalFunction>,
26 >,
27 deprecated_functions: HashMap<
29 DeprecatedFunction,
30 Rc<dyn FunctionDefinition>,
31 BuildNoHashHasher<DeprecatedFunction>,
32 >,
33}
34
35impl FunctionRegistry {
36 thread_local!(
38 static INSTANCE: RefCell<FunctionRegistry> = RefCell::new(FunctionRegistry::new_internal())
39 );
40
41 pub fn get_definition(
50 kind: &FunctionKind
51 ) -> Option<Rc<dyn FunctionDefinition>> {
52 match kind {
53 FunctionKind::Internal(internal) => Self::INSTANCE
54 .with_borrow(|i| i.internal_functions.get(&internal).cloned()),
55 FunctionKind::Deprecated(deprecated) => {
56 Self::INSTANCE.with_borrow(|i| {
57 i.deprecated_functions.get(&deprecated).cloned()
58 })
59 },
60 FunctionKind::Closure(_) => None, FunctionKind::Mf(mf) => {
62 MfFunctionRegistry::get_definition(&mf.name)
63 },
64 }
65 }
66
67 fn new_internal() -> Self {
71 let internal_functions = InternalFunction::iter()
73 .map(|i| (i.clone(), (&i).into()))
74 .collect();
75
76 let deprecated_functions = DeprecatedFunction::iter()
78 .map(|i| (i.clone(), (&i).into()))
79 .collect();
80
81 Self { internal_functions, deprecated_functions }
82 }
83}