alef 0.84.2

Opinionated polyglot binding generator for Rust libraries
Documentation
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
use crate::codegen::cfg::is_host_owned_rust_path;
use crate::codegen::conversions::{VariantDeclaration, enum_variant_declaration};
use crate::codegen::shared::binding_fields;
use crate::core::ir::{EnumDef, ErrorDef, FieldDef, PrimitiveType, TypeDef, TypeRef};

use super::conversions::{frb_rust_type, frb_rust_type_inner, primitive_name};

/// Emit rustdoc `///` lines above the next item.
///
/// `flutter_rust_bridge` propagates Rust doc comments to the generated Dart
/// classes, so attaching `///` lines to mirror structs, mirror enums, their
/// fields, and their variants makes the doc text reach the Dart side without
/// any post-processing.
fn emit_rust_doc(doc: &str, indent: &str, out: &mut String) {
    if doc.is_empty() {
        return;
    }
    for line in doc.lines() {
        out.push_str(indent);
        if line.is_empty() {
            out.push_str("///\n");
        } else {
            out.push_str("/// ");
            out.push_str(line);
            out.push('\n');
        }
    }
}

/// Emit the `#[frb(mirror)]` Rust struct flutter_rust_bridge reads to generate the Dart class.
///
/// `[crates.dart] rename_fields` does not apply on this path, and cannot: under the default
/// `DartStyle::Frb`, alef emits no Dart source at all — FRB derives the Dart property names from
/// this mirror's Rust field names using its own casing rule. The field names here must therefore
/// stay identical to the core type's, or the mirror stops mirroring. The Dart field surface
/// `rename_fields` does govern is `DartStyle::Ffi`'s `@freezed` parameters, handled in
/// `gen_ffi::types::dart_field_identifier`. ~keep
pub(crate) fn emit_mirror_struct(out: &mut String, ty: &TypeDef, source_crate_name: &str) {
    use crate::backends::dart::template_env;

    if ty.is_opaque {
        // Opaque handle types cannot use #[frb(mirror)] because the local mirror struct
        // is zero-sized while the core type has data. Instead, emit a #[frb(opaque)] wrapper
        let source_module = source_crate_name.replace('-', "_");
        let inner_path = if ty.rust_path.is_empty() {
            format!("{source_module}::{}", ty.name)
        } else {
            ty.rust_path.replace('-', "_")
        };
        emit_rust_doc(&ty.doc, "", out);
        let wrapper_cfg = super::helpers::widen_opaque_wrapper_cfg(ty.cfg.as_deref().unwrap_or(""));
        out.push_str(&template_env::render(
            "rust_opaque_wrapper_struct.jinja",
            minijinja::context! {
                name => ty.name.as_str(),
                inner_path => inner_path.as_str(),
                source_cfg => wrapper_cfg.as_str(),
            },
        ));
        return;
    }

    emit_rust_doc(&ty.doc, "", out);
    out.push_str(&template_env::render(
        "rust_mirror_struct_attribute.jinja",
        minijinja::context! {
            name => ty.name.as_str(),
            source_cfg => ty.cfg.as_deref().unwrap_or(""),
        },
    ));
    out.push_str(&template_env::render(
        "rust_mirror_struct_open.jinja",
        minijinja::context! {
            name => ty.name.as_str(),
        },
    ));
    for field in binding_fields(&ty.fields) {
        let rust_ty = frb_rust_type(&field.ty, field.optional);
        emit_rust_doc(&field.doc, "    ", out);
        out.push_str(&template_env::render(
            "rust_mirror_struct_field.jinja",
            minijinja::context! {
                field_name => field.name.as_str(),
                rust_ty => rust_ty,
            },
        ));
    }
    out.push_str(&template_env::render(
        "rust_mirror_struct_close.jinja",
        minijinja::context! {},
    ));
}

