clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use serde_json::Value;
use std::fs;
use std::path::Path;
use quick_xml::de::from_str as xml_from_str;

pub fn yaml_load_once(yaml_file: &str, clm_root: &str) -> crate::error::Result<String> {
    let path = Path::new(yaml_file);
    if !path.exists() {
        return Err(crate::error::ClmError::Evaluation(format!("File not found: {}", yaml_file)));
    }
    
    let content_hash = crate::fs_utils::compute_stripped_file_hash(path)
        .map_err(|e| crate::error::ClmError::Evaluation(format!("Failed to compute file hash: {}", e)))?;
    let ast_handle = format!("ast:{}", content_hash);

    if let Some(cached_content) = crate::db::resolve_handle_content(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &ast_handle)? {
        if let Ok(Some(current_hash)) = crate::db::resolve_handle_hash(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &ast_handle) {
            crate::cache::setter_mcard_cache_handle(&ast_handle, &current_hash);

        }
        return Ok(cached_content);
    }

    let json_str = parse_to_mcards(yaml_file)?;
    
    crate::memorize::memoize_clm_state(yaml_file, &content_hash, &json_str, clm_root)
        .map_err(crate::error::ClmError::Evaluation)?;

    Ok(json_str)
}

pub fn parse_to_mcards(file_path: &str) -> crate::error::Result<String> {
    let path = Path::new(file_path);
    if !path.exists() {
        return Err(crate::error::ClmError::Evaluation(format!("File not found: {}", file_path)));
    }
    
    let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
    let mut parsed: Value = match extension {
        "xml" => {
            let xml_content = fs::read_to_string(path)?;
            xml_from_str(&xml_content)
                .map_err(|e| crate::error::ClmError::Evaluation(format!("Failed to parse XML: {}", e)))?
        }
        "jsonl" => {
            let content = fs::read_to_string(path)?;
            let mut array = Vec::new();
            for line in content.lines() {
                let trimmed = line.trim();
                if !trimmed.is_empty() {
                    let val: Value = serde_json::from_str(trimmed)?;
                    array.push(val);
                }
            }
            Value::Array(array)
        }
        _ => {
            // Assume YAML
            let yaml_content = fs::read_to_string(path)?;
            match serde_yaml::from_str(&yaml_content) {
                Ok(val) => val,
                Err(original_err) => {
                    if let Some(block_content) = extract_yaml_code_block(&yaml_content) {
                        serde_yaml::from_str(&block_content)?
                    } else {
                        return Err(crate::error::ClmError::YamlParse(original_err));
                    }
                }
            }
        }
    };

    normalize_numbers(&mut parsed);

    Ok(serde_json::to_string_pretty(&parsed)?)
}

fn normalize_numbers(value: &mut Value) {
    match value {
        Value::Number(num) => {
            if let Some(f) = num.as_f64()
                && f.fract() == 0.0 {
                    if f >= 0.0 {
                        if let Some(u) = num.as_u64().or(Some(f as u64)) {
                            *value = Value::Number(serde_json::Number::from(u));
                        }
                    } else {
                        if let Some(i) = num.as_i64().or(Some(f as i64)) {
                            *value = Value::Number(serde_json::Number::from(i));
                        }
                    }
                }
        }
        Value::Object(map) => {
            for val in map.values_mut() {
                normalize_numbers(val);
            }
        }
        Value::Array(arr) => {
            for val in arr.iter_mut() {
                normalize_numbers(val);
            }
        }
        _ => {}
    }
}


pub fn evaluate_jq(json: &Value, query: &str) -> Value {
    // Split by "//" for fallback alternatives
    let parts: Vec<&str> = query.split("//").map(|s| s.trim()).collect();
    for part in parts {
        let val = evaluate_single_query(json, part);
        if !val.is_null() && val != Value::String("null".to_string()) {
            return val;
        }
    }
    Value::Null
}

fn evaluate_single_query(json: &Value, query: &str) -> Value {
    let mut current = json;
    
    // Normalize path by stripping starting dot
    let trimmed = if query.starts_with('.') {
        &query[1..]
    } else {
        query
    };
    
    if trimmed.is_empty() {
        return current.clone();
    }

    // Split components by dot, but we need to handle brackets
    // e.g., "mcard.content.pending_cases[0]"
    let components = trimmed.split('.');
    for comp in components {
        if comp.is_empty() {
            continue;
        }
        
        // Check for bracket indexing, e.g. "pending_cases[0]" or "process[0]"
        if comp.contains('[') && comp.ends_with(']') {
            let parts: Vec<&str> = comp.split('[').collect();
            if parts.len() == 2 {
                let key = parts[0];
                let idx_str = parts[1].trim_end_matches(']');
                if let Ok(idx) = idx_str.parse::<usize>() {
                    // Navigate to key
                    let sub_val = if key.is_empty() {
                        current
                    } else if let Some(v) = current.get(key) {
                        v
                    } else {
                        return Value::Null;
                    };
                    
                    // Navigate to index
                    if let Some(array_val) = sub_val.get(idx) {
                        current = array_val;
                    } else {
                        return Value::Null;
                    }
                } else {
                    return Value::Null;
                }
            } else {
                return Value::Null;
            }
        } else {
            // Direct key access
            if let Some(v) = current.get(comp) {
                current = v;
            } else {
                return Value::Null;
            }
        }
    }
    current.clone()
}

pub fn yaml_get_field(json_str: &str, jq_path: &str) -> String {
    if json_str.is_empty() || json_str == "null" {
        return "null".to_string();
    }
    let parsed: Value = match serde_json::from_str(json_str) {
        Ok(v) => v,
        Err(_) => return "null".to_string(),
    };
    
    let resolved = evaluate_jq(&parsed, jq_path);
    match resolved {
        Value::Null => "null".to_string(),
        Value::String(s) => s,
        other => other.to_string(),
    }
}

pub fn yaml_get_array_length(json_str: &str, jq_path: &str) -> String {
    if json_str.is_empty() || json_str == "null" {
        return "0".to_string();
    }
    let parsed: Value = match serde_json::from_str(json_str) {
        Ok(v) => v,
        Err(_) => return "0".to_string(),
    };
    
    let resolved = evaluate_jq(&parsed, jq_path);
    match resolved {
        Value::Array(arr) => arr.len().to_string(),
        _ => "0".to_string(),
    }
}

pub fn yaml_get_array_item(json_str: &str, jq_path: &str, index: usize) -> String {
    if json_str.is_empty() || json_str == "null" {
        return "null".to_string();
    }
    let parsed: Value = match serde_json::from_str(json_str) {
        Ok(v) => v,
        Err(_) => return "null".to_string(),
    };
    
    let full_path = format!("{}[{}]", jq_path, index);
    let resolved = evaluate_jq(&parsed, &full_path);
    match resolved {
        Value::Null => "null".to_string(),
        Value::String(s) => s,
        other => other.to_string(),
    }
}

fn extract_yaml_code_block(content: &str) -> Option<String> {
    let mut in_block = false;
    let mut block_lines = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("```") {
            if in_block {
                return Some(block_lines.join("\n"));
            } else {
                let lang = trimmed.trim_start_matches('`').trim().to_lowercase();
                if lang.is_empty() || lang == "yaml" || lang == "yml" || lang == "json" {
                    in_block = true;
                }
            }
        } else if in_block {
            block_lines.push(line);
        }
    }
    None
}