alef 0.110.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
use crate::codegen::conversions::ConversionConfig;
use crate::core::ir::{FieldDef, TypeRef};

use super::field_fragments::sanitized_vec_field_to_core_expr;
use super::{
    apply_field_newtype_from_core, apply_field_newtype_to_core, core_container_type,
    explicit_newtype_replaces_base_conversion, explicit_newtype_uses_wasm_jsvalue, field_references_excluded_type,
    is_tuple_variant,
};

/// Emit a named-field initializer, collapsing `field_name: field_name` to the shorthand
/// `field_name` when the conversion expression is just the field itself (no conversion
/// needed). Avoids `clippy::redundant_field_names` in generated struct/variant literals.
fn field_init(field_name: &str, expr: &str) -> String {
    if expr == field_name {
        field_name.to_string()
    } else {
        format!("{field_name}: {expr}")
    }
}

fn explicit_newtype_to_core_expr(field: &FieldDef, binding: &str, config: &ConversionConfig) -> String {
    use crate::codegen::conversions::field_conversion_to_core_cfg;

    let wrapper = field.newtype_wrapper.as_ref().expect("caller checked wrapper");
    let wasm_jsvalue =
        config.wasm_explicit_newtype_containers_use_jsvalue && explicit_newtype_uses_wasm_jsvalue(wrapper);
    let source = if wasm_jsvalue {
        let binding_ty = core_container_type(&field.ty, config.core_type_paths);
        let deserialize = |value: &str| {
            format!(
                "serde_wasm_bindgen::from_value::<{binding_ty}>({value}).unwrap_or_else(|error| \
                 wasm_bindgen::throw_val(wasm_bindgen::JsValue::from_str(&error.to_string())))"
            )
        };
        if field.optional {
            format!("({binding}).map(|value| {})", deserialize("value"))
        } else {
            deserialize(binding)
        }
    } else if explicit_newtype_replaces_base_conversion(&field.ty, field.optional, wrapper, config) {
        binding.to_string()
    } else {
        let base = field_conversion_to_core_cfg(binding, &field.ty, field.optional, config);
        if let Some(expr) = base.strip_prefix(&format!("{binding}: ")) {
            expr.replace(&format!("val.{binding}"), binding)
        } else {
            base
        }
    };
    apply_field_newtype_to_core(&source, &field.ty, field.optional, wrapper)
}

fn explicit_newtype_from_core_expr(field: &FieldDef, binding: &str, config: &ConversionConfig) -> String {
    use crate::codegen::conversions::field_conversion_from_core_cfg;
    use ahash::AHashSet;

    let wrapper = field.newtype_wrapper.as_ref().expect("caller checked wrapper");
    let wasm_jsvalue =
        config.wasm_explicit_newtype_containers_use_jsvalue && explicit_newtype_uses_wasm_jsvalue(wrapper);
    let converted = apply_field_newtype_from_core(binding, &field.ty, field.optional, wrapper);
    if wasm_jsvalue {
        return if field.optional {
            format!(
                "({converted}).map(|value| serde_wasm_bindgen::to_value(&value).unwrap_or_else(|error| \
                 wasm_bindgen::throw_val(wasm_bindgen::JsValue::from_str(&error.to_string()))))"
            )
        } else {
            format!(
                "serde_wasm_bindgen::to_value(&{converted}).unwrap_or_else(|error| \
                 wasm_bindgen::throw_val(wasm_bindgen::JsValue::from_str(&error.to_string())))"
            )
        };
    }
    if explicit_newtype_replaces_base_conversion(&field.ty, field.optional, wrapper, config) {
        return converted;
    }
    let base = field_conversion_from_core_cfg(
        binding,
        &field.ty,
        field.optional,
        field.sanitized,
        &AHashSet::new(),
        config,
    );
    base.strip_prefix(&format!("{binding}: "))
        .unwrap_or(&base)
        .replace(&format!("val.{binding}"), &converted)
}