pub(crate) fn emit_mirror_enum(
    out: &mut String,
    en: &EnumDef,
    source_crate_name: &str,
    configured_features: Option<&[String]>,
) {
    use crate::backends::dart::template_env;

    // A variant merged in from a foreign `[[crates.source_crates]]` crate can only ever be
    // declared here if this binding's own configured feature set does NOT prove it unreachable
    // -- the same `enum_variant_declaration` authority every conversion arm in
    // `enum_conversions.rs` and every other backend's own wrapper declaration already consults
    // (`codegen::conversions::enums::enum_variant_declaration`'s doc comment names the exact
    // defect two independently-written declaration/conversion answers caused). Before this fix
    // this mirror always declared every variant regardless of `configured_features`, so a Dart
    // caller could construct a value the real dependency build never compiles in, and passing it
    // back into Rust hit the `mirror -> core` catch-all's `unreachable!()` -- the round-trip
    // failure the consumer's audit reported. A HOST-owned cfg-gated variant still resolves to
    // `Keep` unconditionally here (matching every other backend's declaration surface); this
    // mirror has no per-variant `#[cfg(...)]` template, so a host-owned gate's `Keep{cfg:
    // Some(_)}` and `Keep{cfg: None}` render identically -- only `Drop` changes anything. ~keep
    let is_host_enum = is_host_owned_rust_path(source_crate_name, &en.rust_path);
    let configured_features_set: Option<std::collections::HashSet<&str>> =
        configured_features.map(|features| features.iter().map(String::as_str).collect());
    let declared_variants: Vec<&crate::core::ir::EnumVariant> = en
        .variants
        .iter()
        .filter(|variant| {
            !matches!(
                enum_variant_declaration(variant, is_host_enum, configured_features_set.as_ref()),
                VariantDeclaration::Drop
            )
        })
        .collect();

    let all_unit = declared_variants
        .iter()
        .all(|v| v.fields.iter().all(|f| f.binding_excluded));
    emit_rust_doc(&en.doc, "", out);
    out.push_str(&template_env::render(
        "rust_mirror_enum_attribute.jinja",
        minijinja::context! {
            name => en.name.as_str(),
            source_cfg => en.cfg.as_deref().unwrap_or(""),
        },
    ));
    out.push_str(&template_env::render(
        "rust_mirror_enum_open.jinja",
        minijinja::context! {
            name => en.name.as_str(),
        },
    ));
    if all_unit {
        for variant in &declared_variants {
            emit_rust_doc(&variant.doc, "    ", out);
            out.push_str(&template_env::render(
                "rust_mirror_enum_unit_variant.jinja",
                minijinja::context! {
                    variant_name => variant.name.as_str(),
                },
            ));
        }
    } else {
        for variant in &declared_variants {
            let visible_fields: Vec<&_> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();
            if visible_fields.is_empty() {
                emit_rust_doc(&variant.doc, "    ", out);
                out.push_str(&template_env::render(
                    "rust_mirror_enum_unit_variant.jinja",
                    minijinja::context! {
                        variant_name => variant.name.as_str(),
                    },
                ));
            } else {
                emit_rust_doc(&variant.doc, "    ", out);
                out.push_str(&template_env::render(
                    "rust_mirror_enum_data_variant_open.jinja",
                    minijinja::context! {
                        variant_name => variant.name.as_str(),
                    },
                ));
                for (idx, f) in visible_fields.iter().enumerate() {
                    let fname = if f.name.is_empty() || f.name.starts_with('_') {
                        format!("field{idx}")
                    } else {
                        f.name.clone()
                    };
                    let rust_ty = frb_rust_type_inner(&f.ty);
                    emit_rust_doc(&f.doc, "        ", out);
                    out.push_str(&template_env::render(
                        "rust_mirror_enum_data_variant_field.jinja",
                        minijinja::context! {
                            field_name => fname,
                            rust_ty => rust_ty,
                        },
                    ));
                }
                out.push_str(&template_env::render(
                    "rust_mirror_enum_data_close.jinja",
                    minijinja::context! {},
                ));
            }
        }
    }
    out.push_str("}\n");
}

