clm_rust_core/evaluator/dispatch/
mod.rs1pub mod catalog;
15pub mod catalog_ids;
16mod registry_core;
17mod registry_runtime;
18mod registry_infra;
19#[path = "registry_infra_testing.rs"]
21mod registry_testing;
22mod orchestrator_intercepts;
23mod orchestrator_invariants;
24mod orchestrator_pipeline;
25mod orchestrator_routes;
26mod orchestrator;
27pub(crate) mod runtime;
28
29use std::fs;
30use std::path::Path;
31
32pub(crate) type InternalFn = fn(&str, &str) -> Result<(i32, String), String>;
35
36pub(super) fn get_clm_args(clm_file: &str) -> Result<Vec<String>, String> {
40 let content = fs::read_to_string(clm_file)
41 .map_err(|e| format!("Failed to read CLM file for args: {}", e))?;
42 let data: serde_yaml::Value = serde_yaml::from_str(&content)
43 .map_err(|e| format!("Failed to parse CLM YAML: {}", e))?;
44 let clm_data = data.get("clm").unwrap_or(&data);
45 let concrete_impl = clm_data.get("concrete_impl")
46 .ok_or_else(|| "No concrete_impl in CLM".to_string())?;
47 let process = concrete_impl.get("process")
48 .ok_or_else(|| "No process in concrete_impl".to_string())?;
49
50 if let Some(args_val) = process.get("args") {
51 if let Some(arr) = args_val.as_sequence() {
52 let mut result = Vec::new();
53 for item in arr {
54 if let Some(s) = item.as_str() {
55 result.push(s.to_string());
56 } else if let Some(i) = item.as_i64() {
57 result.push(i.to_string());
58 } else if let Some(b) = item.as_bool() {
59 result.push(b.to_string());
60 } else {
61 result.push("".to_string());
62 }
63 }
64 Ok(result)
65 } else {
66 Err("process.args is not a list/sequence".to_string())
67 }
68 } else {
69 Ok(Vec::new())
70 }
71}
72
73pub(super) fn resolve_json_ast_internal(json_or_file: &str, clm_root: &str) -> String {
75 let trimmed = json_or_file.trim();
76 if trimmed.starts_with('{') || trimmed.starts_with('[') {
77 trimmed.to_string()
78 } else {
79 let raw_path = Path::new(json_or_file);
80 let path = if raw_path.is_absolute() {
81 raw_path.to_path_buf()
82 } else {
83 Path::new(clm_root).join(json_or_file)
84 };
85 if path.exists() {
86 crate::yaml::parse_to_mcards(&path.to_string_lossy()).unwrap_or_else(|_| "null".to_string())
87 } else {
88 trimmed.to_string()
89 }
90 }
91}
92
93pub struct DispatchEntry {
97 pub module_path: &'static str,
98 pub func: InternalFn,
99}
100
101inventory::collect!(DispatchEntry);
102
103#[macro_export]
105macro_rules! register_clm_dispatch {
106 ($path:expr, $func:expr) => {
107 inventory::submit! {
108 $crate::evaluator::dispatch::DispatchEntry {
109 module_path: $path,
110 func: $func,
111 }
112 }
113 };
114}
115
116const TESTING_ROUTES: &[&str] = &["clm_rust_core::integration::", "clm_rust_core::test::", "clm_rust_core::prove::"];
120
121pub(crate) fn get_internal_registry(module_path: &str) -> Option<InternalFn> {
127 registry_core::dispatch(module_path)
128 .or_else(|| registry_runtime::dispatch(module_path))
129 .or_else(|| registry_infra::dispatch(module_path))
130 .or_else(|| testing_registry(module_path))
131}
132
133fn testing_registry(module_path: &str) -> Option<InternalFn> {
134 if !TESTING_ROUTES.iter().any(|prefix| module_path.starts_with(prefix)) {
135 return None;
136 }
137 static REGISTRY: std::sync::OnceLock<std::collections::HashMap<&'static str, InternalFn>> = std::sync::OnceLock::new();
138 let map = REGISTRY.get_or_init(|| {
139 let mut m = std::collections::HashMap::with_capacity(256);
140 for entry in inventory::iter::<DispatchEntry> {
141 m.insert(entry.module_path, entry.func);
142 }
143 m
144 });
145
146 map.get(module_path).copied().or_else(|| registry_testing::dispatch_testing_verification(module_path))
147}
148
149pub(crate) use orchestrator::try_internal_dispatch;
152#[allow(unused_imports)]
153pub(crate) use orchestrator::try_internal_dispatch_with_context;