use uni_common::{Result, UniError, Value};
use super::UniInner;
pub struct Functions<'a> {
pub(crate) inner: &'a UniInner,
}
impl Functions<'_> {
pub fn register<F>(&self, name: &str, func: F) -> Result<()>
where
F: Fn(&[Value]) -> Result<Value> + Send + Sync + 'static,
{
let mut registry = self.inner.custom_functions.write().map_err(|_| {
UniError::Internal(anyhow::anyhow!("custom function registry lock poisoned"))
})?;
registry.register(name.to_string(), std::sync::Arc::new(func));
Ok(())
}
pub fn remove(&self, name: &str) -> Result<bool> {
let mut registry = self.inner.custom_functions.write().map_err(|_| {
UniError::Internal(anyhow::anyhow!("custom function registry lock poisoned"))
})?;
Ok(registry.remove(name))
}
pub fn list(&self) -> Vec<String> {
let registry = self.inner.custom_functions.read().unwrap();
registry.iter().map(|(name, _)| name.to_string()).collect()
}
pub fn count(&self) -> usize {
let registry = self.inner.custom_functions.read().unwrap();
registry.iter().count()
}
}