1use uni_common::{Result, UniError, Value};
7
8use super::UniInner;
9
10pub struct Functions<'a> {
15 pub(crate) inner: &'a UniInner,
16}
17
18impl Functions<'_> {
19 pub fn register<F>(&self, name: &str, func: F) -> Result<()>
23 where
24 F: Fn(&[Value]) -> Result<Value> + Send + Sync + 'static,
25 {
26 let mut registry = self.inner.custom_functions.write().map_err(|_| {
27 UniError::Internal(anyhow::anyhow!("custom function registry lock poisoned"))
28 })?;
29 registry.register(name.to_string(), std::sync::Arc::new(func));
30 Ok(())
31 }
32
33 pub fn remove(&self, name: &str) -> Result<bool> {
35 let mut registry = self.inner.custom_functions.write().map_err(|_| {
36 UniError::Internal(anyhow::anyhow!("custom function registry lock poisoned"))
37 })?;
38 Ok(registry.remove(name))
39 }
40
41 pub fn list(&self) -> Vec<String> {
43 let registry = self.inner.custom_functions.read().unwrap();
44 registry.iter().map(|(name, _)| name.to_string()).collect()
45 }
46
47 pub fn count(&self) -> usize {
49 let registry = self.inner.custom_functions.read().unwrap();
50 registry.iter().count()
51 }
52}