Skip to main content

clm_rust_core/layer0/
classifier.rs

1use serde_json::Value;
2use crate::yaml::evaluate_jq;
3
4pub fn classify(json_ast: &Value) -> String {
5    let has_abstract = evaluate_jq(
6        json_ast,
7        ".clm.abstract_spec.context // .abstract_spec.context",
8    );
9    let has_concrete = evaluate_jq(
10        json_ast,
11        ".clm.concrete_impl.process.type // .concrete_impl.process.type // \
12         .clm.concrete_impl.process_behavior_exec.dispatcher // .concrete_impl.process_behavior_exec.dispatcher // \
13         .clm.concrete_impl.runtime // .concrete_impl.runtime // \
14         .clm.concrete.runtime // .concrete.runtime",
15    );
16
17    let abs_str = match has_abstract {
18        Value::String(s) => s,
19        Value::Null => "".to_string(),
20        other => other.to_string(),
21    };
22    
23    let conc_str = match has_concrete {
24        Value::String(s) => s,
25        Value::Null => "".to_string(),
26        other => other.to_string(),
27    };
28
29    if !abs_str.is_empty() && !conc_str.is_empty() {
30        "Function".to_string()
31    } else {
32        "Number".to_string()
33    }
34}
35
36pub fn detect_dialect(json_ast: &Value, classification: &str) -> String {
37    if classification != "Function" {
38        return "UNKNOWN".to_string();
39    }
40
41    // Dialect M (Process type)
42    let process_type = evaluate_jq(
43        json_ast,
44        ".clm.concrete_impl.process.type // .concrete_impl.process.type",
45    );
46    let p_type_str = match process_type {
47        Value::String(s) => s,
48        Value::Null => "".to_string(),
49        other => other.to_string(),
50    };
51    if !p_type_str.is_empty() {
52        return format!("M:{}", p_type_str);
53    }
54
55    // Dialect A (Dispatcher)
56    let disp = evaluate_jq(
57        json_ast,
58        ".clm.concrete_impl.process_behavior_exec.dispatcher // .concrete_impl.process_behavior_exec.dispatcher",
59    );
60    let disp_str = match disp {
61        Value::String(s) => s,
62        Value::Null => "".to_string(),
63        other => other.to_string(),
64    };
65    if !disp_str.is_empty() {
66        return format!("A:{}", disp_str);
67    }
68
69    // Dialect B (Runtime)
70    let rt = evaluate_jq(
71        json_ast,
72        ".clm.concrete_impl.runtime // .concrete_impl.runtime",
73    );
74    let rt_str = match rt {
75        Value::String(s) => s,
76        Value::Null => "".to_string(),
77        other => other.to_string(),
78    };
79    if !rt_str.is_empty() {
80        let op = evaluate_jq(
81            json_ast,
82            ".clm.concrete_impl.operation // .concrete_impl.operation",
83        );
84        let op_str = match op {
85            Value::String(s) => s,
86            Value::Null => "invoke".to_string(),
87            other => other.to_string(),
88        };
89        return format!("B:{}:{}", rt_str, op_str);
90    }
91
92    // Dialect C (Concrete runtime)
93    let crt = evaluate_jq(
94        json_ast,
95        ".clm.concrete.runtime // .concrete.runtime",
96    );
97    let crt_str = match crt {
98        Value::String(s) => s,
99        Value::Null => "".to_string(),
100        other => other.to_string(),
101    };
102    if !crt_str.is_empty() {
103        let cop = evaluate_jq(
104            json_ast,
105            ".clm.concrete.operation // .concrete.operation",
106        );
107        let cop_str = match cop {
108            Value::String(s) => s,
109            Value::Null => "invoke".to_string(),
110            other => other.to_string(),
111        };
112        return format!("C:{}:{}", crt_str, cop_str);
113    }
114
115    "UNKNOWN".to_string()
116}