alef 0.81.0

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
use super::*;

#[test]
fn test_gen_pyo3_kwargs_constructor() {
    let typ = make_test_type();
    let output = gen_pyo3_kwargs_constructor(&typ, &|tr: &TypeRef| match tr {
        TypeRef::Primitive(p) => format!("{:?}", p),
        TypeRef::String | TypeRef::Char => "str".to_string(),
        _ => "Any".to_string(),
    });

    assert!(output.contains("#[new]"));
    assert!(output.contains("#[pyo3(signature = ("));
    assert!(output.contains("timeout=30"));
    assert!(output.contains("enabled=True"));
    assert!(output.contains("name=\"default\""));
    assert!(output.contains("fn new("));
}

#[test]
fn test_gen_napi_defaults_constructor() {
    let typ = make_test_type();
    let output = gen_napi_defaults_constructor(&typ, &|tr: &TypeRef| match tr {
        TypeRef::Primitive(p) => format!("{:?}", p),
        TypeRef::String | TypeRef::Char => "String".to_string(),
        _ => "Value".to_string(),
    });

    assert!(output.contains("pub fn new(mut env: napi::Env, obj: napi::Object)"));
    assert!(output.contains("timeout"));
    assert!(output.contains("enabled"));
    assert!(output.contains("name"));
}

#[test]
fn test_gen_go_functional_options() {
    let typ = make_test_type();
    let output = gen_go_functional_options(&typ, &|tr: &TypeRef| match tr {
        TypeRef::Primitive(p) => match p {
            PrimitiveType::U64 => "uint64".to_string(),
            PrimitiveType::Bool => "bool".to_string(),
            _ => "interface{}".to_string(),
        },
        TypeRef::String | TypeRef::Char => "string".to_string(),
        _ => "interface{}".to_string(),
    });

    assert!(output.contains("type Config struct {"));
    assert!(output.contains("type ConfigOption func(*Config)"));
    assert!(output.contains("func WithConfigTimeout(val uint64) ConfigOption"));
    assert!(output.contains("func WithConfigEnabled(val bool) ConfigOption"));
    assert!(output.contains("func WithConfigName(val string) ConfigOption"));
    assert!(output.contains("func NewConfig(opts ...ConfigOption) *Config"));
}

#[test]
fn test_gen_java_builder() {
    let typ = make_test_type();
    let output = gen_java_builder(&typ, "dev.test", &|tr: &TypeRef| match tr {
        TypeRef::Primitive(p) => match p {
            PrimitiveType::U64 => "long".to_string(),
            PrimitiveType::Bool => "boolean".to_string(),
            _ => "int".to_string(),
        },
        TypeRef::String | TypeRef::Char => "String".to_string(),
        _ => "Object".to_string(),
    });

    assert!(output.contains("package dev.test;"));
    assert!(output.contains("public class ConfigBuilder"));
    assert!(output.contains("withTimeout"));
    assert!(output.contains("withEnabled"));
    assert!(output.contains("withName"));
    assert!(output.contains("public Config build()"));
}

#[test]
fn test_gen_csharp_record() {
    let typ = make_test_type();
    let output = gen_csharp_record(&typ, "MyNamespace", &|tr: &TypeRef| match tr {
        TypeRef::Primitive(p) => match p {
            PrimitiveType::U64 => "ulong".to_string(),
            PrimitiveType::Bool => "bool".to_string(),
            _ => "int".to_string(),
        },
        TypeRef::String | TypeRef::Char => "string".to_string(),
        _ => "object".to_string(),
    });

    assert!(output.contains("namespace MyNamespace;"));
    assert!(output.contains("public record Config"));
    assert!(output.contains("public ulong Timeout"));
    assert!(output.contains("public bool Enabled"));
    assert!(output.contains("public string Name"));
    assert!(output.contains("init;"));
}

#[test]
fn test_gen_magnus_kwargs_constructor_hash_path_for_many_fields() {
    let mut fields: Vec<FieldDef> = (0..16)
        .map(|i| FieldDef {
            version: Default::default(),
            name: format!("field_{i}"),
            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,
            binding_excluded: false,
            binding_exclusion_reason: None,
            original_type: None,
        })
        .collect();
    fields[0].optional = true;

    let typ = TypeDef {
        name: "BigConfig".to_string(),
        rust_path: "crate::BigConfig".to_string(),
        original_rust_path: String::new(),
        fields,
        methods: vec![],
        is_opaque: false,
        is_clone: true,
        is_copy: false,
        doc: String::new(),
        cfg: None,
        is_trait: false,
        has_default: true,
        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![],
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_variant_wrapper: false,
        has_lifetime_params: false,
        has_private_fields: false,
        version: Default::default(),
    };
    let output = gen_magnus_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        output.contains("Option<magnus::RHash>"),
        "should accept RHash via scan_args"
    );
    assert!(output.contains("ruby.to_symbol("), "should use symbol lookup");
    assert!(
        output.contains("field_0: kwargs.get(ruby.to_symbol(\"field_0\")).and_then(|v|"),
        "optional field should use and_then"
    );
    assert!(
        output.contains("field_0:").then_some(()).is_some(),
        "field_0 should appear in output"
    );
}

