clm_rust_core/
runtime_dispatch.rs1use std::env;
13
14pub fn petri_run_net(topo_file: &str, clm_root: &str) -> Result<String, String> {
20 petri_run_net_with_backend(
21 &env::var("CLM_RUNTIME_BACKEND").unwrap_or_default(),
22 topo_file,
23 clm_root,
24 )
25}
26
27fn petri_run_net_with_backend(
28 backend: &str,
29 topo_file: &str,
30 clm_root: &str,
31) -> Result<String, String> {
32 if backend == "wasm" {
33 let yaml_body = std::fs::read_to_string(topo_file)
34 .map_err(|e| format!("failed to read topology for wasm backend: {}", e))?;
35 crate::cpn_wasm::run_net(&yaml_body)
36 } else {
37 crate::cpn::vm::petri_run_net(topo_file, clm_root)
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use std::io::Write;
45
46 #[test]
47 fn test_dispatch_wasm_backend() {
48 let dir = std::env::temp_dir().join("clm_runtime_dispatch_wasm_test");
49 std::fs::create_dir_all(&dir).unwrap();
50 let path = dir.join("net.yaml");
51 {
52 let mut f = std::fs::File::create(&path).unwrap();
53 f.write_all(
54 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"
55 )
56 .unwrap();
57 }
58 let output = petri_run_net_with_backend("wasm", path.to_str().unwrap(), "/tmp/clm_root").unwrap();
59 assert!(output.contains("signature"));
60 assert!(output.contains("epiplexity_metrics"));
61 }
62
63 #[test]
64 fn test_dispatch_native_backend_without_env() {
65 let dir = std::env::temp_dir().join("clm_runtime_dispatch_native_test");
69 std::fs::create_dir_all(&dir).unwrap();
70 let path = dir.join("net.yaml");
71 {
72 let mut f = std::fs::File::create(&path).unwrap();
73 f.write_all(b"not a valid topology\n").unwrap();
74 }
75 let result = petri_run_net_with_backend("native", path.to_str().unwrap(), "/tmp/clm_root");
76 assert!(result.is_err());
77 }
78}