alef 0.26.4

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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
use alef::backends::zig::ZigBackend;
use alef::core::backend::Backend;
use alef::core::config::{ResolvedCrateConfig, new_config::NewAlefConfig};
use alef::core::ir::{
    ApiSurface, CoreWrapper, EnumDef, EnumVariant, ErrorDef, ErrorVariant, FieldDef, FunctionDef, ParamDef,
    PrimitiveType, TypeDef, TypeRef,
};

fn make_field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
    FieldDef {
        name: name.to_string(),
        ty,
        optional,
        default: None,
        doc: String::new(),
        sanitized: false,
        is_boxed: false,
        type_rust_path: None,
        cfg: None,
        typed_default: None,
        core_wrapper: CoreWrapper::None,
        vec_inner_core_wrapper: CoreWrapper::None,
        newtype_wrapper: None,
        serde_rename: None,
        serde_flatten: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    }
}

fn make_param(name: &str, ty: TypeRef) -> ParamDef {
    ParamDef {
        name: name.to_string(),
        ty,
        optional: false,
        default: None,
        sanitized: false,
        typed_default: None,
        is_ref: false,
        is_mut: false,
        newtype_wrapper: None,
        original_type: None,
        map_is_ahash: false,
        map_key_is_cow: false,
        vec_inner_is_ref: false,
        map_is_btree: false,
        core_wrapper: alef::core::ir::CoreWrapper::None,
    }
}

fn make_basic_api() -> ApiSurface {
    ApiSurface {
        crate_name: "demo".into(),
        version: "0.1.0".into(),
        types: vec![TypeDef {
            name: "Config".to_string(),
            rust_path: "demo::Config".to_string(),
            original_rust_path: String::new(),
            fields: vec![
                make_field("value", TypeRef::Primitive(PrimitiveType::I32), false),
                make_field("label", TypeRef::String, false),
                make_field("tag", TypeRef::Optional(Box::new(TypeRef::String)), true),
            ],
            methods: vec![],
            is_opaque: false,
            is_clone: true,
            is_copy: false,
            doc: "A demo configuration struct.".to_string(),
            cfg: None,
            is_trait: false,
            has_default: false,
            has_stripped_cfg_fields: false,
            is_return_type: false,
            serde_rename_all: None,
            has_serde: true,
            super_traits: vec![],
            binding_excluded: false,
            binding_exclusion_reason: None,
            is_variant_wrapper: false,
            has_lifetime_params: false,
            version: Default::default(),
        }],
        functions: vec![FunctionDef {
            name: "process".into(),
            rust_path: "demo::process".into(),
            original_rust_path: String::new(),
            params: vec![
                make_param("input", TypeRef::String),
                make_param("count", TypeRef::Primitive(PrimitiveType::U32)),
            ],
            return_type: TypeRef::String,
            is_async: false,
            error_type: Some("DemoError".to_string()),
            doc: "Process input and return a result.".to_string(),
            cfg: None,
            sanitized: false,
            return_sanitized: false,
            returns_ref: false,
            returns_cow: false,
            return_newtype_wrapper: None,
            binding_excluded: false,
            binding_exclusion_reason: None,
            version: Default::default(),
        }],
        enums: vec![EnumDef {
            name: "Status".to_string(),
            rust_path: "demo::Status".to_string(),
            original_rust_path: String::new(),
            variants: vec![
                EnumVariant {
                    name: "Active".to_string(),
                    fields: vec![],
                    doc: "Active state.".to_string(),
                    is_default: false,
                    serde_rename: None,
                    binding_excluded: false,
                    binding_exclusion_reason: None,
                    is_tuple: false,
                    originally_had_data_fields: false,
                    cfg: None,
                    version: Default::default(),
                },
                EnumVariant {
                    name: "Inactive".to_string(),
                    fields: vec![],
                    doc: "Inactive state.".to_string(),
                    is_default: false,
                    serde_rename: None,
                    binding_excluded: false,
                    binding_exclusion_reason: None,
                    is_tuple: false,
                    originally_had_data_fields: false,
                    cfg: None,
                    version: Default::default(),
                },
            ],
            methods: vec![],
            doc: "Processing status.".to_string(),
            cfg: None,
            is_copy: false,
            has_serde: false,
            has_default: false,
            serde_tag: None,
            serde_untagged: false,
            serde_rename_all: None,
            binding_excluded: false,
            binding_exclusion_reason: None,
            excluded_variants: vec![],
            version: Default::default(),
        }],
        errors: vec![ErrorDef {
            name: "DemoError".to_string(),
            rust_path: "demo::DemoError".to_string(),
            original_rust_path: String::new(),
            variants: vec![
                ErrorVariant {
                    name: "InvalidInput".to_string(),
                    message_template: Some("invalid input provided".to_string()),
                    fields: vec![],
                    has_source: false,
                    has_from: false,
                    is_unit: true,
                    is_tuple: false,
                    doc: "Input validation failed.".to_string(),
                },
                ErrorVariant {
                    name: "ProcessingFailed".to_string(),
                    message_template: Some("processing failed".to_string()),
                    fields: vec![],
                    has_source: false,
                    has_from: false,
                    is_unit: true,
                    is_tuple: false,
                    doc: "Processing encountered an error.".to_string(),
                },
            ],
            doc: "Errors emitted by demo operations.".to_string(),
            methods: vec![],
            binding_excluded: false,
            binding_exclusion_reason: None,
            version: Default::default(),
        }],
        excluded_type_paths: ::std::collections::HashMap::new(),
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    }
}

