alef 0.107.12

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
//! Source and input hash coverage, split from tests.rs.

use super::tests::*;
use super::*;
use tempfile::tempdir;

#[test]
fn sources_hash_changes_with_content() {
    let dir = tempdir().unwrap();
    let s = write_file(dir.path(), "a.rs", "fn a() {}");
    let h_before = compute_sources_hash(std::slice::from_ref(&s)).unwrap();
    std::fs::write(&s, "fn a() { let _ = 1; }").unwrap();
    let h_after = compute_sources_hash(&[s]).unwrap();
    assert_ne!(h_before, h_after);
}

#[test]
fn file_hash_idempotent_under_strip_hash_line() {
    let bare = "// auto-generated by alef\nfn body() {}\n";
    let with_line = "// auto-generated by alef\n// alef:hash:deadbeef\nfn body() {}\n";

    let h1 = compute_file_hash(bare);
    let h2 = compute_file_hash(with_line);
    assert_eq!(h1, h2, "hash must ignore an existing alef:hash: line");
}

/// Anti-churn guarantee: no inputs-hash argument means an unrelated source/`alef.toml`
/// change cannot move an untouched file's stamp. See `core::hash`'s module doc. ~keep
#[test]
fn file_hash_does_not_change_when_generation_inputs_change() {
    let content = "// auto-generated by alef\nfn body() {}\n";
    let h_a = compute_file_hash(content);
    let h_b = compute_file_hash(content);
    assert_eq!(
        h_a, h_b,
        "identical file content must hash identically regardless of generation inputs -- \
         `compute_file_hash` has no inputs_hash parameter to differ on"
    );
}

#[test]
fn file_hash_changes_when_content_changes() {
    let h_a = compute_file_hash("fn a() {}\n");
    let h_b = compute_file_hash("fn b() {}\n");
    assert_ne!(h_a, h_b);
}

/// ~keep This asserted only that the digest was 64 hex chars, so it passed no matter what
/// `compute_file_hash` folded in -- it could never have failed and gave false confidence that
/// the pin was already excluded, which it was not. The real independence claim is now proved by
/// `inputs_hash_alef_version_pin_table`; what is left here is the domain-separator check the
/// length assertion was actually standing in for.
#[test]
fn file_hash_is_domain_separated_from_a_bare_blake3_digest() {
    let content = "fn a() {}\n";
    let h = compute_file_hash(content);
    assert_eq!(h.len(), 64, "blake3 hex output is 64 chars");
    assert_ne!(
        h,
        blake3::hash(content.as_bytes()).to_hex().to_string(),
        "file hash must use its own domain separator, not digest the content alone"
    );
}

#[test]
fn inputs_hash_is_stable() {
    let h1 = compute_inputs_hash("abc", b"toml");
    let h2 = compute_inputs_hash("abc", b"toml");
    assert_eq!(h1, h2, "compute_inputs_hash must be deterministic");
    assert_eq!(h1.len(), 64, "blake3 hex output is 64 chars");
}

#[test]
fn inputs_hash_changes_when_sources_hash_changes() {
    let h1 = compute_inputs_hash("sources_a", b"toml");
    let h2 = compute_inputs_hash("sources_b", b"toml");
    assert_ne!(h1, h2);
}

#[test]
fn inputs_hash_changes_when_alef_toml_changes() {
    let h1 = compute_inputs_hash("sources", b"[workspace]\nlanguages=[\"python\"]\n");
    let h2 = compute_inputs_hash("sources", b"[workspace]\nlanguages=[\"ruby\"]\n");
    assert_ne!(h1, h2);
}

#[test]
fn inputs_hash_uses_domain_separator() {
    let h = compute_inputs_hash("", b"");
    assert_eq!(h.len(), 64);
    let plain_empty = blake3::hash(b"").to_hex().to_string();
    assert_ne!(
        h, plain_empty,
        "inputs hash must include the alef:inputs domain separator and CODEGEN_FORMAT_VERSION"
    );
}

#[test]
fn inputs_hash_invariant_to_toml_comment_change() {
    let without_comment = b"[workspace]\nlanguages = [\"python\"]\n" as &[u8];
    let with_comment = b"# repo-level config\n[workspace]\nlanguages = [\"python\"]\n" as &[u8];
    let h1 = compute_inputs_hash("sources_abc", without_comment);
    let h2 = compute_inputs_hash("sources_abc", with_comment);
    assert_eq!(
        h1, h2,
        "comment-only change to alef.toml must not invalidate generated files"
    );
}