/// Return the conversion expression to reconstruct a real-type field value from a
/// mirror field binding.
///
/// Mirror fields use FRB-widened types: integers → `i64`, floats → `f64`,
/// `Duration` → `i64` millis, and optional primitive/Duration fields collapse to
/// their non-optional widened form. String/Bytes/Vec optional fields retain
/// `Option<...>` wrapping in the mirror because FRB handles those correctly.
///
/// `field_expr` is the pattern-binding identifier (e.g. `"f_status"`). The
/// caller binds it via `ref f_<name>` so its type is `&MirrorFieldType`.
fn field_from_expr(field: &FieldDef, field_expr: &str) -> String {
    match &field.ty {
        TypeRef::Primitive(prim) => {
            let native = primitive_name(prim);
            let base = match prim {
                PrimitiveType::I64 | PrimitiveType::F64 | PrimitiveType::Bool => {
                    format!("*{field_expr}")
                }
                _ => format!("*{field_expr} as {native}"),
            };
            if field.optional { format!("Some({base})") } else { base }
        }
        TypeRef::Duration => {
            let base = format!("std::time::Duration::from_millis(*{field_expr} as u64)");
            if field.optional { format!("Some({base})") } else { base }
        }
        TypeRef::String | TypeRef::Bytes => {
            if field.optional {
                format!("Some({field_expr}.clone())")
            } else {
                format!("{field_expr}.clone()")
            }
        }
        TypeRef::Char => {
            let base = format!("{field_expr}.chars().next().unwrap_or('\\0')");
            if field.optional { format!("Some({base})") } else { base }
        }
        TypeRef::Optional(inner) => {
            let inner_field = FieldDef {
                name: field.name.clone(),
                ty: *inner.clone(),
                optional: false,
                ..field.clone()
            };
            let inner_expr = field_from_expr(&inner_field, "v");
            format!("{field_expr}.as_ref().map(|v| {inner_expr})")
        }
        TypeRef::Vec(inner) => {
            let inner_field = FieldDef {
                name: "_x".to_string(),
                ty: *inner.clone(),
                optional: false,
                ..field.clone()
            };
            let inner_expr = field_from_expr(&inner_field, "x");
            format!("{field_expr}.iter().map(|x| {inner_expr}).collect()")
        }
        _ => format!("{field_expr}.clone()"),
    }
}

/// Return true if every field in the variant can be safely reconstructed in the
/// `From<&MirrorEnum>` impl.
///
/// Sanitized fields represent types that were erased to `String` during
/// extraction (e.g. `serde_json::Error`). Such originals cannot be recovered
/// from the mirror, so the entire variant must be skipped in the From impl.
fn variant_is_reconstructible(fields: &[&FieldDef]) -> bool {
    fields.iter().all(|f| !f.sanitized)
}