fn make_basic_config() -> ResolvedCrateConfig {
    let toml = r#"
[workspace]
languages = ["zig"]

[[crates]]
name = "demo"
sources = ["src/lib.rs"]
"#;
    let cfg: NewAlefConfig = toml::from_str(toml).expect("test config must parse");
    cfg.resolve().expect("test config must resolve").remove(0)
}

#[test]
fn snapshot_basic_struct_function_enum_error() {
    let api = make_basic_api();
    let config = make_basic_config();
    let files = ZigBackend.generate_bindings(&api, &config).unwrap();
    for file in &files {
        insta::assert_snapshot!(
            format!("snapshot_basic__{}", file.path.display().to_string().replace('/', "__")),
            &file.content
        );
    }
}

#[test]
fn trait_bridge_vtable_builder_coverage() {
    // Regression test: verify that for every trait bridge configured, the Zig backend
    // emits a `make_{trait_snake}_vtable` comptime constructor function.
    // This ensures trait-bridge e2e test fixtures that call these builders will compile.
    //
    // The invariant: whenever alef.toml registers a trait bridge with
    // `trait_name = "SomeTrait"`, the Zig binding must emit both:
    //   1. A vtable struct: `pub struct ISomeTrait { ... }`
    //   2. A comptime builder: `pub fn make_some_trait_vtable(...) ISomeTrait { ... }`
    //
    // This test uses the public backend API to generate bindings for a synthetic
    // trait and verifies both are present.

    use alef::core::config::{BridgeBinding, TraitBridgeConfig};

    let mut api = make_basic_api();
    let method = alef::core::ir::MethodDef {
        name: "process".to_string(),
        params: vec![make_param("input", TypeRef::String)],
        return_type: TypeRef::String,
        is_async: false,
        is_static: false,
        error_type: None,
        doc: String::new(),
        receiver: Some(alef::core::ir::ReceiverKind::Ref),
        trait_source: None,
        sanitized: false,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };

    // Add a trait type (marked as a trait with is_trait=true)
    let trait_def = TypeDef {
        name: "PluginTrait".to_string(),
        rust_path: "demo::PluginTrait".to_string(),
        original_rust_path: String::new(),
        fields: vec![],
        methods: vec![method],
        is_opaque: false,
        is_clone: false,
        is_copy: false,
        doc: "A test plugin trait".to_string(),
        cfg: None,
        is_trait: true, // CRITICAL: mark as trait
        has_default: false,
        has_stripped_cfg_fields: false,
        is_return_type: false,
        serde_rename_all: None,
        has_serde: false,
        super_traits: vec![],
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_variant_wrapper: false,
        has_lifetime_params: false,
        version: Default::default(),
    };
    api.types.push(trait_def);

    // Configure a trait bridge for that trait
    let mut config = make_basic_config();
    config.trait_bridges.push(TraitBridgeConfig {
        trait_name: "PluginTrait".to_string(),
        super_trait: None,
        registry_getter: Some("demo::registry::get_plugin_registry".to_string()),
        register_fn: Some("register_plugin".to_string()),
        unregister_fn: Some("unregister_plugin".to_string()),
        clear_fn: Some("clear_plugins".to_string()),
        bind_via: BridgeBinding::FunctionParam,
        ffi_skip_methods: vec![],
        type_alias: None,
        param_name: None,
        register_extra_args: None,
        exclude_languages: vec![],
        options_type: None,
        options_field: None,
        context_type: None,
        result_type: None,
    });

    // Generate bindings
    let files = ZigBackend.generate_bindings(&api, &config).unwrap();
    let content = files
        .iter()
        .find(|f| f.content.contains("pub fn make_") || f.content.contains("pub struct I"))
        .or_else(|| files.first())
        .expect("zig binding file must be generated")
        .content
        .clone();

    // Verify: vtable struct exists
    assert!(
        content.contains("pub const IPluginTrait = extern struct {"),
        "BUG: vtable struct missing. Content preview:\n{}",
        &content[..std::cmp::min(1500, content.len())]
    );

    // Verify: vtable builder exists
    assert!(
        content.contains("pub fn make_plugin_trait_vtable(comptime T: type, instance: *T) IPluginTrait {"),
        "BUG: vtable builder missing. Expected 'pub fn make_plugin_trait_vtable(...)' in generated code."
    );
}

