alef 0.25.25

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
use crate::backends::rustler::gen_bindings::functions::{
    gen_nif_async_function, gen_nif_async_method, gen_nif_function, gen_nif_method,
};
use crate::backends::rustler::gen_bindings::helpers::{collect_types_for_nif_derives, get_module_info};
use crate::backends::rustler::gen_bindings::rust_items::{
    gen_from_json_nif, gen_nif_init, patch_streaming_default_param, rustler_default_for_type,
};
use crate::backends::rustler::gen_bindings::types::{
    gen_enum, gen_opaque_resource, gen_rustler_config_impl, gen_rustler_flat_data_enum_from_core,
    gen_rustler_flat_data_enum_to_core, gen_struct,
};
use crate::backends::rustler::type_map::RustlerMapper;
use crate::codegen::builder::RustFileBuilder;
use crate::codegen::generators;
use crate::codegen::shared::binding_fields;
use crate::codegen::type_mapper::TypeMapper;
use crate::core::backend::GeneratedFile;
use crate::core::config::{BridgeBinding, Language, ResolvedCrateConfig, resolve_output_dir};
use crate::core::ir::{ApiSurface, TypeRef};
use ahash::AHashSet;
use std::path::PathBuf;

/// Prepend `#[cfg(<pred>)]` to a code item when the source symbol carries a cfg predicate.
fn prepend_cfg(cfg: Option<&str>, item: String) -> String {
    match cfg {
        Some(pred) if !pred.is_empty() => format!("#[cfg({pred})]\n{item}"),
        _ => item,
    }
}