#[test]
fn inputs_hash_invariant_to_toml_whitespace_and_key_reorder() {
    let a = b"[workspace]\nbar = 2\nfoo = 1\n" as &[u8];
    let b_bytes = b"[workspace]\nfoo = 1\nbar = 2\n" as &[u8];
    let h1 = compute_inputs_hash("sources_xyz", a);
    let h2 = compute_inputs_hash("sources_xyz", b_bytes);
    assert_eq!(
        h1, h2,
        "key-reordering in alef.toml must not invalidate generated files"
    );
}

#[test]
fn inputs_hash_invariant_to_toml_crlf_vs_lf() {
    let lf = b"[workspace]\nlanguages = [\"ruby\"]\n" as &[u8];
    let crlf = b"[workspace]\r\nlanguages = [\"ruby\"]\r\n" as &[u8];
    let h1 = compute_inputs_hash("sources_def", lf);
    let h2 = compute_inputs_hash("sources_def", crlf);
    assert_eq!(
        h1, h2,
        "CRLF vs LF difference in alef.toml must not invalidate generated files"
    );
}

#[test]
fn inputs_hash_stable_independent_of_alef_rev() {
    let h1 = compute_inputs_hash("stable_sources", b"[workspace]\nlanguages = [\"python\"]\n");
    let h2 = compute_inputs_hash("stable_sources", b"[workspace]\nlanguages = [\"python\"]\n");
    assert_eq!(
        h1, h2,
        "hash must be stable; ALEF_REV is not an input to compute_inputs_hash"
    );
    assert_eq!(h1.len(), 64);
}

#[test]
fn inputs_hash_tolerates_empty_alef_toml() {
    let h = compute_inputs_hash("some_sources_hash", b"");
    assert_eq!(h.len(), 64);
}

/// Table-driven cases proving `alef_version` is excluded from `inputs_hash` while every other
/// `[workspace]` key still participates. `expect_equal = true` means the two configs must hash
/// identically; `false` means they must differ.
#[test]
fn inputs_hash_alef_version_pin_table() {
    struct Case {
        name: &'static str,
        toml_a: &'static [u8],
        toml_b: &'static [u8],
        expect_equal: bool,
    }

    let cases = [
        Case {
            name: "alef_version bump alone does not change the hash",
            toml_a: b"[workspace]\nalef_version = \"0.61.0\"\nlanguages = [\"python\"]\n",
            toml_b: b"[workspace]\nalef_version = \"0.61.1\"\nlanguages = [\"python\"]\n",
            expect_equal: true,
        },
        Case {
            name: "adding an alef_version pin where none existed does not change the hash",
            toml_a: b"[workspace]\nlanguages = [\"python\"]\n",
            toml_b: b"[workspace]\nalef_version = \"0.61.1\"\nlanguages = [\"python\"]\n",
            expect_equal: true,
        },
        Case {
            name: "a real workspace key change still changes the hash (control)",
            toml_a: b"[workspace]\nalef_version = \"0.61.0\"\nlanguages = [\"python\"]\n",
            toml_b: b"[workspace]\nalef_version = \"0.61.0\"\nlanguages = [\"ruby\"]\n",
            expect_equal: false,
        },
        Case {
            name: "changing alef_version together with a real key still changes the hash",
            toml_a: b"[workspace]\nalef_version = \"0.61.0\"\nlanguages = [\"python\"]\n",
            toml_b: b"[workspace]\nalef_version = \"0.61.1\"\nlanguages = [\"ruby\"]\n",
            expect_equal: false,
        },
    ];

    for case in cases {
        let h1 = compute_inputs_hash("sources_pin_table", case.toml_a);
        let h2 = compute_inputs_hash("sources_pin_table", case.toml_b);
        if case.expect_equal {
            assert_eq!(h1, h2, "case `{}` expected equal hashes", case.name);
        } else {
            assert_ne!(h1, h2, "case `{}` expected different hashes", case.name);
        }
    }
}