/// A field with a real `#[serde(default = "Type::static_fn")]` default (resolved to
/// `PublicFunctionCall` once the fn is confirmed to be a public static method on a mirrored
/// type — see `resolve_public_default_functions`) must NOT be treated as a required Ruby
/// keyword argument, even though its type is `Named`. Covers the "applies" half of the
/// magnus fix: reverting the `!has_callable_default` guard on the required-field branch makes
/// this fail (the field falls back into the `ok_or_else("missing required field")` error path).
#[test]
fn test_gen_magnus_kwargs_constructor_named_function_call_default_is_not_required() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        name: "ssrf".to_string(),
        ty: TypeRef::Named("SsrfPolicy".to_string()),
        typed_default: Some(DefaultValue::PublicFunctionCall(
            "crawlberg::SsrfPolicy::from_env".to_string(),
        )),
        ..Default::default()
    });
    let output = gen_magnus_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        !output.contains("missing required field: ssrf"),
        "a field with a real serde default must not become a required argument; got:\n{output}"
    );
    assert!(
        output.contains("unwrap_or(crawlberg::SsrfPolicy::from_env()"),
        "expected the constructor to call the real default fn; got:\n{output}"
    );
}

/// The `#[serde(default = "...")]` function returns the field's core type; Magnus mirrors
/// `Named` types into its own `#[magnus::wrap]` struct, a distinct Rust type from the core one
/// under the same short name, so the call needs `.into()` to become the type the field holds.
/// Covers the "converts" half of the magnus fix: reverting the `.into()` append (while keeping
/// the required-field guard fix) makes this fail while
/// `test_gen_magnus_kwargs_constructor_named_function_call_default_is_not_required` still passes.
#[test]
fn test_gen_magnus_kwargs_constructor_named_function_call_default_converts_into_wrapper() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        name: "ssrf".to_string(),
        ty: TypeRef::Named("SsrfPolicy".to_string()),
        typed_default: Some(DefaultValue::PublicFunctionCall(
            "crawlberg::SsrfPolicy::from_env".to_string(),
        )),
        ..Default::default()
    });
    let output = gen_magnus_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        output.contains("ssrf: kwargs.get(ruby.to_symbol(\"ssrf\")).and_then(|v| SsrfPolicy::try_convert(v).ok()).unwrap_or(crawlberg::SsrfPolicy::from_env().into()),"),
        "expected the default to be converted into the wrapper type via .into(); got:\n{output}"
    );
}

#[test]
fn test_gen_php_kwargs_constructor_basic() {
    let typ = make_test_type();
    let output = gen_php_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        output.contains("pub fn __construct("),
        "should use PHP constructor name"
    );
    assert!(
        output.contains("timeout: Option<u64>"),
        "timeout param should be Option<u64>"
    );
    assert!(
        output.contains("enabled: Option<bool>"),
        "enabled param should be Option<bool>"
    );
    assert!(
        output.contains("name: Option<String>"),
        "name param should be Option<String>"
    );
    assert!(output.contains("-> Self {"), "should return Self");
    assert!(
        output.contains("timeout: timeout.unwrap_or(30),"),
        "should apply int default for timeout"
    );
    assert!(
        output.contains("enabled: enabled.unwrap_or(true),"),
        "should apply bool default for enabled"
    );
    assert!(
        output.contains("name: name.unwrap_or(\"default\".to_string()),"),
        "should apply string default for name"
    );
}

#[test]
fn test_gen_php_kwargs_constructor_optional_field_passthrough() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "tag".to_string(),
        ty: TypeRef::String,
        optional: true,
        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,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    });
    let output = gen_php_kwargs_constructor(&typ, &simple_type_mapper);
    // Anchored on the whole initializer line: `output.contains("tag,")` also matches
    // `tag: tag,`, so it cannot tell a passthrough from `clippy::redundant_field_names`. The
    // initializer lines are emitted flush left; the formatter indents them later. ~keep
    assert!(
        output.contains("\ntag,\n"),
        "optional field should be passed through as field-init shorthand; got:\n{output}"
    );
    assert!(
        !output.contains("tag: tag"),
        "optional passthrough must not emit a redundant field name; got:\n{output}"
    );
    assert!(!output.contains("tag.unwrap"), "optional field should not call unwrap");
}

