rusteal-codegen 0.3.0

Code generator: UHT JSON to Rust bindings and C++ wrappers for rusteal
Documentation
// The generated crate's `prelude`: the engine types a game reaches for first.
//
// A curated list, feature-gated exactly like the modules it draws from — never
// a glob of the generated modules, which would bring thousands of names and
// collide across extension traits.

use std::collections::HashSet;
use std::path::Path;

use crate::context::CodegenContext;

/// One prelude entry: the feature that must be on, and the `use` path.
const ENTRIES: &[(&str, &str)] = &[
    (
        "core",
        "crate::core_ue::{\n    \
             FVector, FVectorExt,\n    \
             FVector2D, FVector2DExt,\n    \
             FVector4, FVector4Ext,\n    \
             FQuat, FQuatExt,\n    \
             FRotator, FRotatorExt,\n    \
             FTransform, FTransformExt,\n    \
             FLinearColor, FLinearColorExt,\n    \
             FColor, FColorExt,\n    \
             FPlane, FPlaneExt,\n    \
             FBox2D, FBox2DExt,\n\
         }",
    ),
    ("core", "crate::manual::vector::OwnedFVectorExt"),
    ("core", "crate::manual::vector2d::OwnedFVector2DExt"),
    ("core", "crate::manual::vector4::OwnedFVector4Ext"),
    ("core", "crate::manual::quat::OwnedFQuatExt"),
    ("core", "crate::manual::rotator::OwnedFRotatorExt"),
    ("core", "crate::manual::transform::OwnedFTransformExt"),
    ("core", "crate::manual::linear_color::OwnedFLinearColorExt"),
    ("core", "crate::manual::color::OwnedFColorExt"),
    ("core", "crate::manual::plane::OwnedFPlaneExt"),
    ("core", "crate::manual::ue_box2d::OwnedFBox2DExt"),
    ("engine", "crate::engine::{Actor, ActorExt, World, WorldExt}"),
    ("engine", "crate::manual::world_ext::{WorldSpawnExt, find_object, load_object}"),
    ("input", "crate::input_core::FKey"),
    ("input", "crate::manual::fkey::FKeyExt"),
];

/// Write `src/prelude.rs`, keeping only the entries whose feature exists in
/// this generation (a feature no module maps to would never compile).
pub fn write_prelude(src_dir: &Path, ctx: &CodegenContext) {
    let available: HashSet<&str> = ctx
        .enabled_modules
        .iter()
        .filter_map(|module| ctx.feature_for_module(module))
        .collect();

    let mut out = String::from(
        "// Auto-generated by rusteal-codegen. Do not edit.\n\
         //\n\
         // One-import access to the engine types used most often:\n\
         //\n\
         //     use rusteal_runtime::prelude::*;\n\
         //     use bindings::prelude::*;\n\n",
    );
    for (feature, path) in ENTRIES {
        if !available.contains(feature) {
            continue;
        }
        out.push_str(&format!("#[cfg(feature = \"{feature}\")]\npub use {path};\n"));
    }

    let path = src_dir.join("prelude.rs");
    std::fs::write(&path, out)
        .unwrap_or_else(|e| panic!("Failed to write {}: {e}", path.display()));
}