#[test]
fn inputs_hash_differs_from_file_hash() {
    let sources = "abc";
    let content = "fn a() {}\n";
    let ih = compute_inputs_hash(sources, content.as_bytes());
    let fh = compute_file_hash(content);
    assert_ne!(ih, fh, "inputs hash and file hash must not collide");
}

#[test]
fn crate_sources_hash_differs_across_crates_with_disjoint_sources() {
    use crate::core::config::resolved::ResolvedCrateConfig;
    let dir = tempdir().unwrap();
    let a = write_file(dir.path(), "a.rs", "fn a() {}");
    let b = write_file(dir.path(), "b.rs", "fn b() {}");
    let make_cfg = |name: &str, sources: Vec<std::path::PathBuf>| ResolvedCrateConfig {
        name: name.to_string(),
        sources,
        source_crates: vec![],
        resolved_source_crates: Default::default(),
        version_from: "Cargo.toml".to_string(),
        core_import: None,
        workspace_root: None,
        skip_core_import: false,
        error_type: None,
        error_constructor: None,
        features: vec![],
        wrapper_default_features: vec![],
        path_mappings: Default::default(),
        extra_dependencies: Default::default(),
        auto_path_mappings: true,
        languages: vec![],
        targets: Default::default(),
        python: None,
        node: None,
        ruby: None,
        php: None,
        elixir: None,
        wasm: None,
        ffi: None,
        go: None,
        java: None,
        dart: None,
        kotlin: None,
        kotlin_android: None,
        jni: None,
        swift: None,
        gleam: None,
        csharp: None,
        r: None,
        zig: None,
        exclude: Default::default(),
        include: Default::default(),
        output_paths: Default::default(),
        explicit_output: Default::default(),
        test: Default::default(),
        build_commands: Default::default(),
        generate: Default::default(),
        generate_overrides: Default::default(),
        dto: Default::default(),
        tools: Default::default(),
        opaque_types: Default::default(),
        client_constructors: Default::default(),
        sync: None,
        citation: None,
        publish: None,
        e2e: None,
        adapters: vec![],
        trait_bridges: vec![],
        services: vec![],
        handler_contracts: vec![],
        scaffold: None,
        package_metadata: None,
        readme: None,
        docs: None,
        custom_files: Default::default(),
        custom_modules: Default::default(),
        custom_registrations: Default::default(),
        suppress_validation_codes: Vec::new(),
        untagged_union_text_types: vec![],
        poly: Default::default(),
        extra_clippy_allows: vec![],
        crate_attributes: vec![],
        cargo_lints: Default::default(),
        verify: Default::default(),
    };

    let cfg_a = make_cfg("alpha", vec![a]);
    let cfg_b = make_cfg("beta", vec![b]);

    let hash_a = compute_crate_sources_hash(&cfg_a).unwrap();
    let hash_b = compute_crate_sources_hash(&cfg_b).unwrap();

    assert_ne!(
        hash_a, hash_b,
        "crates with disjoint sources must produce different hashes"
    );
}