#[test]
fn test_gen_php_kwargs_constructor_unwrap_or_default_for_primitive() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "retries".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,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    });
    let output = gen_php_kwargs_constructor(&typ, &simple_type_mapper);
    assert!(
        output.contains("retries: retries.unwrap_or_default(),"),
        "primitive with no default should use unwrap_or_default"
    );
}

#[test]
fn test_gen_rustler_kwargs_constructor_basic() {
    let typ = make_test_type();
    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        output.contains("pub fn new(opts: std::collections::HashMap<String, rustler::Term>)"),
        "should accept HashMap of Terms"
    );
    assert!(output.contains("Self {"), "should construct Self");
    assert!(
        output.contains("timeout: opts.get(\"timeout\").and_then(|t| t.decode().ok()).unwrap_or(30),"),
        "should apply int default for timeout"
    );
    assert!(
        output.contains("enabled: opts.get(\"enabled\").and_then(|t| t.decode().ok()).unwrap_or(true),"),
        "should apply bool default for enabled"
    );
}

#[test]
fn test_gen_rustler_kwargs_constructor_optional_field() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "extra".to_string(),
        ty: TypeRef::String,
        optional: true,
        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,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    });
    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);
    assert!(
        output.contains("extra: opts.get(\"extra\").and_then(|t| t.decode().ok()),"),
        "optional field should decode without unwrap"
    );
}

#[test]
fn test_gen_rustler_kwargs_constructor_skips_binding_excluded_fields() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "internal_cache".to_string(),
        ty: TypeRef::String,
        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,
        binding_excluded: true,
        binding_exclusion_reason: Some("internal implementation detail".to_string()),
        original_type: None,
    });

    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        !output.contains("internal_cache"),
        "binding-excluded fields must not be exposed in Rustler constructors; got:\n{output}"
    );
}

#[test]
fn test_gen_rustler_kwargs_constructor_named_type_uses_unwrap_or_default() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "inner".to_string(),
        ty: TypeRef::Named("InnerConfig".to_string()),
        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,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    });
    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);
    assert!(
        output.contains("inner: opts.get(\"inner\").and_then(|t| t.decode().ok()).unwrap_or_default(),"),
        "Named type with no default should use unwrap_or_default"
    );
}

/// A `String` field's own default must be spelled out; a `String` field with no default falls
/// back to the type's.
///
/// The first half previously asserted the opposite, because the branch decided what kind of
/// default it was looking at by sniffing the *rendered Rust* for a leading quote — and
/// `StringLiteral("default")` renders as `"default".to_string()`, which starts with one. An
/// ordinary `String` field was therefore misread as an enum-variant default and collapsed to
/// `.unwrap_or_default()`, so `Config.new(%{})` produced `""` where the source crate says
/// `"default"`. The old assertion named the mechanism ("quoted default") rather than the
/// contract, which is why it read as correct.
#[test]
fn test_gen_rustler_kwargs_constructor_string_field_keeps_its_literal_default() {
    let mut typ = make_test_type();
    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);
    assert!(
        output.contains("name: opts.get(\"name\").and_then(|t| t.decode().ok()).unwrap_or(\"default\".to_string()),"),
        "a String field's own default must survive into the constructor; got:\n{output}"
    );
    typ.fields.push(FieldDef {
        version: Default::default(),
        name: "label".to_string(),
        ty: TypeRef::String,
        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,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    });
    let output2 = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);
    assert!(
        output2.contains("label: opts.get(\"label\").and_then(|t| t.decode().ok()).unwrap_or_default(),"),
        "String field with no default should use unwrap_or_default"
    );
}

/// The branch's real purpose, pinned so it survives the string-sniff removal. An `EnumVariant`
/// default on a `String`-typed field renders as the variant's *wire* string, which is not a value
/// the binding struct's field can be initialised from here, so it defers to the type's `Default`.
/// Without this test, `defers_to_field_type_default` reads as if only the `Named` arm mattered.
#[test]
fn test_gen_rustler_kwargs_constructor_enum_variant_on_string_field_defers_to_default() {
    let mut typ = make_test_type();
    typ.fields.push(FieldDef {
        name: "mode".to_string(),
        ty: TypeRef::String,
        typed_default: Some(DefaultValue::EnumVariant("Fast".to_string())),
        ..Default::default()
    });

    let output = gen_rustler_kwargs_constructor(&typ, &simple_type_mapper);

    assert!(
        output.contains("mode: opts.get(\"mode\").and_then(|t| t.decode().ok()).unwrap_or_default(),"),
        "an enum-variant default on a String field has no literal here; got:\n{output}"
    );
}