polyplugc 0.1.1

CLI code generator for polyplug - generates type-safe bindings for multiple languages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Integration test: use polyplug_codegen library to generate Rust bindings,
//! compile them as a cdylib, load with libloading, dispatch `add(3, 5)` through
//! the interface, assert == 8.

#![allow(clippy::expect_used)]

use std::path::Path;
use std::path::PathBuf;
use std::process::Command;

use core::ffi::c_void;
use polyplug_abi::AbiError;
use polyplug_abi::AbiErrorCode;
use polyplug_abi::Array;
use polyplug_abi::BundleInitContext;
use polyplug_abi::DependencyInfo;
use polyplug_abi::GuestContractHandle;
use polyplug_abi::GuestContractInstance;
use polyplug_abi::GuestContractInterface;
use polyplug_abi::HostApi;
use polyplug_abi::PluginDescriptor;
use polyplug_abi::string_view_null;
use polyplug_codegen::{GenerateConfig, Lang, Side};
use polyplug_utils::BundleId;
use polyplugc::generate;

// ─── Helper: compile target dir ──────────────────────────────────────────────

/// Workspace root resolved from `CARGO_MANIFEST_DIR` (`crates/polyplug_codegen`).
fn workspace_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("parent of crates/polyplug_codegen")
        .parent()
        .expect("workspace root")
        .to_path_buf()
}

/// Platform-specific shared library filename for the generated plugin.
fn so_filename() -> &'static str {
    if cfg!(target_os = "macos") {
        "libcodegen_rust_test_plugin.dylib"
    } else if cfg!(target_os = "windows") {
        "codegen_rust_test_plugin.dll"
    } else {
        "libcodegen_rust_test_plugin.so"
    }
}

// ─── Helper: generate code using library API ─────────────────────────────────

/// Use polyplugc::generate() to generate Rust bindings.
fn generate_rust_bindings(api_toml: &Path, out_dir: &Path, side: Side) {
    let config = GenerateConfig {
        api_toml: api_toml.to_path_buf(),
        lang: Lang::Rust,
        side,
        out_dir: out_dir.to_path_buf(),
    };

    let output = generate(config).expect("polyplugc::generate failed");

    // Write generated files to disk
    for file in &output.files {
        let file_path = out_dir.join(&file.path);
        if let Some(parent) = file_path.parent() {
            std::fs::create_dir_all(parent).expect("failed to create parent dir");
        }
        std::fs::write(&file_path, &file.content).expect("failed to write generated file");
    }
}

// ─── Helper: write Cargo.toml for the generated cdylib ───────────────────────

/// Write a `Cargo.toml` for a cdylib crate that depends on `polyplug_abi`,
/// `polyplug_guest`, and `polyplug_utils`.
fn write_plugin_cargo_toml(crate_dir: &Path, guest_lib_path: &Path) {
    let abi_lib_path: PathBuf = workspace_root().join("crates").join("polyplug_abi");
    let utils_lib_path: PathBuf = workspace_root().join("crates").join("polyplug_utils");
    let content: String = format!(
        r#"[package]
name    = "codegen_rust_test_plugin"
version = "0.1.0"
edition = "2021"

[lib]
name      = "codegen_rust_test_plugin"
crate-type = ["cdylib"]

[dependencies]
polyplug_abi = {{ path = "{}" }}
polyplug_guest = {{ path = "{}" }}
polyplug_utils = {{ path = "{}" }}

[workspace]
"#,
        // Backslashes are invalid TOML escape sequences; forward slashes are valid
        // path separators on every platform (including Windows).
        abi_lib_path.to_string_lossy().replace('\\', "/"),
        guest_lib_path.to_string_lossy().replace('\\', "/"),
        utils_lib_path.to_string_lossy().replace('\\', "/")
    );
    let cargo_toml_path: PathBuf = crate_dir.join("Cargo.toml");
    std::fs::write(&cargo_toml_path, content).expect("failed to write plugin Cargo.toml");
}

// ─── Helper: write src/lib.rs for the generated cdylib ───────────────────────