#[test]
fn crate_sources_hash_includes_source_crates() {
    use crate::core::config::{SourceCrate, resolved::ResolvedCrateConfig};
    let dir = tempdir().unwrap();
    let a = write_file(dir.path(), "a.rs", "fn a() {}");
    let b = write_file(dir.path(), "b.rs", "fn b() {}");
    let make_cfg =
        |sources: Vec<std::path::PathBuf>, source_crate_sources: Vec<std::path::PathBuf>| -> ResolvedCrateConfig {
            let source_crates = if source_crate_sources.is_empty() {
                vec![]
            } else {
                vec![SourceCrate {
                    name: "extra-crate".to_string(),
                    sources: source_crate_sources,
                    roots: vec![],
                    from_registry: false,
                }]
            };
            ResolvedCrateConfig {
                name: "test".to_string(),
                sources,
                source_crates,
                resolved_source_crates: Default::default(),
                version_from: "Cargo.toml".to_string(),
                core_import: None,
                workspace_root: None,
                skip_core_import: false,
                error_type: None,
                error_constructor: None,
                features: vec![],
                wrapper_default_features: vec![],
                path_mappings: Default::default(),
                extra_dependencies: Default::default(),
                auto_path_mappings: true,
                languages: vec![],
                targets: Default::default(),
                python: None,
                node: None,
                ruby: None,
                php: None,
                elixir: None,
                wasm: None,
                ffi: None,
                go: None,
                java: None,
                dart: None,
                kotlin: None,
                kotlin_android: None,
                jni: None,
                swift: None,
                gleam: None,
                csharp: None,
                r: None,
                zig: None,
                exclude: Default::default(),
                include: Default::default(),
                output_paths: Default::default(),
                explicit_output: Default::default(),
                test: Default::default(),
                build_commands: Default::default(),
                generate: Default::default(),
                generate_overrides: Default::default(),
                dto: Default::default(),
                tools: Default::default(),
                opaque_types: Default::default(),
                client_constructors: Default::default(),
                sync: None,
                citation: None,
                publish: None,
                e2e: None,
                adapters: vec![],
                trait_bridges: vec![],
                services: vec![],
                handler_contracts: vec![],
                scaffold: None,
                package_metadata: None,
                readme: None,
                docs: None,
                custom_files: Default::default(),
                custom_modules: Default::default(),
                custom_registrations: Default::default(),
                suppress_validation_codes: Vec::new(),
                untagged_union_text_types: vec![],
                poly: Default::default(),
                extra_clippy_allows: vec![],
                crate_attributes: vec![],
                cargo_lints: Default::default(),
                verify: Default::default(),
            }
        };

    let cfg_without_extra = make_cfg(vec![a.clone()], vec![]);
    let cfg_with_extra = make_cfg(vec![a.clone()], vec![b.clone()]);

    let hash_without = compute_crate_sources_hash(&cfg_without_extra).unwrap();
    let hash_with = compute_crate_sources_hash(&cfg_with_extra).unwrap();

    assert_ne!(
        hash_without, hash_with,
        "adding a source_crate source file must change the hash"
    );
}

#[test]
fn compute_crate_sources_hash_dedupes_overlapping_paths() {
    use crate::core::config::{SourceCrate, resolved::ResolvedCrateConfig};
    let dir = tempdir().unwrap();
    let a = write_file(dir.path(), "a.rs", "fn a() {}");
    let b = write_file(dir.path(), "b.rs", "fn b() {}");

    let make_cfg =
        |sources: Vec<std::path::PathBuf>, source_crate_sources: Vec<std::path::PathBuf>| -> ResolvedCrateConfig {
            let source_crates = if source_crate_sources.is_empty() {
                vec![]
            } else {
                vec![SourceCrate {
                    name: "extra-crate".to_string(),
                    sources: source_crate_sources,
                    roots: vec![],
                    from_registry: false,
                }]
            };
            ResolvedCrateConfig {
                name: "test".to_string(),
                sources,
                source_crates,
                resolved_source_crates: Default::default(),
                version_from: "Cargo.toml".to_string(),
                core_import: None,
                workspace_root: None,
                skip_core_import: false,
                error_type: None,
                error_constructor: None,
                features: vec![],
                wrapper_default_features: vec![],
                path_mappings: Default::default(),
                extra_dependencies: Default::default(),
                auto_path_mappings: true,
                languages: vec![],
                targets: Default::default(),
                python: None,
                node: None,
                ruby: None,
                php: None,
                elixir: None,
                wasm: None,
                ffi: None,
                go: None,
                java: None,
                dart: None,
                kotlin: None,
                kotlin_android: None,
                jni: None,
                swift: None,
                gleam: None,
                csharp: None,
                r: None,
                zig: None,
                exclude: Default::default(),
                include: Default::default(),
                output_paths: Default::default(),
                explicit_output: Default::default(),
                test: Default::default(),
                build_commands: Default::default(),
                generate: Default::default(),
                generate_overrides: Default::default(),
                dto: Default::default(),
                tools: Default::default(),
                opaque_types: Default::default(),
                client_constructors: Default::default(),
                sync: None,
                citation: None,
                publish: None,
                e2e: None,
                adapters: vec![],
                trait_bridges: vec![],
                services: vec![],
                handler_contracts: vec![],
                scaffold: None,
                package_metadata: None,
                readme: None,
                docs: None,
                custom_files: Default::default(),
                custom_modules: Default::default(),
                custom_registrations: Default::default(),
                suppress_validation_codes: Vec::new(),
                untagged_union_text_types: vec![],
                poly: Default::default(),
                extra_clippy_allows: vec![],
                crate_attributes: vec![],
                cargo_lints: Default::default(),
                verify: Default::default(),
            }
        };

    let cfg_with_dupes = make_cfg(vec![a.clone(), a.clone(), b.clone()], vec![a.clone()]);
    let cfg_unique = make_cfg(vec![a.clone(), b.clone()], vec![]);

    let hash_dup = compute_crate_sources_hash(&cfg_with_dupes).unwrap();
    let hash_unique = compute_crate_sources_hash(&cfg_unique).unwrap();
    assert_eq!(
        hash_dup, hash_unique,
        "duplicate source paths must not affect the per-crate sources hash"
    );
}

