clm_rust_core/evaluator/dispatch/
mod.rs1mod registry_core;
15mod registry_runtime;
16mod registry_infra;
17mod orchestrator;
18pub(crate) mod runtime;
19
20use std::fs;
21use std::path::Path;
22
23pub(crate) type InternalFn = fn(&str, &str) -> Result<(i32, String), String>;
26
27pub(super) fn get_clm_args(clm_file: &str) -> Result<Vec<String>, String> {
31 let content = fs::read_to_string(clm_file)
32 .map_err(|e| format!("Failed to read CLM file for args: {}", e))?;
33 let data: serde_yaml::Value = serde_yaml::from_str(&content)
34 .map_err(|e| format!("Failed to parse CLM YAML: {}", e))?;
35 let clm_data = data.get("clm").unwrap_or(&data);
36 let concrete_impl = clm_data.get("concrete_impl")
37 .ok_or_else(|| "No concrete_impl in CLM".to_string())?;
38 let process = concrete_impl.get("process")
39 .ok_or_else(|| "No process in concrete_impl".to_string())?;
40
41 if let Some(args_val) = process.get("args") {
42 if let Some(arr) = args_val.as_sequence() {
43 let mut result = Vec::new();
44 for item in arr {
45 if let Some(s) = item.as_str() {
46 result.push(s.to_string());
47 } else if let Some(i) = item.as_i64() {
48 result.push(i.to_string());
49 } else if let Some(b) = item.as_bool() {
50 result.push(b.to_string());
51 } else {
52 result.push("".to_string());
53 }
54 }
55 Ok(result)
56 } else {
57 Err("process.args is not a list/sequence".to_string())
58 }
59 } else {
60 Ok(Vec::new())
61 }
62}
63
64pub(super) fn resolve_json_ast_internal(json_or_file: &str, clm_root: &str) -> String {
66 let trimmed = json_or_file.trim();
67 if trimmed.starts_with('{') || trimmed.starts_with('[') {
68 trimmed.to_string()
69 } else {
70 let raw_path = Path::new(json_or_file);
71 let path = if raw_path.is_absolute() {
72 raw_path.to_path_buf()
73 } else {
74 Path::new(clm_root).join(json_or_file)
75 };
76 if path.exists() {
77 crate::yaml::parse_to_mcards(&path.to_string_lossy()).unwrap_or_else(|_| "null".to_string())
78 } else {
79 trimmed.to_string()
80 }
81 }
82}
83
84pub struct DispatchEntry {
88 pub module_path: &'static str,
89 pub func: InternalFn,
90}
91
92inventory::collect!(DispatchEntry);
93
94#[macro_export]
96macro_rules! register_clm_dispatch {
97 ($path:expr, $func:expr) => {
98 inventory::submit! {
99 $crate::evaluator::dispatch::DispatchEntry {
100 module_path: $path,
101 func: $func,
102 }
103 }
104 };
105}
106
107pub(crate) fn get_internal_registry(module_path: &str) -> Option<InternalFn> {
114 static REGISTRY: std::sync::OnceLock<std::collections::HashMap<&'static str, InternalFn>> = std::sync::OnceLock::new();
115 let map = REGISTRY.get_or_init(|| {
116 let mut m = std::collections::HashMap::with_capacity(256);
117 for entry in inventory::iter::<DispatchEntry> {
118 m.insert(entry.module_path, entry.func);
119 }
120 m
121 });
122
123 if let Some(&func) = map.get(module_path) {
124 return Some(func);
125 }
126
127 registry_core::dispatch(module_path)
128 .or_else(|| registry_runtime::dispatch(module_path))
129 .or_else(|| registry_infra::dispatch(module_path))
130}
131
132pub(crate) use orchestrator::try_internal_dispatch;
135#[allow(unused_imports)]
136pub(crate) use orchestrator::try_internal_dispatch_with_context;