/// Emit a safe `impl From<&MirrorEnum> for CorePath` conversion.
///
/// Each reconstructible variant is matched arm-by-arm with explicit field casts
/// from FRB-widened types (i64/f64) to the real primitive widths. Variants whose
/// fields include sanitized (erased) types are skipped — a wildcard arm with
/// `unreachable!` is emitted to cover them so the match stays exhaustive.
/// `#[allow(unreachable_patterns)]` is emitted unconditionally to suppress the
/// compiler warning when all variants are in fact reconstructible.
fn emit_from_impl(out: &mut String, error: &ErrorDef, core_path: &str, error_cfg: &str) {
    use crate::backends::dart::template_env;

    let any_skipped = error.variants.iter().any(|v| {
        let visible_fields: Vec<&FieldDef> = v.fields.iter().filter(|f| !f.binding_excluded).collect();
        !v.is_unit && !visible_fields.is_empty() && !variant_is_reconstructible(&visible_fields)
    });

    out.push_str(&template_env::render(
        "rust_mirror_error_from_impl_open.rs.jinja",
        minijinja::context! {
            name => error.name.as_str(),
            core_path => core_path,
            source_cfg => error_cfg,
        },
    ));
    for variant in &error.variants {
        let vname = &variant.name;
        if variant.is_unit {
            out.push_str(&template_env::render(
                "rust_mirror_error_unit_from_arm.rs.jinja",
                minijinja::context! {
                    name => error.name.as_str(),
                    vname => vname.as_str(),
                },
            ));
        } else if !variant.is_unit && variant.is_tuple && variant.fields.iter().all(|f| f.binding_excluded) {
            out.push_str(&template_env::render(
                "rust_mirror_error_excluded_from_arm.rs.jinja",
                minijinja::context! {
                    name => error.name.as_str(),
                    vname => vname.as_str(),
                },
            ));
        } else if !variant.is_unit && variant.fields.is_empty() {
            out.push_str(&template_env::render(
                "rust_mirror_error_unit_from_arm.rs.jinja",
                minijinja::context! {
                    name => error.name.as_str(),
                    vname => vname.as_str(),
                },
            ));
        } else if variant.fields.iter().all(|f| f.binding_excluded) {
            out.push_str(&template_env::render(
                "rust_mirror_error_excluded_from_arm.rs.jinja",
                minijinja::context! {
                    name => error.name.as_str(),
                    vname => vname.as_str(),
                },
            ));
        } else {
            let visible_fields: Vec<&FieldDef> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();

            if !variant_is_reconstructible(&visible_fields) {
                continue;
            }

            let is_tuple_variant = visible_fields
                .iter()
                .all(|f| f.name.is_empty() || f.name.starts_with('_'));

            let field_names: Vec<String> = visible_fields
                .iter()
                .enumerate()
                .map(|(idx, f)| {
                    if f.name.is_empty() || f.name.starts_with('_') {
                        format!("field{idx}")
                    } else {
                        f.name.clone()
                    }
                })
                .collect();

            let pat_fields: String = field_names
                .iter()
                .map(|fname| format!("{fname}: f_{fname}"))
                .collect::<Vec<_>>()
                .join(", ");
            out.push_str(&template_env::render(
                "rust_mirror_error_struct_pattern_arm.rs.jinja",
                minijinja::context! {
                    name => error.name.as_str(),
                    vname => vname.as_str(),
                    pat_fields => pat_fields.as_str(),
                },
            ));

            if is_tuple_variant {
                let mut args: Vec<String> = visible_fields
                    .iter()
                    .enumerate()
                    .map(|(i, f)| {
                        let fname = &field_names[i];
                        field_from_expr(f, &format!("f_{fname}"))
                    })
                    .collect();
                let excluded_count = variant.fields.iter().filter(|f| f.binding_excluded).count();
                for _ in 0..excluded_count {
                    args.push("Default::default()".to_string());
                }
                out.push_str(&template_env::render(
                    "rust_mirror_error_tuple_return.rs.jinja",
                    minijinja::context! {
                        vname => vname.as_str(),
                        args => args.join(", "),
                    },
                ));
            } else {
                let mut real_fields: Vec<String> = Vec::new();
                for (i, f) in visible_fields.iter().enumerate() {
                    let fname = &field_names[i];
                    let expr = field_from_expr(f, &format!("f_{fname}"));
                    real_fields.push(format!("                    {fname}: {expr}"));
                }
                for f in variant.fields.iter().filter(|f| f.binding_excluded) {
                    real_fields.push(format!("                    {}: Default::default()", f.name));
                }
                out.push_str(&template_env::render(
                    "rust_mirror_error_struct_return.rs.jinja",
                    minijinja::context! {
                        vname => vname.as_str(),
                        real_fields => real_fields.join(",\n"),
                    },
                ));
            }
            out.push_str("            }\n");
        }
    }
    if any_skipped {
        out.push_str(&template_env::render(
            "rust_mirror_error_sanitized_wildcard_arm.rs.jinja",
            minijinja::context! {},
        ));
    }
    out.push_str(&template_env::render(
        "rust_mirror_error_from_impl_close.rs.jinja",
        minijinja::context! {},
    ));
}

