decy-core 2.2.0

Core transpilation pipeline for C-to-Rust conversion
Documentation
//! Full C++ Program Transpilation — Proven by Compilation
//!
//! Demonstrates: ALL C++ features together in one realistic program
//! Classes, namespaces, operators, inheritance, constructors, destructors,
//! methods with self access, arrays, conditionals
//! **Proves**: the entire feature set works together and compiles
//!
//! Run: `cargo run -p decy-core --example cpp_full_program_demo`

use decy_core::transpile;
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Full C++ Program -> Rust (All Features) ===\n");

    let cpp = r#"
extern "C" { void __trigger(); }

class Color {
public:
    int r;
    int g;
    int b;
    Color(int red, int green, int blue) : r(red), g(green), b(blue) {}
    int brightness() { return (r + g + b) / 3; }
    Color operator+(Color other) { Color c(0,0,0); return c; }
    bool operator==(Color other) { return r == other.r; }
    ~Color() {}
};

class Pixel : public Color {
public:
    int x;
    int y;
    Pixel(int px, int py, int cr, int cg, int cb) : Color(cr, cg, cb), x(px), y(py) {}
    int position_hash() { return x * 1000 + y; }
};

namespace gfx {
    class Canvas {
    public:
        int width;
        int height;
        Canvas(int w, int h) : width(w), height(h) {}
        int area() { return width * height; }
        int pixel_count() { return width * height; }
    };

    int clamp(int val, int lo, int hi) {
        if (val < lo) return lo;
        if (val > hi) return hi;
        return val;
    }

    namespace utils {
        int max(int a, int b) { return a > b ? a : b; }
        int min(int a, int b) { return a < b ? a : b; }
    }
}
"#;

    println!("C++ Input: {} lines", cpp.lines().count());
    println!("  Features: class, inheritance, operators, destructor, namespace, nested namespace\n");

    let rust = transpile(cpp)?;
    let clean = strip(&rust);
    println!("Rust Output: {} lines", clean.lines().count());
    println!("{}\n", clean);

    let ok = compile_check(&clean)?;
    if ok {
        println!("[PROVEN] Full C++ program compiles with rustc --edition 2021");
    } else {
        println!("[FAILED] Compilation error");
        std::process::exit(1);
    }

    // Verify all features present
    let checks = [
        ("pub struct Color", "Color class"),
        ("pub struct Pixel", "Pixel class"),
        ("base: Color", "Pixel inherits Color"),
        ("impl std::ops::Add<Color>", "Color operator+"),
        ("impl PartialEq for Color", "Color operator=="),
        ("impl Drop for Color", "Color destructor"),
        ("impl std::ops::Deref for Pixel", "Pixel Deref to Color"),
        ("pub mod gfx", "gfx namespace"),
        ("pub struct Canvas", "Canvas in gfx"),
        ("pub mod utils", "nested utils namespace"),
        ("fn clamp(", "gfx::clamp function"),
        ("fn max(", "utils::max function"),
    ];

    for (pattern, name) in &checks {
        assert!(clean.contains(pattern), "{} not found", name);
    }
    println!("[VERIFIED] All {} features confirmed", checks.len());

    Ok(())
}

fn strip(code: &str) -> String {
    let mut out = Vec::new();
    let mut skip = false;
    for line in code.lines() {
        if line.starts_with("fn __") { skip = true; continue; }
        if skip && line.trim() == "}" { skip = false; continue; }
        skip = false;
        if line.starts_with("static mut ERRNO") { continue; }
        out.push(line);
    }
    out.join("\n")
}

fn compile_check(code: &str) -> Result<bool, Box<dyn std::error::Error>> {
    let dir = tempfile::tempdir()?;
    let rs = dir.path().join("out.rs");
    let rlib = dir.path().join("out.rlib");
    std::fs::write(&rs, code)?;
    let o = Command::new("rustc")
        .args(["--edition", "2021", "--crate-type=lib", "-o"])
        .arg(&rlib).arg(&rs).output()?;
    Ok(!String::from_utf8_lossy(&o.stderr).lines().any(|l| l.starts_with("error")))
}