#[test]
fn trait_bridge_multiple_traits_emit_all_vtable_builders() {
    // Regression test for B10: verify that ALL registered trait bridges get their
    // vtable builder functions emitted, not just some of them.
    // This replicates the kreuzberg scenario with 6 traits.

    use alef::core::config::{BridgeBinding, TraitBridgeConfig};

    let mut api = make_basic_api();

    // Define 6 traits matching the kreuzberg plugin system
    let trait_names = vec![
        "DocumentExtractor",
        "OcrBackend",
        "PostProcessor",
        "EmbeddingBackend",
        "Renderer",
        "Validator",
    ];

    for trait_name in &trait_names {
        let method = alef::core::ir::MethodDef {
            name: "process".to_string(),
            params: vec![make_param("input", TypeRef::String)],
            return_type: TypeRef::Unit,
            is_async: false,
            is_static: false,
            error_type: None,
            doc: String::new(),
            receiver: Some(alef::core::ir::ReceiverKind::Ref),
            trait_source: None,
            sanitized: false,
            returns_ref: false,
            returns_cow: false,
            return_newtype_wrapper: None,
            has_default_impl: false,
            binding_excluded: false,
            binding_exclusion_reason: None,
            version: Default::default(),
        };

        let trait_def = TypeDef {
            name: trait_name.to_string(),
            rust_path: format!("demo::{}", trait_name),
            original_rust_path: String::new(),
            fields: vec![],
            methods: vec![method],
            is_opaque: false,
            is_clone: false,
            is_copy: false,
            doc: format!("Test trait: {}", trait_name),
            cfg: None,
            is_trait: true,
            has_default: false,
            has_stripped_cfg_fields: false,
            is_return_type: false,
            serde_rename_all: None,
            has_serde: false,
            super_traits: vec![],
            binding_excluded: false,
            binding_exclusion_reason: None,
            is_variant_wrapper: false,
            has_lifetime_params: false,
            version: Default::default(),
        };
        api.types.push(trait_def);
    }

    // Configure trait bridges for all 6 traits
    let mut config = make_basic_config();
    for trait_name in &trait_names {
        let snake = heck::AsSnakeCase(trait_name).to_string();
        config.trait_bridges.push(TraitBridgeConfig {
            trait_name: trait_name.to_string(),
            super_trait: None,
            registry_getter: None,
            register_fn: Some(format!("register_{}", snake)),
            unregister_fn: Some(format!("unregister_{}", snake)),
            clear_fn: None,
            bind_via: BridgeBinding::FunctionParam,
            ffi_skip_methods: vec![],
            type_alias: None,
            param_name: None,
            register_extra_args: None,
            exclude_languages: vec![],
            options_type: None,
            options_field: None,
            context_type: None,
            result_type: None,
        });
    }

    // Generate bindings
    let files = ZigBackend.generate_bindings(&api, &config).unwrap();
    let content = files
        .iter()
        .find(|f| f.content.contains("pub fn make_") || f.content.contains("pub struct I"))
        .or_else(|| files.first())
        .expect("zig binding file must be generated")
        .content
        .clone();

    // Verify all 6 vtable builders are emitted
    for trait_name in &trait_names {
        let snake = heck::AsSnakeCase(trait_name).to_string();
        let expected_builder = format!("pub fn make_{}_vtable(comptime T: type, instance: *T)", snake);
        assert!(
            content.contains(&expected_builder),
            "BUG: missing make_{}_vtable builder for trait {}. Generated code:\n{}",
            snake,
            trait_name,
            &content[..std::cmp::min(2000, content.len())]
        );
    }
}

