alef 0.104.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
use super::*;
use crate::core::backend::Backend;
use crate::core::config::new_config::NewAlefConfig;
use crate::core::ir::*;
use tracing_test::traced_test;

fn resolved_one(toml: &str) -> ResolvedCrateConfig {
    let cfg: NewAlefConfig = toml::from_str(toml).unwrap();
    cfg.resolve().unwrap().remove(0)
}

fn make_config() -> ResolvedCrateConfig {
    resolved_one(
        r#"
[workspace]
languages = ["ruby"]

[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]

[crates.ruby]
gem_name = "test_lib"
"#,
    )
}

fn make_config_type_def() -> TypeDef {
    TypeDef {
        name: "Config".to_string(),
        rust_path: "test_lib::Config".to_string(),
        original_rust_path: String::new(),
        fields: vec![FieldDef {
            version: Default::default(),
            name: "timeout".to_string(),
            ty: TypeRef::Primitive(PrimitiveType::U32),
            optional: false,
            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,
            serde_with: None,
            serde_skip_serializing_if: false,
            serde_skip: false,
            sensitive: false,
            binding_excluded: false,
            binding_exclusion_reason: None,
            original_type: None,
        }],
        methods: vec![],
        is_opaque: false,
        is_clone: true,
        is_copy: false,
        is_trait: false,
        has_default: false,
        has_stripped_cfg_fields: false,
        is_return_type: false,
        serde_rename_all: None,
        has_serde: false,
        serde_container_default: false,
        serde_container_conversion: Default::default(),
        super_traits: vec![],
        doc: String::new(),
        cfg: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_variant_wrapper: false,
        has_lifetime_params: false,
        has_private_fields: false,
        version: Default::default(),
    }
}

fn make_process_function_def() -> FunctionDef {
    FunctionDef {
        name: "process".to_string(),
        rust_path: "test_lib::process".to_string(),
        original_rust_path: String::new(),
        params: vec![],
        return_type: TypeRef::String,
        is_async: false,
        error_type: None,
        doc: String::new(),
        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(),
    }
}

fn make_api_surface() -> ApiSurface {
    ApiSurface {
        unresolved_modules: Vec::new(),
        crate_name: "test_lib".to_string(),
        version: "0.1.0".to_string(),
        types: vec![make_config_type_def()],
        functions: vec![make_process_function_def()],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: ::std::collections::BTreeMap::new(),
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    }
}

#[test]
fn generates_magnus_module_init() {
    let backend = MagnusBackend;
    let config = make_config();
    let api = make_api_surface();
    let files = backend.generate_bindings(&api, &config).unwrap();
    assert_eq!(files.len(), 1);
    let content = &files[0].content;
    assert!(content.contains("#[magnus::init]"), "must emit #[magnus::init]");
}

#[test]
fn generates_struct_with_magnus_wrap() {
    let backend = MagnusBackend;
    let config = make_config();
    let api = make_api_surface();
    let files = backend.generate_bindings(&api, &config).unwrap();
    let content = &files[0].content;
    assert!(
        content.contains("magnus::wrap"),
        "structs must have magnus::wrap attribute"
    );
    assert!(content.contains("struct Config"), "Config struct must be generated");
}

#[test]
fn generate_public_api_emits_gem_files() {
    let backend = MagnusBackend;
    let config = make_config();
    let api = make_api_surface();
    let files = backend.generate_public_api(&api, &config).unwrap();
    assert_eq!(files.len(), 3, "must generate main rb file + native.rb + version file");
    let paths: Vec<String> = files.iter().map(|f| f.path.to_string_lossy().into_owned()).collect();
    assert!(
        paths.iter().any(|p| p.ends_with("test_lib.rb")),
        "must have main gem file"
    );
    assert!(
        paths.iter().any(|p| p.ends_with("native.rb")),
        "must have native.rb file"
    );
    assert!(
        paths.iter().any(|p| p.ends_with("version.rb")),
        "must have version file"
    );
}