/// Emit a `#[frb(mirror(ErrorName))]` enum + safe `impl From` conversion +
/// `impl ErrorName` block with `#[frb]` introspection methods.
///
/// flutter_rust_bridge translates the mirrored enum into a Dart sealed class with
/// per-variant subclasses. The `impl` block methods annotated with `#[frb]` are
/// surfaced as Dart instance methods on the sealed class.
///
/// Introspection methods convert `self` to the core error type via a safe
/// `From<&MirrorEnum>` impl that reconstructs each variant field-by-field with
/// explicit primitive casts. This avoids the unsound raw-pointer transmute that
/// would arise from mismatched field widths (e.g. `i64` in the mirror vs `u16`
/// in the real type).
pub(crate) fn emit_mirror_error(out: &mut String, error: &ErrorDef, source_crate_name: &str) {
    use crate::backends::dart::template_env;

    emit_rust_doc(&error.doc, "", out);
    out.push_str(&template_env::render(
        "rust_mirror_enum_attribute.jinja",
        minijinja::context! {
            name => error.name.as_str(),
            source_cfg => "",
        },
    ));
    out.push_str(&template_env::render(
        "rust_mirror_enum_open.jinja",
        minijinja::context! {
            name => error.name.as_str(),
        },
    ));

    for variant in &error.variants {
        emit_rust_doc(&variant.doc, "    ", out);
        if variant.is_unit {
            out.push_str(&template_env::render(
                "rust_mirror_enum_unit_variant.jinja",
                minijinja::context! {
                    variant_name => variant.name.as_str(),
                },
            ));
        } else if !variant.is_unit && variant.is_tuple && variant.fields.iter().all(|f| f.binding_excluded) {
            out.push_str(&template_env::render(
                "rust_mirror_enum_data_variant_open.jinja",
                minijinja::context! {
                    variant_name => variant.name.as_str(),
                },
            ));
            out.push_str(&template_env::render(
                "rust_mirror_enum_data_variant_field.jinja",
                minijinja::context! {
                    field_name => "field0",
                    rust_ty => "String",
                },
            ));
            out.push_str(&template_env::render(
                "rust_mirror_enum_data_close.jinja",
                minijinja::context! {},
            ));
        } else {
            let visible_fields: Vec<&FieldDef> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();
            if visible_fields.is_empty() {
                out.push_str(&template_env::render(
                    "rust_mirror_enum_unit_variant.jinja",
                    minijinja::context! {
                        variant_name => variant.name.as_str(),
                    },
                ));
            } else {
                out.push_str(&template_env::render(
                    "rust_mirror_enum_data_variant_open.jinja",
                    minijinja::context! {
                        variant_name => variant.name.as_str(),
                    },
                ));
                for (idx, f) in visible_fields.iter().enumerate() {
                    let fname = if f.name.is_empty() || f.name.starts_with('_') {
                        format!("field{idx}")
                    } else {
                        f.name.clone()
                    };
                    let rust_ty = frb_rust_type_inner(&f.ty);
                    out.push_str(&template_env::render(
                        "rust_mirror_enum_data_variant_field.jinja",
                        minijinja::context! {
                            field_name => fname,
                            rust_ty => rust_ty,
                        },
                    ));
                }
                out.push_str(&template_env::render(
                    "rust_mirror_enum_data_close.jinja",
                    minijinja::context! {},
                ));
            }
        }
    }
    out.push_str("}\n");

    let bridge_methods: Vec<&crate::core::ir::MethodDef> = error.methods.iter().filter(|m| !m.sanitized).collect();
    if bridge_methods.is_empty() {
        return;
    }

    let core_path = if error.rust_path.is_empty() {
        format!("{source_crate_name}::{}", error.name)
    } else {
        error.rust_path.replace('-', "_")
    };

    emit_from_impl(out, error, &core_path, "");

    out.push_str(&crate::backends::dart::template_env::render(
        "rust_error_impl_open.rs.jinja",
        minijinja::context! {
            error_name => error.name.as_str(),
            source_cfg => "",
        },
    ));
    for method in bridge_methods {
        emit_rust_doc(&method.doc, "    ", out);
        let ret_ty = frb_rust_type_inner(&method.return_type);
        out.push_str(&crate::backends::dart::template_env::render(
            "rust_error_method_open.rs.jinja",
            minijinja::context! {
                method_name => method.name.as_str(),
                ret_ty => ret_ty.as_str(),
            },
        ));
        let call_suffix: String =
            if method.returns_ref && matches!(method.return_type, crate::core::ir::TypeRef::String) {
                ".to_string()".to_string()
            } else if let crate::core::ir::TypeRef::Primitive(ref prim) = method.return_type {
                let native = primitive_name(prim);
                let frb_ty = frb_rust_type_inner(&method.return_type);
                if native != frb_ty.as_str() {
                    format!(" as {frb_ty}")
                } else {
                    String::new()
                }
            } else {
                String::new()
            };
        out.push_str(&crate::backends::dart::template_env::render(
            "rust_error_method_body.rs.jinja",
            minijinja::context! {
                core_path => core_path.as_str(),
                method_name => method.name.as_str(),
                call_suffix => call_suffix.as_str(),
            },
        ));
        out.push_str("    }\n");
    }
    out.push_str("}\n");
}