#[test]
fn trait_bridge_vcoverage_assertion_catches_missing_trait_definitions() {
    // Regression test for B10 fallout: trait bridges registered but trait definitions
    // missing/excluded should produce a hard error, not silent omission of vtable builders.
    //
    // BUG PATTERN: alef.toml registers a trait bridge with `trait_name = "Foo"`,
    // but the Rust source doesn't export `Foo` as a trait (or it's excluded from
    // the binding surface). The current code silently skips emit_trait_bridge()
    // via the `if let Some(trait_def) = ...` guard at line 280, leaving
    // e2e tests with dangling references to `make_foo_vtable(...)` that don't
    // exist in the generated binding.
    //
    // This test verifies that the emitter enforces the invariant: every registered
    // trait bridge MUST have a corresponding trait definition in the API surface,
    // or the build should fail explicitly.

    use alef::core::config::{BridgeBinding, TraitBridgeConfig};

    let api = make_basic_api();

    // DO NOT add trait definitions to the API.
    // Configure trait bridges for 2 traits that don't exist in the API.
    let mut config = make_basic_config();
    config.trait_bridges.push(TraitBridgeConfig {
        trait_name: "MissingTrait1".to_string(),
        super_trait: None,
        registry_getter: None,
        register_fn: Some("register_missing1".to_string()),
        unregister_fn: None,
        clear_fn: None,
        bind_via: BridgeBinding::FunctionParam,
        ffi_skip_methods: vec![],
        type_alias: None,
        param_name: None,
        register_extra_args: None,
        exclude_languages: vec![],
        options_type: None,
        options_field: None,
        context_type: None,
        result_type: None,
    });

    let err = ZigBackend
        .generate_bindings(&api, &config)
        .expect_err("missing trait bridge definitions must be rejected");
    let message = err.to_string();
    assert!(
        message.contains("MissingTrait1") && message.contains("has no trait definition"),
        "missing trait bridge error should name the absent trait and reason; got: {message}"
    );
}