/// Write a `src/lib.rs` that:
///   - Declares generated modules (types, contracts, interfaces) but NOT init.
///   - Defines `MyPlugin` and implements `TestAddGuestContract`.
///   - Exports a custom `polyplug_init` that sets `TEST_ADDER_IMPL` then registers the interface.
fn write_plugin_lib_rs(src_dir: &Path) {
    let content: &str = r#"// THIS FILE IS WRITTEN BY integration_codegen_rust TEST — DO NOT EDIT BY HAND

mod guest {
    pub mod types;
    pub mod contracts;
    pub mod interfaces;
}

#[allow(unused_imports)]
use polyplug_abi::AbiError;
use polyplug_abi::AbiErrorCode;
use polyplug_abi::PluginDescriptor;
use polyplug_guest::GuestError;
use polyplug_abi::HostApi;
use polyplug_abi::BundleInitContext;
use polyplug_abi::StringView;
use polyplug_abi::Version;
use polyplug_abi::string_view_null;
use guest::contracts::TestAddGuestContract;
use guest::types::AddArgs;
use guest::interfaces::TEST_ADDER_INTERFACE;
use polyplug_guest::HostContext;

struct MyPlugin;

impl TestAddGuestContract for MyPlugin {
    fn add(&self, args: &AddArgs) -> Result<u32, GuestError> {
        Ok(args.a.wrapping_add(args.b))
    }

    fn add_primitive(&self, a: u32, b: u32) -> Result<u32, GuestError> {
        Ok(a.wrapping_add(b))
    }

    fn version(&self) -> Result<StringView, GuestError> {
        Ok(StringView { ptr: b"1.0.0".as_ptr(), len: 5_usize })
    }

    fn reset(&self) -> Result<(), GuestError> {
        Ok(())
    }
}

/// Factory called by the generated `create_instance` for every host-created
/// instance. The implementation travels in `GuestContractInstance.data` —
/// no static storage.
#[no_mangle]
pub fn polyplug_create_test_adder(_host: HostContext) -> Box<dyn TestAddGuestContract> {
    Box::new(MyPlugin)
}

/// # Safety
/// `host` must be a valid non-null pointer provided by the host.
#[no_mangle]
pub unsafe extern "C" fn polyplug_init(
    host: *const HostApi,
    _ctx: *const BundleInitContext,
) -> AbiError {
    if host.is_null() {
        return AbiError { code: AbiErrorCode::Generic as u32, message: string_view_null() };
    }

    // SAFETY: host is non-null and valid per ABI contract.
    let host: &HostApi = unsafe { &*host };

    let desc: PluginDescriptor = PluginDescriptor {
        name: StringView { ptr: b"codegen_test_plugin".as_ptr(), len: 19_usize },
        contract_name: StringView { ptr: b"test.add".as_ptr(), len: 8_usize },
        version: Version { major: 1, minor: 0, patch: 0 },
    };

    // Out-param ABI: register_guest_contract writes its AbiError through a
    // trailing pointer and returns void; init surfaces it by value.
    let mut err: AbiError = AbiError::ok();
    // SAFETY: desc and TEST_ADDER_INTERFACE are 'static; host is valid; &mut err is valid.
    unsafe {
        (host.register_guest_contract)(
            host as *const _,
            &desc as *const PluginDescriptor,
            &TEST_ADDER_INTERFACE as *const _,
            &mut err as *mut AbiError,
        )
    };
    err
}
"#;
    let lib_rs_path: PathBuf = src_dir.join("lib.rs");
    std::fs::write(&lib_rs_path, content).expect("failed to write plugin src/lib.rs");
}

// ─── HostApi callback capturing the interface pointer ───────────────────────

// Captured interface pointer from the register_guest_contract callback, stored in a thread-local.
std::thread_local! {
    static CAPTURED_INTERFACE: core::cell::Cell<*const GuestContractInterface> =
        const { core::cell::Cell::new(core::ptr::null()) };
}