#[cfg(test)]
mod tests {
    use super::emit_mirror_struct;
    use crate::core::ir::TypeDef;

    /// The regression this task fixes: an opaque wrapper's `inner` field names the host path
    /// (`inner_path`) directly -- unlike a plain mirror struct, whose fields are widened
    /// FRB-native types -- so when the wrapped type is wholly gated behind a Cargo feature
    /// (`TypeDef::cfg`), the struct declaration AND both `From` impls must all carry the gate,
    /// or the generated crate references a path that does not exist in a build excluding that
    /// feature (E0433 in the real failure this mirrors). Before the fix, `source_cfg` was passed
    /// to `rust_opaque_wrapper_struct.jinja` but the template never used it.
    #[test]
    fn gated_opaque_wrapper_carries_cfg_on_struct_and_both_impls() {
        let ty = TypeDef {
            name: "OcrEngine".to_string(),
            rust_path: "mylib::thumbnails::OcrEngine".to_string(),
            cfg: Some(r#"feature = "thumbnails""#.to_string()),
            is_opaque: true,
            ..Default::default()
        };
        let mut out = String::new();
        emit_mirror_struct(&mut out, &ty, "mylib");

        assert!(
            out.contains("mylib::thumbnails::OcrEngine"),
            "expected the gated host path to still be referenced, got:\n{out}"
        );
        assert_eq!(
            out.matches("#[cfg(feature = \"thumbnails\")]").count(),
            3,
            "the whole-type gate must land on the struct declaration and both From impls, got:\n{out}"
        );
    }

    /// Negative control: an ungated opaque type (`TypeDef::cfg` is `None`) must emit no
    /// `#[cfg(...)]` at all.
    #[test]
    fn ungated_opaque_wrapper_emits_no_cfg() {
        let ty = TypeDef {
            name: "PlainEngine".to_string(),
            rust_path: "mylib::PlainEngine".to_string(),
            is_opaque: true,
            ..Default::default()
        };
        let mut out = String::new();
        emit_mirror_struct(&mut out, &ty, "mylib");

        assert!(
            !out.contains("#[cfg("),
            "ungated opaque type must not emit #[cfg(...)], got:\n{out}"
        );
    }
}