hlx 1.2.5

Configuration language designed specifically for ml/ai/data systems
Documentation
use std::fs;
use std::path::Path;
use crate::dna::mds::loader::BinaryLoader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("๐Ÿงช Testing HLX to HLXB conversion and binary loading...");
    println!("๐Ÿ“ก Server should be running on http://localhost:4592");
    println!("\n๐Ÿ“ฅ Test 1: Downloading and converting HLX to HLXB");
    let hlx_url = "http://localhost:4592/test_config.hlx";
    let temp_hlxb_path = "/tmp/test_converted.hlxb";
    let curl_output = std::process::Command::new("curl")
        .args(&["-s", "-o", temp_hlxb_path, hlx_url])
        .output()?;
    if !curl_output.status.success() {
        println!("โŒ Failed to download HLX file from server");
        return Ok(());
    }
    if !Path::new(temp_hlxb_path).exists() {
        println!("โŒ Downloaded file doesn't exist");
        return Ok(());
    }
    let metadata = fs::metadata(temp_hlxb_path)?;
    println!("โœ… Downloaded {} bytes", metadata.len());
    println!("\n๐Ÿ”„ Test 2: Loading binary with BinaryLoader");
    let loader = BinaryLoader::new();
    match loader.load_file(Path::new(temp_hlxb_path)) {
        Ok(binary) => {
            println!("โœ… Binary loaded successfully!");
            println!(
                "   ๐Ÿ“Š Magic: {} {} {} {}", binary.magic[0] as char, binary.magic[1] as
                char, binary.magic[2] as char, binary.magic[3] as char
            );
            println!("   ๐Ÿ”ข Version: {}", binary.version);
            println!("   ๐Ÿ“ฆ Sections: {}", binary.data_sections.len());
            println!(
                "   ๐Ÿ“ Metadata: {} bytes created at {}", binary.metadata.source_hash,
                binary.metadata.created_at
            );
            if binary.magic == [b'H', b'L', b'X', b'B'] {
                println!("โœ… Magic bytes are correct (HLXB)");
            } else {
                println!("โŒ Magic bytes are incorrect");
            }
            if binary.version == 1 {
                println!("โœ… Binary version is correct");
            } else {
                println!("โŒ Binary version is incorrect: {}", binary.version);
            }
            println!("\n๐Ÿ”„ Test 3: Decompiling binary back to source");
            use crate::dna::compiler::Compiler;
            let compiler = Compiler::new(crate::dna::compiler::OptimizationLevel::Two);
            match compiler.decompile(&binary) {
                Ok(source) => {
                    println!("โœ… Successfully decompiled!");
                    println!("   ๐Ÿ“œ Decompiled source (first 100 chars):");
                    println!(
                        "   \"{}\"", & source.chars().take(100).collect::< String > ()
                    );
                    fs::remove_file(temp_hlxb_path)?;
                    println!("\n๐Ÿงน Cleaned up temporary file");
                    println!(
                        "\n๐ŸŽ‰ SUCCESS: Complete HLX โ†” HLXB conversion cycle works!"
                    );
                    println!("   โœ… HLX file served and converted to HLXB");
                    println!("   โœ… HLXB binary loaded by rlib BinaryLoader");
                    println!("   โœ… Binary format is valid and complete");
                    println!("   โœ… Binary can be decompiled back to source");
                    println!(
                        "   ๐Ÿ“š The rlib binary loading functionality is working perfectly!"
                    );
                }
                Err(e) => {
                    println!("โŒ Failed to decompile: {:?}", e);
                    let _ = fs::remove_file(temp_hlxb_path);
                }
            }
        }
        Err(e) => {
            println!("โŒ Failed to load binary: {:?}", e);
            match fs::read_to_string(temp_hlxb_path) {
                Ok(content) => {
                    println!("   ๐Ÿ“„ File content (first 200 chars):");
                    println!(
                        "   \"{}\"", content.chars().take(200).collect::< String > ()
                    );
                }
                Err(_) => {
                    println!("   ๐Ÿ“ฆ Binary content (first 50 bytes):");
                    let content = fs::read(temp_hlxb_path)?;
                    for (i, &byte) in content.iter().take(50).enumerate() {
                        if i % 10 == 0 {
                            print!("   ")
                        }
                        print!("{:02x} ", byte);
                        if i % 10 == 9 {
                            println!()
                        }
                    }
                    println!();
                }
            }
            let _ = fs::remove_file(temp_hlxb_path);
        }
    }
    Ok(())
}