reifydb_extension/function/
wasm_loader.rs1use std::{fs, path::Path, sync::Arc};
7
8use reifydb_routine::function::registry::{Functions, FunctionsConfigurator};
9use reifydb_sdk::error::FFIError;
10use reifydb_type::Result;
11
12use super::wasm::WasmScalarFunction;
13
14pub fn register_wasm_scalar_functions_from_dir(
17 dir: &Path,
18 mut builder: FunctionsConfigurator,
19) -> Result<FunctionsConfigurator> {
20 let entries = fs::read_dir(dir).map_err(|e| {
21 FFIError::Other(format!("Failed to read WASM scalar function directory {}: {}", dir.display(), e))
22 })?;
23
24 for entry in entries {
25 let entry = entry.map_err(|e| FFIError::Other(format!("Failed to read directory entry: {}", e)))?;
26 let path = entry.path();
27
28 if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
29 continue;
30 }
31
32 let name = match path.file_stem().and_then(|s| s.to_str()) {
33 Some(n) => n.to_string(),
34 None => continue,
35 };
36
37 let wasm_bytes = fs::read(&path)
38 .map_err(|e| FFIError::Other(format!("Failed to read WASM file {}: {}", path.display(), e)))?;
39
40 builder = builder.register_function(Arc::new(WasmScalarFunction::new(name, wasm_bytes)));
41 }
42
43 Ok(builder)
44}
45
46pub fn load_wasm_scalar_functions_from_dir(dir: &Path) -> Result<Functions> {
48 Ok(register_wasm_scalar_functions_from_dir(dir, Functions::builder())?.configure())
49}