pub(super) fn generate_bindings(api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
    let mapper = RustlerMapper;
    let core_import = config.core_import_name();

    let elixir_config = config.elixir.as_ref();
    let exclude_functions: AHashSet<String> = elixir_config
        .map(|c| c.exclude_functions.iter().cloned().collect())
        .unwrap_or_default();
    // Service-owner types and handler-contract traits are marked binding_excluded
    // by the service extraction pass: they are emitted by the service-API codegen,
    // not the generic struct/trait/opaque codegen, so skip them in the generic loops too.
    let binding_excluded_names: Vec<String> = api
        .types
        .iter()
        .filter(|t| t.binding_excluded)
        .map(|t| t.name.clone())
        .collect();
    let mut exclude_types: AHashSet<&str> = elixir_config
        .map(|c| c.exclude_types.iter().map(String::as_str).collect())
        .unwrap_or_default();
    exclude_types.extend(binding_excluded_names.iter().map(String::as_str));
    let cpu_bound_functions: AHashSet<String> = elixir_config
        .map(|c| c.cpu_bound_functions.iter().cloned().collect())
        .unwrap_or_default();

    // For options_field bridges, the bridge field (e.g. "visitor") is handled at the
    // Elixir layer via Map.pop — it must not appear as a typed struct field in the NIF
    // bindings because VisitorHandle (Rc<RefCell<dyn Trait>>) cannot implement
    // Rustler's Encoder/Decoder or Send+Sync traits.
    // Build a map: type_name -> set of field names to exclude.
    // We also cover update structs (e.g. ParseOptionsUpdate) by scanning all IR types
    // for the same field name with a type matching the bridge trait alias.
    let mut bridge_excluded_fields: std::collections::HashMap<String, AHashSet<String>> =
        std::collections::HashMap::new();
    for b in config
        .trait_bridges
        .iter()
        .filter(|b| b.bind_via == BridgeBinding::OptionsField)
        .filter(|b| !b.exclude_languages.iter().any(|l| l == "elixir" || l == "rustler"))
    {
        let field_name = b.resolved_options_field().unwrap_or("visitor").to_string();
        let trait_alias = b.type_alias.as_deref().unwrap_or(&b.trait_name);
        if let Some(opts_type) = b.options_type.as_deref() {
            bridge_excluded_fields
                .entry(opts_type.to_string())
                .or_default()
                .insert(field_name.clone());
        }
        // Also exclude from any other IR type that has this field with the trait alias type.
        for typ in api.types.iter() {
            if binding_fields(&typ.fields).any(|f| {
                if f.name != field_name {
                    return false;
                }
                let type_name = match &f.ty {
                    crate::core::ir::TypeRef::Named(n) => Some(n.as_str()),
                    crate::core::ir::TypeRef::Optional(inner) => {
                        if let crate::core::ir::TypeRef::Named(n) = inner.as_ref() {
                            Some(n.as_str())
                        } else {
                            None
                        }
                    }
                    _ => None,
                };
                type_name == Some(trait_alias)
            }) {
                bridge_excluded_fields
                    .entry(typ.name.clone())
                    .or_default()
                    .insert(field_name.clone());
            }
        }
    }

    let mut builder = RustFileBuilder::new().with_generated_header();
    builder.add_inner_attribute("allow(dead_code, unused_imports, unused_variables)");
    builder.add_inner_attribute("allow(clippy::too_many_arguments, clippy::let_unit_value, clippy::needless_borrow, clippy::map_identity, clippy::just_underscores_and_digits, clippy::unused_unit, clippy::unnecessary_cast, clippy::unwrap_or_default, clippy::derivable_impls, clippy::needless_borrows_for_generic_args, clippy::unnecessary_fallible_conversions)");
    builder.add_import("rustler::ResourceArc");
    builder.add_import("rustler::Encoder");

    // Import traits needed for trait method dispatch
    for trait_path in generators::collect_trait_imports(api) {
        builder.add_import(&trait_path);
    }

    // Only import HashMap when Map-typed fields or returns are present
    let has_maps = api
        .types
        .iter()
        .any(|t| t.fields.iter().any(|f| matches!(&f.ty, TypeRef::Map(_, _))))
        || api
            .functions
            .iter()
            .any(|f| matches!(&f.return_type, TypeRef::Map(_, _)));
    if has_maps {
        builder.add_import("std::collections::HashMap");
    }

    // Custom module declarations
    let custom_mods = config.custom_modules.for_language(Language::Elixir);
    for module in custom_mods {
        builder.add_item(&format!("pub mod {module};"));
    }

    // Include service.rs (if services are configured). The service-API
    // codegen emits additional `#[rustler::nif]` functions that Rustler's
    // init! macro discovers via the module tree.
    if !api.services.is_empty() {
        builder.add_item("pub mod service;");
    }

    let (_module_name, module_prefix) = get_module_info(api, config);

    // Check if we have opaque types and add Arc import if needed
    let opaque_types: AHashSet<String> = api
        .types
        .iter()
        .filter(|t| t.is_opaque)
        .map(|t| t.name.clone())
        .collect();
    if !opaque_types.is_empty() {
        builder.add_import("std::sync::Arc");
    }

    // Collect all types that need NifMap/NifStruct derives: both top-level and recursively
    // referenced (e.g., CrawlResult has field pages: Vec<CrawlPageResult>, so CrawlPageResult
    // must also derive NifMap). Walk the full type closure reachable from function signatures.
    let mut types_to_emit = collect_types_for_nif_derives(api, &exclude_types);

    // Add opaque types (they were filtered out by collect_types_for_nif_derives but need to be emitted
    // as rustler::Resource wrappers). Opaques must be handled separately from non-opaque types.
    for typ in &api.types {
        if typ.is_opaque && !exclude_types.contains(typ.name.as_str()) {
            types_to_emit.insert(typ.name.clone());
        }
    }

    let empty_set: AHashSet<String> = AHashSet::new();
    for typ in api
        .types
        .iter()
        .filter(|typ| !typ.is_trait && types_to_emit.contains(&typ.name))
    {
        if typ.is_opaque {
            builder.add_item(&gen_opaque_resource(typ, &core_import, &opaque_types));
            // Client constructor
            if let Some(ctor) = config.client_constructors.get(&typ.name) {
                let ctor_body = crate::codegen::generators::gen_opaque_constructor(ctor, &typ.name, &core_import, "");
                let ctor_impl = format!("impl {} {{\n{}}}", typ.name, ctor_body);
                builder.add_item(&ctor_impl);
            }
        } else {
            // gen_struct adds Default to derives when typ.has_default is true,
            // so no separate Default impl is needed.
            let excl = bridge_excluded_fields.get(typ.name.as_str()).unwrap_or(&empty_set);
            builder.add_item(&gen_struct(typ, &mapper, &module_prefix, excl));
            // Generate config constructor if type has Default
            if typ.has_default && !typ.fields.is_empty() {
                let config_impl = gen_rustler_config_impl(typ, &mapper, excl);
                builder.add_item(&config_impl);
            }
        }
    }

    for enum_def in &api.enums {
        builder.add_item(&gen_enum(enum_def, &module_prefix));
    }

    // Types with has_default=true accept JSON strings at the NIF boundary so
    // partial maps can be passed without every field being required.
    let default_types: AHashSet<String> = api
        .types
        .iter()
        .filter(|t| t.has_default && !t.is_opaque)
        .map(|t| t.name.clone())
        .collect();

    // Build adapter body map before method iteration so bodies are available for NIF generation.
    let adapter_bodies = crate::adapters::build_adapter_bodies(config, Language::Elixir)?;

    // Streaming-adapter method keys ("Owner.method_name") — these methods are emitted
    // as a pair of standalone start/next NIFs from the adapter struct hook, so the
    // regular method-iteration loop must skip them to avoid double-emitting a NIF
    // with the same name.
    let streaming_method_keys: AHashSet<String> = config
        .adapters
        .iter()
        .filter(|a| matches!(a.pattern, crate::core::config::AdapterPattern::Streaming))
        .filter_map(|a| a.owner_type.as_deref().map(|owner| format!("{owner}.{}", a.name)))
        .collect();

    // Emit adapter-generated standalone items (streaming iterators, callback bridges).
    for adapter in &config.adapters {
        match adapter.pattern {
            crate::core::config::AdapterPattern::Streaming => {
                let key = crate::adapters::stream_struct_key(adapter);
                if let Some(struct_code) = adapter_bodies.get(&key) {
                    // Post-process: convert default-typed request params to JSON strings so
                    // partial maps from Elixir decode successfully (rustler NifMap is strict).
                    let patched = patch_streaming_default_param(struct_code, adapter, &default_types, &core_import);
                    builder.add_item(&patched);
                }
            }
            crate::core::config::AdapterPattern::CallbackBridge => {
                let struct_key = format!("{}.__bridge_struct__", adapter.name);
                let impl_key = format!("{}.__bridge_impl__", adapter.name);
                if let Some(struct_code) = adapter_bodies.get(&struct_key) {
                    builder.add_item(struct_code);
                }
                if let Some(impl_code) = adapter_bodies.get(&impl_key) {
                    builder.add_item(impl_code);
                }
            }
            _ => {}
        }
    }

    let active_bridges: Vec<_> = config
        .trait_bridges
        .iter()
        .filter(|b| !b.exclude_languages.iter().any(|l| l == "elixir" || l == "rustler"))
        .cloned()
        .collect();

    // Add globals for trait call dispatch if there are active bridges
    if !active_bridges.is_empty() {
        builder.add_import("std::sync::atomic::{AtomicU64, Ordering}");
        builder.add_import("std::sync::Mutex");
        builder.add_import("std::collections::HashMap");
        builder.add_item("static TRAIT_REPLY_COUNTER: AtomicU64 = AtomicU64::new(1);");
        builder.add_item("type TraitReplyChannel = tokio::sync::oneshot::Sender<std::result::Result<String, String>>;");
        builder.add_item(
            "static TRAIT_REPLY_CHANNELS: std::sync::LazyLock<Mutex<HashMap<u64, TraitReplyChannel>>> = \
                 std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));",
        );
    }

    // Build a name → TypeDef map so codegen can resolve full rust_paths for
    // types that are not re-exported at the crate root (e.g. DrawingType lives
    // at sample_core::extraction::docx::drawing::DrawingType, not sample_core::DrawingType).
    let types_by_name: ahash::AHashMap<&str, &crate::core::ir::TypeDef> =
        api.types.iter().map(|t| (t.name.as_str(), t)).collect();

    for func in api
        .functions
        .iter()
        .filter(|f| !exclude_functions.contains(f.name.as_str()))
    {
        if crate::codegen::generators::trait_bridge::is_trait_bridge_managed_fn(&func.name, &active_bridges) {
            continue;
        }
        let bridge_param = crate::backends::rustler::trait_bridge::find_bridge_param(func, &active_bridges);
        let bridge_field =
            crate::codegen::generators::trait_bridge::find_bridge_field(func, &api.types, &active_bridges);
        // Skip sanitized functions when there's no trait bridge that can replace the
        // sanitized parameter — such functions have non-bindable types (e.g. Result<T, Box<dyn Error>>)
        // and cannot be auto-delegated. Functions whose only "sanitized" param is a configured
        // trait_bridge param are emitted via gen_bridge_function.
        if func.sanitized && bridge_param.is_none() && bridge_field.is_none() {
            continue;
        }
        if let Some((param_idx, bridge_cfg)) = bridge_param {
            let item = crate::backends::rustler::trait_bridge::gen_bridge_function(
                api,
                func,
                param_idx,
                bridge_cfg,
                &mapper,
                &opaque_types,
                &default_types,
                &core_import,
            );
            let item = prepend_cfg(func.cfg.as_deref(), item);
            builder.add_item(&item);
        } else if let Some(ref bm) = bridge_field {
            let item = crate::backends::rustler::trait_bridge::gen_bridge_field_function(
                api,
                func,
                bm,
                bm.bridge,
                &mapper,
                &opaque_types,
                &default_types,
                &core_import,
            );
            let item = prepend_cfg(func.cfg.as_deref(), item);
            builder.add_item(&item);
        } else if func.is_async {
            let item = gen_nif_async_function(
                func,
                &mapper,
                &opaque_types,
                &default_types,
                &core_import,
                &types_by_name,
            );
            let item = prepend_cfg(func.cfg.as_deref(), item);
            builder.add_item(&item);
        } else {
            let item = gen_nif_function(
                func,
                &mapper,
                &opaque_types,
                &default_types,
                &core_import,
                &cpu_bound_functions,
                &types_by_name,
            );
            let item = prepend_cfg(func.cfg.as_deref(), item);
            builder.add_item(&item);
        }
    }

    // Trait bridge wrappers — generate Rustler bridge structs that delegate to Elixir terms
    let has_trait_bridges = config
        .trait_bridges
        .iter()
        .any(|b| !b.exclude_languages.iter().any(|l| l == "elixir" || l == "rustler"));

    for bridge_cfg in config
        .trait_bridges
        .iter()
        .filter(|b| !b.exclude_languages.iter().any(|l| l == "elixir" || l == "rustler"))
    {
        if let Some(trait_type) = api.types.iter().find(|t| t.is_trait && t.name == bridge_cfg.trait_name) {
            let bridge = crate::backends::rustler::trait_bridge::gen_trait_bridge(
                trait_type,
                bridge_cfg,
                &core_import,
                &config.error_type_name(),
                &config.error_constructor_expr(),
                api,
            )?;
            for imp in &bridge.imports {
                builder.add_import(imp);
            }
            builder.add_item(&bridge.code);
        }
    }

    // Emit support NIFs once after all trait bridges to avoid duplicates
    if has_trait_bridges {
        let ctx = minijinja::context! {};
        builder.add_item(&crate::backends::rustler::template_env::render(
            "trait_support_nifs.rs.jinja",
            ctx,
        ));
    }

    for typ in api
        .types
        .iter()
        .filter(|typ| !typ.is_trait && !exclude_types.contains(typ.name.as_str()))
    {
        for method in typ
            .methods
            .iter()
            .filter(|m| !exclude_functions.contains(m.name.as_str()))
            .filter(|m| !streaming_method_keys.contains(&format!("{}.{}", typ.name, m.name)))
            .filter(|m| {
                // Skip methods whose return type references an excluded type.
                // E.g. ParseOptions::builder() returns ParseOptionsBuilder which
                // is excluded because it holds !Send + !Sync core types.
                !crate::codegen::conversions::field_references_excluded_type(
                    &m.return_type,
                    &exclude_types.iter().map(|s| s.to_string()).collect::<Vec<_>>(),
                )
            })
        {
            let core_path = crate::codegen::conversions::core_type_path(typ, &core_import);
            if method.is_async {
                builder.add_item(&gen_nif_async_method(
                    &typ.name,
                    &core_path,
                    method,
                    &mapper,
                    typ.is_opaque,
                    &opaque_types,
                    &default_types,
                    &core_import,
                    &adapter_bodies,
                    &types_by_name,
                ));
            } else {
                builder.add_item(&gen_nif_method(
                    &typ.name,
                    &core_path,
                    method,
                    &mapper,
                    typ.is_opaque,
                    &opaque_types,
                    &default_types,
                    &core_import,
                    &adapter_bodies,
                    &types_by_name,
                ));
            }
        }
    }

    let binding_to_core = crate::codegen::conversions::convertible_types(api);
    let core_to_binding = crate::codegen::conversions::core_to_binding_convertible_types(api);
    let input_types = crate::codegen::conversions::input_type_names(api);
    // Flat data enums are output-only structs with no binding→core From impl.
    // Pass their names as `from_binding_skip_types` so that containing structs
    // (e.g. Metadata.format: Option<FormatMetadata>) use Default::default()
    // instead of .map(Into::into) in generated From impls.
    let flat_data_enum_names_vec: Vec<String> = api
        .enums
        .iter()
        .filter(|e| {
            let has_data = e.variants.iter().any(|v| !v.fields.is_empty());
            has_data && e.variants.iter().filter(|v| !v.fields.is_empty()).all(|v| v.is_tuple)
        })
        .map(|e| e.name.clone())
        .collect();

    // Collect bridge type aliases so they can be passed as `exclude_types` in ConversionConfig.
    // This ensures From impls skip fields (e.g. `visitor: Option<VisitorHandle>`) that were
    // excluded from the binding struct because they reference !Send + !Sync core types.
    let bridge_conv_exclude_types: Vec<String> = config
        .trait_bridges
        .iter()
        .filter(|b| !b.exclude_languages.iter().any(|l| l == "elixir" || l == "rustler"))
        .filter(|b| b.bind_via == BridgeBinding::OptionsField)
        .map(|b| b.type_alias.as_deref().unwrap_or(&b.trait_name).to_string())
        .collect();

    // From/Into conversions — only for types that have NIF wrapper structs (types_to_emit).
    for typ in api
        .types
        .iter()
        .filter(|typ| !typ.is_trait && !exclude_types.contains(typ.name.as_str()) && types_to_emit.contains(&typ.name))
    {
        let rustler_struct_cfg = crate::codegen::conversions::ConversionConfig {
            map_as_string: false,
            exclude_types: &bridge_conv_exclude_types,
            // Flat data enums have no binding→core impl; use Default::default() for their fields.
            from_binding_skip_types: &flat_data_enum_names_vec,
            ..Default::default()
        };
        if input_types.contains(&typ.name)
            && crate::codegen::conversions::can_generate_conversion(typ, &binding_to_core)
        {
            builder.add_item(&crate::codegen::conversions::gen_from_binding_to_core_cfg(
                typ,
                &core_import,
                &rustler_struct_cfg,
            ));
        }
        if crate::codegen::conversions::can_generate_conversion(typ, &core_to_binding) {
            builder.add_item(&crate::codegen::conversions::gen_from_core_to_binding_cfg(
                typ,
                &core_import,
                &opaque_types,
                &rustler_struct_cfg,
            ));
        }
    }
    for e in &api.enums {
        // Data enums (any variant has fields) are generated as NifTaggedEnum with real fields.
        // Set binding_enums_have_data so the From impls destructure fields instead of
        // fabricating Default::default() for every field (which would silently corrupt data).
        let has_data = e.variants.iter().any(|v| !v.fields.is_empty());
        // Flat data enums (all data variants are single-field tuple variants) use a flat
        // NifStruct representation on the Elixir side. Their core→binding conversion is
        // generated by gen_rustler_flat_data_enum_from_core instead of the generic enum
        // arm-matching codepath. They are output-only (no binding→core direction).
        let is_flat_data = has_data && e.variants.iter().filter(|v| !v.fields.is_empty()).all(|v| v.is_tuple);

        if is_flat_data {
            if crate::codegen::conversions::can_generate_enum_conversion_from_core(e) {
                builder.add_item(&gen_rustler_flat_data_enum_from_core(e, &core_import));
            }
            // Emit binding→core for input-typed flat data enums so they round-trip through
            // public function arguments (e.g. Vec<Message> in ChatCompletionRequest). The
            // discriminator field on the local struct selects the matching core variant.
            if input_types.contains(&e.name) && crate::codegen::conversions::can_generate_enum_conversion(e) {
                builder.add_item(&gen_rustler_flat_data_enum_to_core(e, &core_import));
            }
        } else {
            let rustler_conv_config = crate::codegen::conversions::ConversionConfig {
                binding_enums_have_data: has_data,
                ..Default::default()
            };
            if input_types.contains(&e.name) && crate::codegen::conversions::can_generate_enum_conversion(e) {
                builder.add_item(&crate::codegen::conversions::gen_enum_from_binding_to_core_cfg(
                    e,
                    &core_import,
                    &rustler_conv_config,
                ));
            }
            if crate::codegen::conversions::can_generate_enum_conversion_from_core(e) {
                builder.add_item(&crate::codegen::conversions::gen_enum_from_core_to_binding_cfg(
                    e,
                    &core_import,
                    &rustler_conv_config,
                ));
            }
        }
    }

    // Error converter functions
    for error in &api.errors {
        builder.add_item(&crate::codegen::error_gen::gen_rustler_error_converter(
            error,
            &core_import,
        ));
    }

    // NIF shims for whitelisted error introspection methods.
    // Each shim takes a string error message (the current Rustler error term
    // representation) and returns the method's default value.  When the
    // error-passing model is upgraded to structured terms these bodies will
    // be replaced with real dispatch; for now the declarations are emitted
    // so the Elixir public-API wrappers have matching NIF counterparts.
    for error in &api.errors {
        for method in error.methods.iter().filter(|m| !m.sanitized) {
            let fn_name = format!("{}_{}", error.name.to_lowercase(), method.name);
            let return_type = mapper.map_type(&method.return_type);
            let default_val = rustler_default_for_type(&method.return_type);
            let shim = format!(
                "/// Introspection NIF: returns the `{method_name}` value carried by the error.\n\
                     /// Planned: extend to accept a structured error term once error passing is upgraded.\n\
                     #[allow(dead_code)]\n\
                     #[rustler::nif]\n\
                     fn {fn_name}(_msg: String) -> {return_type} {{\n    {default_val}\n}}\n",
                method_name = method.name,
                fn_name = fn_name,
                return_type = return_type,
                default_val = default_val,
            );
            builder.add_item(&shim);
        }
    }

    // from_json NIF shims for Gleam e2e tests.
    // Only emit for types that have a corresponding NIF wrapper struct (types_to_emit).
    for typ in api.types.iter().filter(|t| {
        !t.is_trait
            && !t.is_opaque
            && !t.fields.is_empty()
            && t.has_serde
            && !exclude_types.contains(t.name.as_str())
            && types_to_emit.contains(&t.name)
    }) {
        builder.add_item(&gen_from_json_nif(typ, &core_import));
    }

    builder.add_item(&gen_nif_init(api, config, &exclude_functions, &exclude_types));

    let content = builder.build();

    let output_dir = resolve_output_dir(
        config.output_paths.get("elixir"),
        &config.name,
        "packages/elixir/native/{name}_nif/src/",
    );

    Ok(vec![GeneratedFile {
        path: PathBuf::from(&output_dir).join("lib.rs"),
        content,
        generated_header: false,
    }])
}