#[test]
fn ruby_public_wrapper_keeps_generated_constants_namespaced() {
    let backend = MagnusBackend;
    let config = make_config();
    let api = make_api_surface();
    let files = backend.generate_public_api(&api, &config).unwrap();
    let main_file = files
        .iter()
        .find(|file| file.path.to_string_lossy().ends_with("test_lib.rb"))
        .expect("main Ruby wrapper must exist");

    assert!(
        !main_file.content.contains("Object.const_set"),
        "the generated Ruby wrapper must not export constants globally"
    );
}

#[test]
fn output_path_defaults_to_packages_ruby() {
    let backend = MagnusBackend;
    let config = make_config();
    let api = make_api_surface();
    let files = backend.generate_bindings(&api, &config).unwrap();
    assert!(
        files[0].path.to_string_lossy().contains("lib.rs"),
        "output must be lib.rs"
    );
}

/// A two-variant, fieldless `Status` enum, used to assert that enum types are excluded
/// from the native.rb re-export list.
fn make_status_enum_def() -> EnumDef {
    EnumDef {
        name: "Status".to_string(),
        rust_path: "sample_markdown::Status".to_string(),
        original_rust_path: String::new(),
        variants: vec![
            EnumVariant {
                serde_untagged: false,
                name: "Active".to_string(),
                fields: vec![],
                doc: String::new(),
                is_default: false,
                serde_rename: None,
                binding_excluded: false,
                binding_exclusion_reason: None,
                is_tuple: false,
                sensitive: false,
                originally_had_data_fields: false,
                cfg: None,
                version: Default::default(),
            },
            EnumVariant {
                serde_untagged: false,
                name: "Inactive".to_string(),
                fields: vec![],
                doc: String::new(),
                is_default: false,
                serde_rename: None,
                binding_excluded: false,
                binding_exclusion_reason: None,
                is_tuple: false,
                sensitive: false,
                originally_had_data_fields: false,
                cfg: None,
                version: Default::default(),
            },
        ],
        methods: vec![],
        doc: String::new(),
        serde_content: None,
        serde_tag: None,
        serde_rename_all: None,
        rename_all_fields: None,
        serde_untagged: false,
        is_copy: false,
        has_serde: false,
        has_default: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        cfg: None,
        version: Default::default(),
    }
}

#[test]
fn test_explicit_re_export_list_filters_internal_types() {
    let backend = MagnusBackend;

    let cfg_str = r#"
[workspace]
languages = ["ruby"]

[[crates]]
name = "my_lib"
sources = ["src/lib.rs"]

[crates.ruby]
gem_name = "my_gem"
"#;
    let cfg: NewAlefConfig = toml::from_str(cfg_str).unwrap();
    let config = cfg.resolve().unwrap().remove(0);

    let mut api = make_api_surface();
    api.crate_name = "my_lib".to_string();
    api.enums.push(make_status_enum_def());

    let files = backend.generate_public_api(&api, &config).unwrap();
    let native_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().ends_with("native.rb"))
        .expect("native.rb must exist");

    assert!(
        !native_file.content.contains("const_get(:Status)"),
        "enum types must not be in re-export list"
    );
    assert!(
        native_file.content.contains("const_get(:Config)"),
        "struct types must be in re-export list via const_get"
    );
}

