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