/// register_guest_contract callback that captures the interface pointer into `CAPTURED_INTERFACE`.
///
/// # Safety
/// `descriptor` and `interface` must be valid for the duration of the call.
unsafe extern "C" fn capture_interface_callback(
    _host: *const HostApi,
    _descriptor: *const PluginDescriptor,
    interface: *const GuestContractInterface,
    out_err: *mut AbiError,
) {
    CAPTURED_INTERFACE.with(|cell| cell.set(interface));
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

// ─── AddArgs mirrors the generated repr(C) struct ────────────────────────────

/// `AddArgs` — must match generated `types.rs` layout (`#[repr(C)]`).
#[repr(C)]
struct AddArgs {
    a: u32,
    b: u32,
}

// ─── HostApi stub functions ─────────────────────────────────────────────────

unsafe extern "C" fn stub_alloc(_host: *const HostApi, size: usize, align: usize) -> *mut u8 {
    polyplug_abi::ffi::polyplug_host_alloc(size, align)
}

unsafe extern "C" fn stub_free(_host: *const HostApi, ptr: *mut u8, size: usize, align: usize) {
    // SAFETY: This is an unsafe extern "C" function. The caller ensures ptr is valid.
    unsafe {
        polyplug_abi::ffi::polyplug_host_free(ptr, size, align);
    }
}

unsafe extern "C" fn stub_find_guest_contract(
    _host: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> GuestContractHandle {
    GuestContractHandle {
        index: u32::MAX,
        generation: 0,
    }
}

unsafe extern "C" fn stub_find_all_guest_contracts(
    _host: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> Array<GuestContractHandle> {
    Array::empty()
}

unsafe extern "C" fn stub_resolve_guest_contract(
    _host: *const HostApi,
    _handle: GuestContractHandle,
) -> *const GuestContractInterface {
    core::ptr::null()
}

unsafe extern "C" fn stub_get_host_contract(
    _host: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> polyplug_abi::HostContractInstance {
    polyplug_abi::HostContractInstance {
        data: core::ptr::null_mut(),
    }
}

unsafe extern "C" fn stub_resolve_host_contract_interface(
    _host: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> *const polyplug_abi::HostContractInterface {
    core::ptr::null()
}

unsafe extern "C" fn stub_list_bundles(_host: *const HostApi) -> Array<BundleId> {
    Array::empty()
}

unsafe extern "C" fn stub_get_dependencies(_host: *const HostApi) -> Array<DependencyInfo> {
    Array::empty()
}

unsafe extern "C" fn stub_load_bundle(
    _this: *const HostApi,
    _path: *const u8,
    _path_len: usize,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

unsafe extern "C" fn stub_reload_bundle(
    _this: *const HostApi,
    _path: *const u8,
    _path_len: usize,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

unsafe extern "C" fn stub_register_host_contract(
    _this: *const HostApi,
    _interface: *const polyplug_abi::HostContractInterface,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

unsafe extern "C" fn stub_register_loader(
    _this: *const HostApi,
    _loader_ptr: *mut c_void,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

unsafe extern "C" fn stub_get_last_error(
    _this: *const HostApi,
    _buf: *mut u8,
    _buf_len: usize,
) -> usize {
    0
}

unsafe extern "C" fn stub_get_error_len(_this: *const HostApi) -> usize {
    0
}

unsafe extern "C" fn stub_unload_bundle(
    _this: *const HostApi,
    _bundle_id: BundleId,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

// ─── Test ─────────────────────────────────────────────────────────────────────

#[test]
fn test_rust_codegen_compile_and_run() {
    // ── 1. Paths ──────────────────────────────────────────────────────────────
    let tmp_dir: PathBuf = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("codegen_rust_test");
    let src_dir: PathBuf = tmp_dir.join("src");
    let api_toml: PathBuf = workspace_root()
        .join("tests")
        .join("fixtures")
        .join("test_bundle.toml");
    let guest_lib_path: PathBuf = workspace_root().join("sdks").join("rust").join("guest");

    std::fs::create_dir_all(&src_dir).expect("failed to create src dir");

    // ── 2. Generate Rust bindings using library API ───────────────────────────
    generate_rust_bindings(&api_toml, &src_dir, Side::Guest);

    // ── 3. Write Cargo.toml + src/lib.rs ─────────────────────────────────────
    write_plugin_cargo_toml(&tmp_dir, &guest_lib_path);
    write_plugin_lib_rs(&src_dir);

    // ── 4. cargo build --release ──────────────────────────────────────────────
    let workspace_root_path: PathBuf = workspace_root();
    let target_dir: PathBuf = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("codegen_rust_build");

    let build_status: std::process::ExitStatus = Command::new(env!("CARGO"))
        .arg("build")
        .arg("--release")
        .arg("--target-dir")
        .arg(&target_dir)
        .arg("--manifest-path")
        .arg(tmp_dir.join("Cargo.toml"))
        .current_dir(&workspace_root_path)
        .status()
        .expect("failed to spawn cargo build");

    assert!(
        build_status.success(),
        "cargo build of generated plugin failed"
    );

    // ── 5. Locate the compiled .so ────────────────────────────────────────────
    let so_path: PathBuf = target_dir.join("release").join(so_filename());
    assert!(
        so_path.exists(),
        "compiled .so not found at {}",
        so_path.display()
    );

    // ── 6. Load with libloading ───────────────────────────────────────────────
    // SAFETY: so_path is a compiled cdylib we just built.
    let library: libloading::Library =
        unsafe { libloading::Library::new(&so_path).expect("failed to load generated plugin .so") };

    // ── 7. Resolve polyplug_init ──────────────────────────────────────────────
    // SAFETY: symbol matches expected ABI signature.
    let init_fn: libloading::Symbol<
        '_,
        unsafe extern "C" fn(*const HostApi, *const BundleInitContext) -> AbiError,
    > = unsafe {
        library
            .get(b"polyplug_init\0")
            .expect("polyplug_init symbol not found")
    };

    // ── 8. Build HostApi + call polyplug_init ───────────────────────────────
    CAPTURED_INTERFACE.with(|cell| cell.set(core::ptr::null()));

    let host_abi: HostApi = HostApi {
        runtime: core::ptr::null_mut(),
        register_guest_contract: capture_interface_callback,
        alloc: stub_alloc,
        free: stub_free,
        find_guest_contract: stub_find_guest_contract,
        find_all_guest_contracts: stub_find_all_guest_contracts,
        resolve_guest_contract: stub_resolve_guest_contract,
        get_host_contract: stub_get_host_contract,
        resolve_host_contract_interface: stub_resolve_host_contract_interface,
        list_bundles: stub_list_bundles,
        get_dependencies: stub_get_dependencies,
        load_bundle: stub_load_bundle,
        reload_bundle: stub_reload_bundle,
        register_host_contract: stub_register_host_contract,
        register_loader: stub_register_loader,
        get_last_error: stub_get_last_error,
        get_error_len: stub_get_error_len,
        unload_bundle: stub_unload_bundle,
        log: stub_host_log,
        create_guest_instance: stub_create_guest_instance,
        destroy_guest_instance: stub_destroy_guest_instance,
        revision_counter: stub_revision_counter,
        reserved: core::ptr::null(),
    };

    // SAFETY: init_fn is valid; host_abi lives for the duration of the call.
    let ctx: BundleInitContext = BundleInitContext {
        bundle_id: 0,
        bundle_path: string_view_null(),
    };
    // SAFETY: init_fn is valid; host_abi and ctx live for the duration of this call.
    let init_result: AbiError = unsafe {
        init_fn(
            &host_abi as *const HostApi,
            &ctx as *const BundleInitContext,
        )
    };
    assert_eq!(
        init_result.code,
        AbiErrorCode::Ok as u32,
        "polyplug_init must return Ok"
    );

    // ── 9. Retrieve the captured interface ──────────────────────────────────────
    let interface_ptr: *const GuestContractInterface = CAPTURED_INTERFACE.with(|cell| cell.get());
    assert!(
        !interface_ptr.is_null(),
        "interface pointer must be non-null after polyplug_init"
    );

    // SAFETY: interface_ptr is valid — plugin is loaded and library is not yet dropped.
    let interface: &GuestContractInterface = unsafe { &*interface_ptr };

    // Get function_count from the native dispatch structure
    // SAFETY: interface is a valid GuestContractInterface with Native dispatch;
    // reading the native union variant is sound for this contract.
    let function_count: u32 = unsafe { interface.dispatch.native.function_count };
    assert_eq!(
        function_count, 4_u32,
        "test.add interface must have 4 functions"
    );

    // ── 10. Dispatch add(3, 5) via function_id 0 ─────────────────────────────
    let args: AddArgs = AddArgs { a: 3_u32, b: 5_u32 };
    let mut out: u32 = 0_u32;

    // SAFETY: functions[0] is the `add` ABI wrapper with the out-param signature
    //   extern "C" fn(GuestContractInstance, *const (), *mut (), *mut AbiError).
    // The instance parameter is passed as first argument (native dispatch).
    let fn_ptr: *const () = unsafe { *interface.dispatch.native.functions.add(0) };
    // SAFETY: fn_ptr is transmuted to the generic out-param dispatch signature. Argument
    // types are enforced by the test: AddArgs matches what the generated wrapper expects.
    // The signature carries GuestContractInstance first and the AbiError out-param last.
    let dispatch_fn: unsafe extern "C" fn(
        GuestContractInstance,
        *const (),
        *mut (),
        *mut AbiError,
    ) = unsafe { core::mem::transmute(fn_ptr) };

    // The generated create_instance constructs the implementation via the
    // author factory and carries it in instance.data — no static storage.
    let mut instance: GuestContractInstance = GuestContractInstance::null();
    // SAFETY: host_abi outlives the instance; create_instance is the generated factory thunk.
    unsafe {
        (interface.create_instance)(
            polyplug_abi::VmLoaderData::null(),
            &host_abi as *const HostApi,
            core::ptr::null(),
            &mut instance as *mut GuestContractInstance,
        )
    };
    assert!(
        !instance.data.is_null(),
        "create_instance must produce a non-null instance payload"
    );

    let mut call_result: AbiError = AbiError::ok();
    // SAFETY: args is a valid AddArgs; out is a valid u32 location; instance
    // was created by the generated create_instance above; call_result receives
    // the AbiError through the trailing out-param.
    unsafe {
        dispatch_fn(
            instance,
            &args as *const AddArgs as *const (),
            &mut out as *mut u32 as *mut (),
            &mut call_result as *mut AbiError,
        )
    };

    // SAFETY: instance was created by create_instance; destroy exactly once.
    unsafe {
        (interface.destroy_instance)(
            polyplug_abi::VmLoaderData::null(),
            &host_abi as *const HostApi,
            instance,
        )
    };

    assert_eq!(
        call_result.code,
        AbiErrorCode::Ok as u32,
        "add(3, 5) must return Ok"
    );
    assert_eq!(out, 8_u32, "add(3, 5) must equal 8");

    println!("test_rust_codegen_compile_and_run: add(3, 5) = {}", out);

    // Keep the library alive until after the last call.
    core::mem::forget(library);
}

// ─── Enum types codegen test ─────────────────────────────────────────────────

#[test]
fn test_rust_codegen_generates_enum_types() {
    // ── 1. Paths ──────────────────────────────────────────────────────────────
    let out_dir: PathBuf =
        PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("integration_codegen_rust_enum");
    let api_toml: PathBuf = workspace_root()
        .join("tests")
        .join("fixtures")
        .join("test_bundle.toml");

    std::fs::create_dir_all(&out_dir).expect("failed to create out_dir");

    // ── 2. Generate Rust bindings using library API ───────────────────────────
    generate_rust_bindings(&api_toml, &out_dir, Side::Host);

    // ── 3. Read host/types.rs and assert enum content ─────────────────────────
    let types_file: PathBuf = out_dir.join("host").join("types.rs");
    let content: String = std::fs::read_to_string(&types_file).expect("read types file");

    assert!(
        content.contains("#[repr(u32)]"),
        "types.rs must contain #[repr(u32)]: {}",
        types_file.display()
    );
    assert!(
        content.contains("pub enum PixelFormat"),
        "types.rs must contain pub enum PixelFormat"
    );
    assert!(
        content.contains("pub mod image_flags"),
        "types.rs must contain pub mod image_flags"
    );
    assert!(
        content.contains("pub struct ImageDesc"),
        "types.rs must contain pub struct ImageDesc"
    );

    println!("test_rust_codegen_generates_enum_types: all enum assertions passed ✓");
}

/// `HostApi.log` stub for test hosts — drops the record.
unsafe extern "C" fn stub_host_log(
    _this: *const polyplug_abi::HostApi,
    _level: u32,
    _scope: polyplug_abi::StringView,
    _message: polyplug_abi::StringView,
) {
}

unsafe extern "C" fn stub_create_guest_instance(
    _this: *const polyplug_abi::HostApi,
    _interface: *const polyplug_abi::GuestContractInterface,
    _args: *const core::ffi::c_void,
    out_instance: *mut polyplug_abi::GuestContractInstance,
) {
    if !out_instance.is_null() {
        // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
        unsafe { out_instance.write(polyplug_abi::GuestContractInstance::null()) };
    }
}

unsafe extern "C" fn stub_destroy_guest_instance(
    _this: *const polyplug_abi::HostApi,
    _interface: *const polyplug_abi::GuestContractInterface,
    _instance: polyplug_abi::GuestContractInstance,
) {
}

unsafe extern "C" fn stub_revision_counter(_this: *const polyplug_abi::HostApi) -> *const u64 {
    core::ptr::null()
}