/// A field accessor and a same-named inherent method both mint `fn providers(&self)` in the
/// single `#[magnus::wrap]` impl block, which rustc rejects with `E0592`, and both register
/// `define_method("providers", ...)`. The accessor is emitted first and must win.
#[test]
fn gen_struct_methods_skips_method_wrapper_when_field_accessor_already_emitted() {
    let backend = MagnusBackend;
    let config = make_config();
    let mut api = make_api_surface();
    api.types = vec![TypeDef {
        name: "LlmConfig".to_string(),
        rust_path: "test_lib::LlmConfig".to_string(),
        fields: vec![FieldDef {
            name: "providers".to_string(),
            ty: TypeRef::String,
            optional: true,
            ..Default::default()
        }],
        methods: vec![MethodDef {
            name: "providers".to_string(),
            return_type: TypeRef::String,
            receiver: Some(ReceiverKind::Ref),
            cfg: None,
            ..Default::default()
        }],
        ..Default::default()
    }];

    let files = backend.generate_bindings(&api, &config).unwrap();
    let content = &files[0].content;

    let definitions = content.matches("fn providers(&self)").count();
    assert_eq!(
        definitions, 1,
        "`providers` must be defined exactly once, found {definitions} in:\n{content}"
    );
    let registrations = content.matches(r#"define_method("providers""#).count();
    assert_eq!(
        registrations, 1,
        "`providers` must be registered exactly once, found {registrations} in:\n{content}"
    );
}

/// End-to-end reproduction of the liter-llm incident through the real `generate_bindings` entry
/// point: a `#[cfg(feature = "tokenizer")]`-gated function reaches the generated `lib.rs`, but the
/// scaffolded `native/Cargo.toml` on disk (deliberately stale here, as `alef scaffold` output
/// legitimately goes once a core crate gains a cfg-gated item after the last scaffold run) never
/// declares `tokenizer`. `alef build` must surface that with a warning instead of staying silent.
#[traced_test]
#[test]
fn generate_bindings_warns_when_the_scaffolded_manifest_is_missing_a_referenced_feature() {
    let backend = MagnusBackend;
    let mut config = make_config();
    let workspace = tempfile::tempdir().expect("tempdir");
    config.workspace_root = Some(workspace.path().to_path_buf());

    // Same formula `MagnusBackend::generate_bindings` itself reads the manifest back from, so
    // this test does not depend on guessing the scaffold's directory layout.
    let manifest_path = workspace
        .path()
        .join(crate::scaffold::ruby_native_manifest_path(&config));
    std::fs::create_dir_all(manifest_path.parent().expect("manifest has a parent dir"))
        .expect("create native crate dir");
    std::fs::write(
        &manifest_path,
        "[package]\nname = \"test_lib_rb\"\n\n[features]\ndefault = []\n",
    )
    .expect("write stale manifest");

    let mut api = make_api_surface();
    api.functions.push(FunctionDef {
        name: "count_tokens".to_string(),
        rust_path: "test_lib::count_tokens".to_string(),
        cfg: Some(r#"feature = "tokenizer""#.to_string()),
        ..Default::default()
    });

    let _ = backend.generate_bindings(&api, &config).unwrap();

    assert!(
        logs_contain("does not enable by default"),
        "a stale scaffolded manifest missing a referenced feature must warn during `alef build`"
    );
}

/// The same manifest, but declaring `tokenizer` -- the normal, up-to-date case -- must stay
/// silent.
#[traced_test]
#[test]
fn generate_bindings_stays_silent_when_the_scaffolded_manifest_declares_the_feature() {
    let backend = MagnusBackend;
    let mut config = make_config();
    let workspace = tempfile::tempdir().expect("tempdir");
    config.workspace_root = Some(workspace.path().to_path_buf());

    let manifest_path = workspace
        .path()
        .join(crate::scaffold::ruby_native_manifest_path(&config));
    std::fs::create_dir_all(manifest_path.parent().expect("manifest has a parent dir"))
        .expect("create native crate dir");
    std::fs::write(
        &manifest_path,
        "[package]\nname = \"test_lib_rb\"\n\n[features]\ndefault = [\"tokenizer\"]\ntokenizer = [\"test-lib/tokenizer\"]\n",
    )
    .expect("write up-to-date manifest");

    let mut api = make_api_surface();
    api.functions.push(FunctionDef {
        name: "count_tokens".to_string(),
        rust_path: "test_lib::count_tokens".to_string(),
        cfg: Some(r#"feature = "tokenizer""#.to_string()),
        ..Default::default()
    });

    let _ = backend.generate_bindings(&api, &config).unwrap();

    assert!(
        !logs_contain("does not enable by default"),
        "an up-to-date manifest must not warn"
    );
}