#[test]
fn trait_bridge_register_fn_passes_vtable_pointer_not_value() {
    // Regression test: B20 — verify that generated registration functions pass
    // the vtable as a typed pointer (`&_c_vtable`) not as a value (`_c_vtable`).
    //
    // The C FFI layer expects `demo_register_ocr_backend(..., vtable: [*c]const struct_*, ...)`.
    // The Zig wrapper creates a local `_c_vtable` value via `@bitCast`, then must pass
    // its address `&_c_vtable` to the C function. Passing the value directly causes a type error.

    use alef::core::config::{BridgeBinding, TraitBridgeConfig};

    let mut api = make_basic_api();
    let method = alef::core::ir::MethodDef {
        name: "process".to_string(),
        params: vec![make_param("input", TypeRef::String)],
        return_type: TypeRef::Unit,
        is_async: false,
        is_static: false,
        error_type: None,
        doc: String::new(),
        receiver: Some(alef::core::ir::ReceiverKind::Ref),
        trait_source: None,
        sanitized: false,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };

    let trait_def = TypeDef {
        name: "OcrBackend".to_string(),
        rust_path: "demo::OcrBackend".to_string(),
        original_rust_path: String::new(),
        fields: vec![],
        methods: vec![method],
        is_opaque: false,
        is_clone: false,
        is_copy: false,
        doc: "OCR backend trait".to_string(),
        cfg: None,
        is_trait: true,
        has_default: false,
        has_stripped_cfg_fields: false,
        is_return_type: false,
        serde_rename_all: None,
        has_serde: false,
        super_traits: vec![],
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_variant_wrapper: false,
        has_lifetime_params: false,
        version: Default::default(),
    };
    api.types.push(trait_def);

    let mut config = make_basic_config();
    config.trait_bridges.push(TraitBridgeConfig {
        trait_name: "OcrBackend".to_string(),
        super_trait: Some("demo::Plugin".to_string()),
        registry_getter: Some("demo::registry::get_ocr_registry".to_string()),
        register_fn: Some("register_ocr_backend".to_string()),
        unregister_fn: Some("unregister_ocr_backend".to_string()),
        clear_fn: None,
        bind_via: BridgeBinding::FunctionParam,
        ffi_skip_methods: vec![],
        type_alias: None,
        param_name: None,
        register_extra_args: None,
        exclude_languages: vec![],
        options_type: None,
        options_field: None,
        context_type: None,
        result_type: None,
    });

    let files = ZigBackend.generate_bindings(&api, &config).unwrap();
    let content = files
        .iter()
        .find(|f| f.content.contains("pub fn register_ocr_backend"))
        .or_else(|| files.first())
        .expect("zig binding file must be generated")
        .content
        .clone();

    // CRITICAL: The register function must pass `&_c_vtable` (typed pointer),
    // NOT `_c_vtable` (value).
    // Pattern: `const _rc = c.demo_register_ocr_backend(name, &_c_vtable, ...)`
    assert!(
        content.contains("c.demo_register_ocr_backend(name, &_c_vtable,"),
        "BUG: register_ocr_backend should pass `&_c_vtable` (pointer), not `_c_vtable` (value). Content:\n{}",
        &content
    );

    // Negative check: should NOT pass the value directly
    assert!(
        !content.contains("c.demo_register_ocr_backend(name, _c_vtable,")
            || content.contains("c.demo_register_ocr_backend(name, &_c_vtable,"),
        "BUG: register_ocr_backend passing value instead of pointer"
    );
}

#[test]
fn fallible_function_returning_primitive_no_null_check() {
    // Regression test for alef zig codegen bug: primitive return types (u64, bool, etc.)
    // cannot be null and should NOT emit `if (_result == null)` check.
    // See: tslp packages/zig/src/tree_sitter_language_pack.zig line 766 in v1.9.0-rc.26
    //
    // Bug symptom: Zig 0.16 error "comparison of 'u64' with null"
    // Root cause: codegen unconditionally emitted null-check for non-Unit, non-Bytes returns
    // without checking if the type can actually be null.

    let mut api = make_basic_api();
    // Add a function that returns u64 and is fallible (has error_type)
    api.functions.push(FunctionDef {
        name: "count_items".into(),
        rust_path: "demo::count_items".into(),
        original_rust_path: String::new(),
        params: vec![],
        return_type: TypeRef::Primitive(PrimitiveType::U64),
        is_async: false,
        error_type: Some("DemoError".to_string()),
        doc: "Count items, returning a u64 result.".to_string(),
        cfg: None,
        sanitized: false,
        return_sanitized: false,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    });

    let config = make_basic_config();
    let files = ZigBackend.generate_bindings(&api, &config).unwrap();
    let content = files
        .iter()
        .find(|f| f.content.contains("count_items"))
        .or_else(|| files.first())
        .expect("zig binding file must be generated")
        .content
        .clone();

    // CRITICAL: Extract just the count_items function and verify it does NOT contain null check
    let start = content
        .find("pub fn count_items")
        .expect("count_items function must exist");
    let end = content[start..].find("}\n").expect("function must have closing brace") + start + 2;
    let count_items_fn = &content[start..end];

    assert!(
        !count_items_fn.contains("if (_result == null)"),
        "BUG: fallible function returning u64 should NOT emit null check. \
         Zig 0.16 error: 'comparison of u64 with null'. Function:\n{}",
        count_items_fn
    );

    // POSITIVE: Should contain the error check and early return via last_error_code
    assert!(
        content.contains("if (c.demo_last_error_code() != 0)"),
        "BUG: fallible function should check last_error_code. Content:\n{}",
        &content
    );

    // The function should return the result directly without a null guard
    assert!(
        content.contains("return _result;") || content.contains("return"),
        "BUG: function should have a return statement. Content:\n{}",
        &content
    );
}