use std::env;
pub fn petri_run_net(topo_file: &str, clm_root: &str) -> Result<String, String> {
petri_run_net_with_backend(
&env::var("CLM_RUNTIME_BACKEND").unwrap_or_default(),
topo_file,
clm_root,
)
}
fn petri_run_net_with_backend(
backend: &str,
topo_file: &str,
clm_root: &str,
) -> Result<String, String> {
if backend == "wasm" {
let yaml_body = std::fs::read_to_string(topo_file)
.map_err(|e| format!("failed to read topology for wasm backend: {}", e))?;
crate::cpn_wasm::run_net(&yaml_body)
} else {
crate::cpn::vm::petri_run_net(topo_file, clm_root)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn test_dispatch_wasm_backend() {
let dir = std::env::temp_dir().join("clm_runtime_dispatch_wasm_test");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("net.yaml");
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(
b"places:\n - id: start\n tokens: 1\n - id: end\n tokens: 0\ntransitions:\n - id: t1\n inputs: [start]\n outputs: [end]\n binding:\n runtime: echo\n content: hello\n"
)
.unwrap();
}
let output = petri_run_net_with_backend("wasm", path.to_str().unwrap(), "/tmp/clm_root").unwrap();
assert!(output.contains("signature"));
assert!(output.contains("epiplexity_metrics"));
}
#[test]
fn test_dispatch_native_backend_without_env() {
let dir = std::env::temp_dir().join("clm_runtime_dispatch_native_test");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("net.yaml");
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(b"not a valid topology\n").unwrap();
}
let result = petri_run_net_with_backend("native", path.to_str().unwrap(), "/tmp/clm_root");
assert!(result.is_err());
}
}