#[test]
fn compute_crate_sources_hash_is_order_independent() {
    use crate::core::config::resolved::ResolvedCrateConfig;
    let dir = tempdir().unwrap();
    let a = write_file(dir.path(), "a.rs", "fn a() {}");
    let b = write_file(dir.path(), "b.rs", "fn b() {}");
    let c = write_file(dir.path(), "c.rs", "fn c() {}");

    let make_cfg = |sources: Vec<std::path::PathBuf>| -> ResolvedCrateConfig {
        ResolvedCrateConfig {
            name: "test".to_string(),
            sources,
            source_crates: vec![],
            resolved_source_crates: Default::default(),
            version_from: "Cargo.toml".to_string(),
            core_import: None,
            workspace_root: None,
            skip_core_import: false,
            error_type: None,
            error_constructor: None,
            features: vec![],
            wrapper_default_features: vec![],
            path_mappings: Default::default(),
            extra_dependencies: Default::default(),
            auto_path_mappings: true,
            languages: vec![],
            targets: Default::default(),
            python: None,
            node: None,
            ruby: None,
            php: None,
            elixir: None,
            wasm: None,
            ffi: None,
            go: None,
            java: None,
            dart: None,
            kotlin: None,
            kotlin_android: None,
            jni: None,
            swift: None,
            gleam: None,
            csharp: None,
            r: None,
            zig: None,
            exclude: Default::default(),
            include: Default::default(),
            output_paths: Default::default(),
            explicit_output: Default::default(),
            test: Default::default(),
            build_commands: Default::default(),
            generate: Default::default(),
            generate_overrides: Default::default(),
            dto: Default::default(),
            tools: Default::default(),
            opaque_types: Default::default(),
            client_constructors: Default::default(),
            sync: None,
            citation: None,
            publish: None,
            e2e: None,
            adapters: vec![],
            trait_bridges: vec![],
            services: vec![],
            handler_contracts: vec![],
            scaffold: None,
            package_metadata: None,
            readme: None,
            docs: None,
            custom_files: Default::default(),
            custom_modules: Default::default(),
            custom_registrations: Default::default(),
            suppress_validation_codes: Vec::new(),
            untagged_union_text_types: vec![],
            poly: Default::default(),
            extra_clippy_allows: vec![],
            crate_attributes: vec![],
            cargo_lints: Default::default(),
            verify: Default::default(),
        }
    };

    let cfg1 = make_cfg(vec![a.clone(), b.clone(), c.clone()]);
    let cfg2 = make_cfg(vec![c.clone(), a.clone(), b.clone()]);
    let cfg3 = make_cfg(vec![b.clone(), c.clone(), a.clone()]);

    let h1 = compute_crate_sources_hash(&cfg1).unwrap();
    let h2 = compute_crate_sources_hash(&cfg2).unwrap();
    let h3 = compute_crate_sources_hash(&cfg3).unwrap();
    assert_eq!(h1, h2, "reordering sources must not change the hash");
    assert_eq!(h2, h3, "reordering sources must not change the hash");
}

#[test]
fn file_hash_round_trip_via_inject_extract() {
    let raw = "// auto-generated by alef\nfn body() {}\n";
    let file_hash = compute_file_hash(raw);
    let on_disk = inject_hash_line(raw, &file_hash);

    let extracted = extract_hash(&on_disk).expect("hash line should be present");
    let recomputed = compute_file_hash(&on_disk);
    assert_eq!(extracted, file_hash);
    assert_eq!(recomputed, file_hash);
    assert_eq!(extracted, recomputed, "verify must reproduce the embedded hash");
}