Skip to main content

coreason_runtime_rust/
wasm_dispatcher.rs

1// Copyright (c) 2026 CoReason, Inc.
2// All rights reserved.
3
4use extism::Plugin;
5use std::path::Path;
6
7pub struct WasmDispatcher;
8
9impl WasmDispatcher {
10    pub fn execute_plugin(
11        plugin_path: &str,
12        function_name: &str,
13        input_data: &str,
14    ) -> Result<String, String> {
15        let wasm_file_path = Path::new(plugin_path);
16        if !wasm_file_path.exists() {
17            return Err(format!("Plugin file not found at path: {}", plugin_path));
18        }
19
20        let wasm_bytes = std::fs::read(wasm_file_path)
21            .map_err(|e| format!("Failed to read WASM bytes from file: {}", e))?;
22
23        // Initialize Extism Plugin in a sandboxed WASI environment
24        let mut plugin = Plugin::new(&wasm_bytes, [], true)
25            .map_err(|e| format!("Failed to initialize Extism plugin: {}", e))?;
26
27        // Call the target function in the guest module
28        let output = plugin
29            .call::<&str, &str>(function_name, input_data)
30            .map_err(|e| format!("Extism guest execution failed: {}", e))?;
31
32        Ok(output.to_string())
33    }
34}