/// Wrap a sanitized field's JSON deserialize so a value that fails to parse emits a
/// `tracing::warn!` before falling back to `Default::default()`, instead of swallowing the
/// parse error with no diagnostic. Only reachable for `binding_enums_have_data: true` backends
/// (rustler, magnus), where the field's JSON text comes from a host-language caller. Stays
/// infallible on purpose: this expression sits inside `impl From<Binding> for Core`, and every
/// backend call site uses `.into()`, so returning `Result` here would require a coordinated
/// `TryFrom` migration across backends outside this module's territory. ~keep
fn sanitized_field_parse_or_warn(access: &str, variant_name: &str, field_name: &str) -> String {
    let context = format!("variant = \"{variant_name}\", field = \"{field_name}\"");
    crate::codegen::template_env::render(
        "conversions/sanitized_json_parse_or_warn",
        crate::alef_context! {
            access => access,
            context => context,
            message => "binding provided unparseable JSON for enum variant field; substituting default",
        },
    )
    .trim_end()
    .to_string()
}

/// Generate a match arm for binding -> core direction.
/// Binding enums are always unit-variant-only; core enums may have data variants.
/// `binding_has_data` controls whether the binding enum has the variant's fields (true) or is
/// unit-only (false, e.g. Rustler/Elixir).
/// `binding_uses_tuple_form` records the binding-side variant body shape for tuple variants,
/// so the destructure pattern matches the declaration emitted by the backend template.
/// Generate match arm for binding->core conversion with config (handles type conversions).
pub fn binding_to_core_match_arm_ext_cfg(
    binding_prefix: &str,
    variant_name: &str,
    fields: &[FieldDef],
    binding_has_data: bool,
    config: &ConversionConfig,
    binding_uses_tuple_form: bool,
) -> String {
    use crate::codegen::conversions::field_conversion_to_core_cfg;

    if fields.is_empty() {
        format!("{binding_prefix}::{variant_name} => Self::{variant_name},")
    } else if !binding_has_data {
        if is_tuple_variant(fields) {
            let defaults: Vec<&str> = fields.iter().map(|_| "Default::default()").collect();
            format!(
                "{binding_prefix}::{variant_name} => Self::{variant_name}({}),",
                defaults.join(", ")
            )
        } else {
            let defaults: Vec<String> = fields
                .iter()
                .map(|f| format!("{}: Default::default()", f.name))
                .collect();
            format!(
                "{binding_prefix}::{variant_name} => Self::{variant_name} {{ {} }},",
                defaults.join(", ")
            )
        }
    } else if is_tuple_variant(fields) {
        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
        let binding_pattern = field_names.join(", ");
        let core_args: Vec<String> = fields
            .iter()
            .map(|f| {
                let name = &f.name;
                if f.sanitized {
                    let expr = if let TypeRef::Vec(_) = &f.ty {
                        sanitized_vec_field_to_core_expr(name, &f.ty)
                    } else {
                        sanitized_field_parse_or_warn(name, variant_name, name)
                    };
                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
                }
                if !config.exclude_types.is_empty() && field_references_excluded_type(&f.ty, config.exclude_types) {
                    let expr = sanitized_field_parse_or_warn(name, variant_name, name);
                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
                }
                if f.newtype_wrapper.as_deref().is_some_and(super::is_explicit_newtype) {
                    let expr = explicit_newtype_to_core_expr(f, name, config);
                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
                }
                let conv = field_conversion_to_core_cfg(name, &f.ty, f.optional, config);
                let expr = if let Some(expr) = conv.strip_prefix(&format!("{name}: ")) {
                    let expr = expr.replace(&format!("val.{name}"), name);
                    expr.to_string()
                } else {
                    conv
                };
                let expr = if config.binding_enum_fields_are_boxed && f.is_boxed && matches!(&f.ty, TypeRef::Named(_)) {
                    if f.optional {
                        expr.replace(
                            &format!("{name}.map(Into::into)"),
                            &format!("{name}.map(|v| (*v).into())"),
                        )
                    } else {
                        expr.replace(&format!("{name}.into()"), &format!("(*{name}).into()"))
                    }
                } else {
                    expr
                };
                let expr = f
                    .newtype_wrapper
                    .as_ref()
                    .map(|wrapper| apply_field_newtype_to_core(&expr, &f.ty, f.optional, wrapper))
                    .unwrap_or(expr);
                if f.is_boxed { format!("Box::new({expr})") } else { expr }
            })
            .collect();
        let pattern_syntax = if binding_uses_tuple_form {
            format!("{binding_prefix}::{variant_name}({binding_pattern})")
        } else {
            format!("{binding_prefix}::{variant_name} {{ {binding_pattern} }}")
        };
        format!("{pattern_syntax} => Self::{variant_name}({}),", core_args.join(", "))
    } else {
        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
        let pattern = field_names.join(", ");
        let core_fields: Vec<String> = fields
            .iter()
            .map(|f| {
                if f.sanitized {
                    if let TypeRef::Vec(_) = &f.ty {
                        let expr = sanitized_vec_field_to_core_expr(&f.name, &f.ty);
                        return format!("{}: {expr}", f.name);
                    }
                    let expr = sanitized_field_parse_or_warn(&f.name, variant_name, &f.name);
                    return format!("{}: {expr}", f.name);
                }
                if f.newtype_wrapper.as_deref().is_some_and(super::is_explicit_newtype) {
                    let expr = explicit_newtype_to_core_expr(f, &f.name, config);
                    return field_init(&f.name, &expr);
                }
                let conv = field_conversion_to_core_cfg(&f.name, &f.ty, f.optional, config);
                let expr = if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
                    expr.replace(&format!("val.{}", f.name), &f.name)
                } else {
                    conv.strip_prefix(&format!("{}: ", f.name)).unwrap_or(&conv).to_string()
                };
                let binding_field_is_boxed =
                    config.binding_enum_fields_are_boxed && f.is_boxed && matches!(&f.ty, TypeRef::Named(_));
                let expr = if binding_field_is_boxed {
                    // The binding field is already a `Box<T>`: unbox, convert, and rebox.
                    if f.optional {
                        format!("{}.map(|v| Box::new((*v).into()))", f.name)
                    } else {
                        format!("Box::new((*{}).into())", f.name)
                    }
                } else if f.is_boxed {
                    if f.optional {
                        format!("{expr}.map(Box::new)")
                    } else {
                        format!("Box::new({expr})")
                    }
                } else {
                    expr
                };
                let expr = f
                    .newtype_wrapper
                    .as_ref()
                    .map(|wrapper| apply_field_newtype_to_core(&expr, &f.ty, f.optional, wrapper))
                    .unwrap_or(expr);
                field_init(&f.name, &expr)
            })
            .collect();
        format!(
            "{binding_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
            core_fields.join(", ")
        )
    }
}

