alef 0.25.39

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
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');
        }
    }
}

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
        // struct so FRB v2 manages the value as a reference-counted opaque handle (RustAutoOpaque).
        // Bridge functions use `.inner` to access the wrapped core type.
        //
        // Prefer the IR-recorded `rust_path` (e.g. `sample_core::extractors::HwpxExtractor`)
        // over the naive `{source_crate}::{name}` form, which only resolves for types
        // re-exported at the crate root.
        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);
        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 => ty.cfg.as_deref().unwrap_or(""),
            },
        ));
        return;
    }

    // FRB v2 mirror convention: the mirror struct keeps the same name as the
    // original; the `mirror` attribute argument tells FRB which type this
    // declaration shadows for codegen purposes.
    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) {
    use crate::backends::dart::template_env;
    // Count only non-binding_excluded fields for the unit/data decision.
    let all_unit = en.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 &en.variants {
            // The mirror enum is a DTO/wire type used by flutter_rust_bridge to generate
            // unconditional match arms in `frb_generated.rs`. If a variant is conditionally
            // compiled out of the mirror, those generated arms reference a missing variant
            // and produce E0599. The mirror must always declare every variant regardless of
            // feature flags; the `From<CoreType>` match arms (in `emit_from_impl_for_enum`)
            // carry the cfg guards on the upstream-referencing arms and a catch-all
            // `_ => unreachable!()` handles any variant the upstream did not compile.
            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 &en.variants {
            // Binding surface fields only — exclude fields marked binding_excluded.
            let visible_fields: Vec<&_> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();
            if visible_fields.is_empty() {
                // All fields are binding_excluded (or variant was already unit): emit as unit.
                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() {
                    // Tuple-variant fields land in the IR as "_0", "_1", ... but
                    // flutter_rust_bridge strips a leading underscore from Dart
                    // field names — leaving an invalid bare digit. Rename any
                    // empty or "_N"-style field to a Dart-safe "fieldN".
                    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);
            // Mirror binding is &i64 / &f64 / &bool — deref with *.
            let base = match prim {
                PrimitiveType::I64 | PrimitiveType::F64 | PrimitiveType::Bool => {
                    format!("*{field_expr}")
                }
                _ => format!("*{field_expr} as {native}"),
            };
            // Primitive optional fields land in the mirror as bare i64 (not
            // Option<i64>), so wrap with Some when the real field is optional.
            if field.optional { format!("Some({base})") } else { base }
        }
        TypeRef::Duration => {
            // FRB maps Duration → i64 millis. Mirror binding is &i64 (non-optional
            // regardless of real-field optionality).
            let base = format!("std::time::Duration::from_millis(*{field_expr} as u64)");
            if field.optional { format!("Some({base})") } else { base }
        }
        TypeRef::String | TypeRef::Bytes => {
            // emit_mirror_error uses frb_rust_type_inner which ignores the optional
            // flag, so the mirror field is always bare `String`/`Vec<u8>` (never
            // `Option<String>`). Wrap with Some when the real field is optional.
            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()")
        }
        // Named, Path, Json, Map, Unit — clone is the safe fallback.
        _ => 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;

    // A variant is "skipped" (needs wildcard arm) only when it has non-binding_excluded fields
    // that are sanitized (type erased). All-binding_excluded variants and binding_excluded+sanitized
    // mixes are handled by the arms above.
    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) {
            // Tuple variant with all fields binding_excluded: mirror has a dummy `field0: String`
            // (emitted by emit_mirror_error), so FRB generates struct-syntax patterns.
            // However, the excluded field's type may not implement Default. Since the variant
            // can never be constructed on the dart side (the excluded field is omitted from the mirror),
            // this arm is unreachable and we emit unreachable!() instead of attempting Default::default().
            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() {
            // Non-unit variant with no fields at all in IR (edge case): treat as 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.fields.iter().all(|f| f.binding_excluded) {
            // Non-tuple variant with all fields binding_excluded: struct variant.
            // The excluded fields' types may not implement Default. Since the variant
            // can never be constructed on the dart side (all fields are omitted from the mirror),
            // this arm is unreachable and we emit unreachable!() instead of attempting Default::default().
            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 {
            // Mixed or fully-visible fields. Use only non-binding_excluded fields for the
            // mirror-side pattern; binding_excluded fields are initialized with Default::default().
            let visible_fields: Vec<&FieldDef> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();

            if !variant_is_reconstructible(&visible_fields) {
                // Sanitized visible fields cannot be reconstructed — skip this arm.
                continue;
            }

            // Detect tuple variants: all VISIBLE fields have positional names ("_0", "_1", …).
            let is_tuple_variant = visible_fields
                .iter()
                .all(|f| f.name.is_empty() || f.name.starts_with('_'));

            // Collect display field names (matching emit_mirror_error's rename logic):
            // positional "_N" names become "fieldN" because FRB strips leading underscores.
            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();

            // Pattern: the mirror always uses struct syntax (FRB converts tuple variants
            // to named struct variants), so the destructure is always `{ fieldN: f_fieldN }`.
            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(),
                },
            ));

            // Constructor: tuple variants need positional syntax `Self::Variant(f0, f1)`;
            // struct variants need named syntax `Self::Variant { name: expr }`.
            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();
                // Append Default::default() for any binding_excluded positional fields.
                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}"));
                }
                // Append Default::default() for binding_excluded named fields.
                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");
        }
    }
    // Wildcard arm for skipped sanitized variants — panics with a clear message
    // rather than producing silent garbage at the call site.
    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) {
            // Tuple error variant with all fields binding_excluded (retained in IR).
            // Emit a dummy `String` field so FRB generates tuple-style patterns
            // (`crate::Err::Variant(_)`) against the core type rather than unit-style
            // patterns (`crate::Err::Variant`), which would cause E0533.
            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 {
            // Emit only non-binding_excluded fields in the mirror.
            let visible_fields: Vec<&FieldDef> = variant.fields.iter().filter(|f| !f.binding_excluded).collect();
            if visible_fields.is_empty() {
                // All fields are binding_excluded but not tuple — emit as unit.
                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");

    // Emit introspection methods only when the error has whitelisted methods.
    let bridge_methods: Vec<&crate::core::ir::MethodDef> = error.methods.iter().filter(|m| !m.sanitized).collect();
    if bridge_methods.is_empty() {
        return;
    }

    // Resolve the fully-qualified core type path, preferring the IR-recorded `rust_path`
    // (e.g. `sample_llm::error::SampleLlmError`) over the naive `{crate}::{Name}` fallback.
    let core_path = if error.rust_path.is_empty() {
        format!("{source_crate_name}::{}", error.name)
    } else {
        error.rust_path.replace('-', "_")
    };

    // Emit a safe From<&MirrorEnum> for CoreType impl. Each variant is reconstructed
    // field-by-field with explicit casts from FRB-widened types (i64/f64) to the real
    // primitive widths. This replaces the former unsound raw-pointer transmute.
    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(),
            },
        ));
        // Build any coercion suffix needed to reconcile the core return type with the
        // FRB bridge return type declared above:
        //   - `&str` (returns_ref=true + String TypeRef) → `.to_string()`
        //   - narrow integer or float (e.g. u16) → ` as i64` / ` as f64`
        let call_suffix: String =
            if method.returns_ref && matches!(method.return_type, crate::core::ir::TypeRef::String) {
                // Core returns &str; bridge declares 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");
}