clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use crate::Commands;
use super::resolve_json_ast;

pub(super) fn handle_memorize_cmd(
    command: &Commands,
    clm_root: &str,
    stdout: &mut String,
    stderr: &mut String,
) -> Option<i32> {
    match command {
        Commands::YamlLoadOnce { file } => {
            match clm_rust_core::yaml::yaml_load_once(file, clm_root) {
                Ok(ast) => {
                    stdout.push_str(&ast);
                    stdout.push('\n');
                    Some(0)
                }
                Err(e) => {
                    stderr.push_str(&format!("[YAML_LOAD] ❌ Failed to load yaml: {}\n", e));
                    Some(1)
                }
            }
        }
        Commands::YamlGetField { json_or_file, jq_path } => {
            let json = resolve_json_ast(json_or_file);
            let field = clm_rust_core::yaml::yaml_get_field(&json, jq_path);
            stdout.push_str(&field);
            stdout.push('\n');
            Some(0)
        }
        Commands::YamlGetArrayLength { json_or_file, jq_path } => {
            let json = resolve_json_ast(json_or_file);
            let len = clm_rust_core::yaml::yaml_get_array_length(&json, jq_path);
            stdout.push_str(&len.to_string());
            stdout.push('\n');
            Some(0)
        }
        Commands::YamlGetArrayItem { json_or_file, jq_path, index } => {
            let json = resolve_json_ast(json_or_file);
            let item = clm_rust_core::yaml::yaml_get_array_item(&json, jq_path, *index);
            stdout.push_str(&item);
            stdout.push('\n');
            Some(0)
        }
        Commands::GetFileHandle { raw_path } => {
            let handle = clm_rust_core::fs_utils::get_file_handle(raw_path, Some(clm_root));
            stdout.push_str(&handle);
            stdout.push('\n');
            Some(0)
        }
        Commands::CheckAndRegisterFileMcard { file_path } => {
            match clm_rust_core::memorize::check_and_register_file_mcard(file_path, clm_root) {
                Ok(_) => Some(0),
                Err(e) => {
                    stderr.push_str(&format!("[CHECK_REGISTER] ❌ Error: {}\n", e));
                    Some(1)
                }
            }
        }
        Commands::MemorizeMcard { mcard_file, pillar_pos, pillar, handle, json } => {
            let actual_pillar = if pillar != "auto" {
                pillar.as_str()
            } else {
                pillar_pos.as_deref().unwrap_or(pillar.as_str())
            };
            let result = (|| -> Result<(i32, String, usize), String> {
                let bytes = if mcard_file == "-" {
                    let mut bytes = Vec::new();
                    std::io::Read::read_to_end(&mut std::io::stdin(), &mut bytes).map_err(|error| error.to_string())?;
                    bytes
                } else { std::fs::read(mcard_file).map_err(|error| error.to_string())? };
                let record = clm_rust_core::layer0::mcard_record::MCardRecord::create(&bytes, None)?;
                let exit_code = clm_rust_core::memorize::memorize_mcard_bytes(&bytes, actual_pillar, clm_root,
                    std::path::Path::new(mcard_file).file_name().and_then(|value| value.to_str()))?;
                if let Some(handle) = handle {
                    let conn = clm_rust_core::db::get_connection(actual_pillar, clm_root).map_err(|error| error.to_string())?;
                    clm_rust_core::tape::register_handle(&conn, handle, &record.hash, &record.g_time)?;
                }
                Ok((exit_code, record.hash, bytes.len()))
            })();
            match result {
                Ok((exit_code, hash, byte_count)) => {
                    if *json {
                        let envelope = serde_json::json!({
                            "status": "success",
                            "data": {
                                "hash": hash,
                                "bytes": byte_count,
                                "handle": handle,
                                "pillar": actual_pillar,
                            },
                            "meta": {
                                "runtime": "rust",
                                "version": env!("CARGO_PKG_VERSION"),
                            }
                        });
                        stdout.push_str(&serde_json::to_string_pretty(&envelope).unwrap_or_default());
                        stdout.push('\n');
                        Some(0)
                    } else {
                        Some(exit_code)
                    }
                }
                Err(e) => {
                    if *json {
                        let envelope = serde_json::json!({
                            "status": "failure",
                            "error": {
                                "code": "STORAGE_ERROR",
                                "message": e.to_string(),
                            },
                            "meta": {
                                "runtime": "rust",
                                "version": env!("CARGO_PKG_VERSION"),
                            }
                        });
                        stdout.push_str(&serde_json::to_string_pretty(&envelope).unwrap_or_default());
                        stdout.push('\n');
                    } else {
                        stderr.push_str(&format!("{}\n", e));
                    }
                    Some(1)
                }
            }
        }
        _ => None,
    }
}