/// Generate a match arm for core -> binding direction.
/// When the binding also has data variants, destructure and forward fields.
/// When the binding is unit-variant-only, discard core data with `..`.
/// `binding_has_data` controls whether the binding enum has the variant's fields (true) or is
/// unit-only (false).
/// `binding_uses_tuple_form` records the binding-side variant body shape for tuple variants,
/// so the constructor matches the declaration emitted by the backend template.
/// Generate match arm for core->binding conversion with config (handles type conversions).
pub fn core_to_binding_match_arm_ext_cfg(
    core_prefix: &str,
    variant_name: &str,
    fields: &[FieldDef],
    binding_has_data: bool,
    config: &ConversionConfig,
    binding_uses_tuple_form: bool,
) -> String {
    use crate::codegen::conversions::field_conversion_from_core_cfg;
    use ahash::AHashSet;

    if fields.is_empty() {
        format!("{core_prefix}::{variant_name} => Self::{variant_name},")
    } else if !binding_has_data {
        if is_tuple_variant(fields) {
            format!("{core_prefix}::{variant_name}(..) => Self::{variant_name},")
        } else {
            format!("{core_prefix}::{variant_name} {{ .. }} => Self::{variant_name},")
        }
    } else if is_tuple_variant(fields) {
        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
        let core_pattern = field_names.join(", ");
        let binding_fields: Vec<String> = fields
            .iter()
            .map(|f| {
                if f.newtype_wrapper.as_deref().is_some_and(super::is_explicit_newtype) {
                    let expr = explicit_newtype_from_core_expr(f, &f.name, config);
                    return if binding_uses_tuple_form {
                        expr
                    } else {
                        field_init(&f.name, &expr)
                    };
                }
                let conv =
                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
                    let source = f
                        .newtype_wrapper
                        .as_ref()
                        .map(|wrapper| apply_field_newtype_from_core(&f.name, &f.ty, f.optional, wrapper))
                        .unwrap_or_else(|| f.name.clone());
                    let mut expr = expr.replace(&format!("val.{}", f.name), &source);
                    if f.is_boxed {
                        if config.binding_enum_fields_are_boxed {
                            if f.optional {
                                expr = expr.replace(
                                    &format!("{}.map(Into::into)", f.name),
                                    &format!("{}.map(|v| Box::new((*v).into()))", f.name),
                                );
                            } else {
                                expr = expr.replace(
                                    &format!("{}.into()", f.name),
                                    &format!("Box::new((*{}).into())", f.name),
                                );
                            }
                        } else {
                            expr = expr.replace(&format!("{}.into()", f.name), &format!("(*{}).into()", f.name));
                        }
                    }
                    if binding_uses_tuple_form {
                        let string_move = format!("{}.to_string()", f.name);
                        if expr == string_move {
                            expr = f.name.clone();
                        }
                        expr
                    } else {
                        field_init(&f.name, &expr)
                    }
                } else {
                    conv
                }
            })
            .collect();
        if binding_uses_tuple_form {
            format!(
                "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name}({}),",
                binding_fields.join(", ")
            )
        } else {
            format!(
                "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name} {{ {} }},",
                binding_fields.join(", ")
            )
        }
    } else {
        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
        let pattern = field_names.join(", ");
        let binding_fields: Vec<String> = fields
            .iter()
            .map(|f| {
                if f.newtype_wrapper.as_deref().is_some_and(super::is_explicit_newtype) {
                    let expr = explicit_newtype_from_core_expr(f, &f.name, config);
                    return field_init(&f.name, &expr);
                }
                let conv =
                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
                    let source = f
                        .newtype_wrapper
                        .as_ref()
                        .map(|wrapper| apply_field_newtype_from_core(&f.name, &f.ty, f.optional, wrapper))
                        .unwrap_or_else(|| f.name.clone());
                    let mut expr = expr.replace(&format!("val.{}", f.name), &source);
                    if f.is_boxed {
                        if config.binding_enum_fields_are_boxed {
                            if f.optional {
                                expr = expr.replace(
                                    &format!("{}.map(Into::into)", f.name),
                                    &format!("{}.map(|v| Box::new((*v).into()))", f.name),
                                );
                            } else {
                                expr = expr.replace(
                                    &format!("{}.into()", f.name),
                                    &format!("Box::new((*{}).into())", f.name),
                                );
                            }
                        } else {
                            expr = expr.replace(&format!("{}.into()", f.name), &format!("(*{}).into()", f.name));
                        }
                    }
                    field_init(&f.name, &expr)
                } else {
                    conv
                }
            })
            .collect();
        format!(
            "{core_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
            binding_fields.join(", ")
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::ir::PrimitiveType;

    /// Regression coverage for task #511: struct-form named field initializers used to emit
    /// `field: field` unconditionally, tripping `clippy::redundant_field_names` on every
    /// no-op field in generated enum conversions. Table-driven over the collapse rule itself.
    ///
    /// Task #548 re-verified this same helper against every non-collapsible shape a naive
    /// substring/prefix match could get wrong: a method call (`.clone()`), a function call
    /// wrapping the identifier, a deref, a differently-named binding, and a raw identifier field
    /// paired with either its own bare form (must collapse -- `Foo { r#type }` is valid
    /// shorthand) or a differing expression (must not). All pass unchanged because `field_init`
    /// only ever compares `expr` to `field_name` by exact string equality, never a substring or
    /// prefix match. ~keep
    #[test]
    fn field_init_collapses_only_when_expr_is_exactly_the_field_name() {
        let cases: &[(&str, &str, &str)] = &[
            // No conversion needed: expr is exactly the field name -> collapses to shorthand.
            ("url", "url", "url"),
            // Conversion needed: expr differs (a real transform) -> must NOT collapse. This is
            // the more important half: a false-positive collapse here would drop data silently.
            ("url", "val.url.to_string()", "url: val.url.to_string()"),
            // Target name differs from source name: expr names a different identifier, not a
            // conversion of the same field -> must NOT collapse (exact-equality boundary, not
            // a prefix/substring match).
            ("id", "identifier", "id: identifier"),
            // Method call on the field's own binding -> not a bare identifier -> must NOT
            // collapse, even though `bar` is a prefix of `bar.clone()`.
            ("bar", "bar.clone()", "bar: bar.clone()"),
            // Function call wrapping the identifier -> must NOT collapse.
            ("bar", "into_x(bar)", "bar: into_x(bar)"),
            // Deref of the identifier -> must NOT collapse.
            ("bar", "*bar", "bar: *bar"),
            // Raw identifier field with an identical raw-identifier expr -> collapses. `Foo {
            // r#type }` is valid shorthand syntax, so this is a real no-op field like any other.
            ("r#type", "r#type", "r#type"),
            // Raw identifier field whose expr is NOT the same raw identifier -> must NOT
            // collapse.
            ("r#type", "r#type.to_string()", "r#type: r#type.to_string()"),
        ];
        for (field_name, expr, expected) in cases {
            assert_eq!(
                field_init(field_name, expr),
                *expected,
                "field_init({field_name:?}, {expr:?}) mismatch"
            );
        }
    }

    fn string_field(name: &str) -> FieldDef {
        FieldDef {
            name: name.to_string(),
            ty: TypeRef::String,
            ..Default::default()
        }
    }

    /// End-to-end reproduction of the reported defect shape (`Self::Link { url: url, title:
    /// title }`) at the public match-arm generator: one field that needs no conversion must
    /// collapse to shorthand, one field with a real conversion must keep its `name: expr` form.
    #[test]
    fn core_to_binding_match_arm_collapses_no_op_fields_only() {
        let fields = vec![
            FieldDef {
                name: "id".to_string(),
                ty: TypeRef::Primitive(PrimitiveType::I32),
                ..Default::default()
            },
            string_field("url"),
        ];
        let config = ConversionConfig::default();

        let arm = core_to_binding_match_arm_ext_cfg("Core", "Link", &fields, true, &config, false);

        assert_eq!(
            arm, "Core::Link { id, url } => Self::Link { id, url: url.to_string() },",
            "no-op field `id` must collapse to shorthand while converted field `url` keeps its \
             `name: expr` form, got:\n{arm}"
        );
    }

    /// Same reproduction for the binding->core direction, where a bare `String` field needs no
    /// conversion at all: both fields must collapse (this is the exact `url: url, title: title`
    /// shape from the reported clippy findings).
    #[test]
    fn binding_to_core_match_arm_collapses_all_no_op_fields() {
        let fields = vec![string_field("url"), string_field("title")];
        let config = ConversionConfig::default();

        let arm = binding_to_core_match_arm_ext_cfg("Binding", "Link", &fields, true, &config, false);

        assert_eq!(
            arm, "Binding::Link { url, title } => Self::Link { url, title },",
            "both no-op fields must collapse to shorthand, got:\n{arm}"
        );
    }
}