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
use super::scan_args_defaults::{
gen_scan_args_prologue_with_defaults, last_param_is_default_struct, needs_variadic_arity,
};
use super::serde_bindings::{
magnus_ahash_pre_call_bindings, magnus_call_args_with_ahash, magnus_serde_let_bindings, magnus_serde_recoverable,
params_need_fallible_deser,
};
use crate::backends::magnus::type_map::MagnusMapper;
use crate::codegen::generators;
use crate::codegen::shared::function_params;
use crate::codegen::type_mapper::TypeMapper;
use crate::core::ir::{ApiSurface, FunctionDef, TypeRef};
use ahash::AHashSet;
/// Generate a free function binding.
pub(in crate::backends::magnus::gen_bindings) fn gen_function(
func: &FunctionDef,
mapper: &MagnusMapper,
opaque_types: &AHashSet<String>,
mutex_types: &AHashSet<String>,
core_import: &str,
api: &ApiSurface,
) -> String {
let is_default_config_func = last_param_is_default_struct(func, api);
let variadic = needs_variadic_arity(&func.params) || is_default_config_func;
// For non-opaque Named params, accept magnus::Value so a plain Ruby Hash works directly.
// The binding calls to_json internally before serde_json deserialization.
let params = if variadic {
"args: &[magnus::Value]".to_string()
} else {
function_params(&func.params, &|ty| {
if let TypeRef::Named(name) = ty {
if !opaque_types.contains(name.as_str()) {
return "magnus::Value".to_string();
}
}
mapper.map_type(ty)
})
};
let return_type = mapper.map_type(&func.return_type);
// A non-variadic, infallible, sync fn whose params need `?`-based serde deser (e.g.
// `max_sim_score(&MultiVectorEmbedding, &MultiVectorEmbedding) -> f64`) must still return
// Result so the `?` in the deser preamble compiles. Scoped to this genuinely-new case only:
// variadic / error / async functions are already Result and keep their existing codegen path.
let force_result_for_deser = !variadic
&& func.error_type.is_none()
&& !func.is_async
&& params_need_fallible_deser(&func.params, opaque_types);
// Async functions always return Result because Runtime::new() can fail.
// Variadic functions must return Result because scan_args uses ? operator.
let has_error = func.error_type.is_some() || func.is_async || variadic || force_result_for_deser;
let return_annotation = mapper.wrap_return(&return_type, has_error);
let can_delegate = crate::codegen::shared::can_auto_delegate_function(func, opaque_types);
// serde-recovery needs a Result to `?` into. Pass only the original triggers (error/async) plus
// the new force case — NOT `variadic` — so variadic config functions keep their prior codegen.
let serde_recoverable = !can_delegate
&& magnus_serde_recoverable(
func,
opaque_types,
func.error_type.is_some() || func.is_async || force_result_for_deser,
);
// Check if any param is a Vec<Named> that will need `{name}_core` rebinding.
let needs_vec_named_let_binding = func.params.iter().any(|p| match &p.ty {
TypeRef::Vec(inner) => matches!(inner.as_ref(), TypeRef::Named(name) if !opaque_types.contains(name.as_str())),
_ => false,
});
// Generate serde_magnus deserialization preamble for non-opaque Named params.
// Two emission modes:
// - delegate path: rebind {name} to the binding type so the existing call_args gen works.
// - serde-recovery path: emit `{name}_core: core::Type` so gen_call_args_with_let_bindings
// can pass `&{name}_core` to the core function.
let mut deser_lines = Vec::new();
// When a Vec<Named> param forces the `_core` call-arg path (gen_call_args_with_let_bindings_*),
// every Named param in the call is referenced as `{name}_core`. The serde let-binding emitter
// names scalar Named params `{name}_core` too, so use it here as well — otherwise the non-serde
// preamble would bind a scalar Named param as `{name}` and the `&{name}_core` call site would
// not resolve.
if serde_recoverable || needs_vec_named_let_binding {
deser_lines.extend(magnus_serde_let_bindings(
&func.params,
opaque_types,
core_import,
mapper,
is_default_config_func,
));
} else {
for (idx, p) in func.params.iter().enumerate() {
let promoted = crate::codegen::shared::is_promoted_optional(&func.params, idx);
if let TypeRef::Named(name) = &p.ty {
if !opaque_types.contains(name.as_str()) {
let binding_ty = &p.name;
if p.optional {
deser_lines.push(crate::backends::magnus::template_env::render(
"function_named_binding.rs.jinja",
minijinja::context! {
mode => "optional",
binding_name => binding_ty,
core_import => core_import,
type_name => name,
is_mut => p.is_mut,
},
));
} else if promoted || (idx == func.params.len() - 1 && is_default_config_func) {
deser_lines.push(crate::backends::magnus::template_env::render(
"function_named_binding.rs.jinja",
minijinja::context! {
mode => "default",
binding_name => binding_ty,
core_import => core_import,
type_name => name,
is_mut => p.is_mut,
},
));
} else {
deser_lines.push(crate::backends::magnus::template_env::render(
"function_named_binding.rs.jinja",
minijinja::context! {
mode => "required",
binding_name => binding_ty,
core_import => core_import,
type_name => name,
is_mut => p.is_mut,
},
));
}
}
} else if let TypeRef::Vec(inner) = &p.ty {
if let TypeRef::Named(name) = inner.as_ref() {
if !opaque_types.contains(name.as_str()) {
let core_inner_ty = format!("{core_import}::{name}");
let vec_ty = format!("Vec<{core_inner_ty}>");
deser_lines.push(crate::backends::magnus::template_env::render(
"function_named_vec_binding.rs.jinja",
minijinja::context! {
name => &p.name,
vec_ty => &vec_ty,
optional => p.optional,
},
));
}
}
}
}
}
// AHashMap<Cow<'static, str>, Value> params: Ruby receives these as
// HashMap<String, String>. Emit pre-call `let __<name>_ahash` bindings so the
// call site can borrow a properly-typed AHashMap.
let ahash_bindings = magnus_ahash_pre_call_bindings(&func.params);
deser_lines.extend(ahash_bindings);
// When variadic, prepend scan_args prologue to unpack individual bindings from args slice.
let scan_args_prologue = if variadic {
format!(
"{}\n ",
gen_scan_args_prologue_with_defaults(&func.params, mapper, opaque_types, is_default_config_func)
)
} else {
String::new()
};
let deser_preamble = if deser_lines.is_empty() {
String::new()
} else {
format!("{}\n ", deser_lines.join("\n "))
};
let body = if can_delegate || serde_recoverable || needs_vec_named_let_binding {
let base_call_args = if serde_recoverable || needs_vec_named_let_binding {
generators::gen_call_args_with_let_bindings_json_str(&func.params, opaque_types)
} else {
generators::gen_call_args(&func.params, opaque_types)
};
let call_args = magnus_call_args_with_ahash(&func.params, opaque_types, &base_call_args);
let core_fn_path = {
let path = func.rust_path.replace('-', "_");
if path.starts_with(core_import) {
path
} else {
format!("{core_import}::{}", func.name)
}
};
let core_call = format!("{core_fn_path}({call_args})");
if func.is_async {
// Async core function: wrap in tokio runtime block_on.
// Runtime::new() can fail, so always use map_err and return Ok(...).
let wrap = generators::wrap_return_with_mutex_mapped(
"result",
&func.return_type,
"",
opaque_types,
mutex_types,
false,
func.returns_ref,
false,
mapper,
);
if func.error_type.is_some() {
crate::backends::magnus::template_env::render(
"function_async_body.rs.jinja",
minijinja::context! {
core_call => &core_call,
wrap => &wrap,
has_error => true,
},
)
} else {
crate::backends::magnus::template_env::render(
"function_async_body.rs.jinja",
minijinja::context! {
core_call => &core_call,
wrap => &wrap,
has_error => false,
},
)
}
} else if func.error_type.is_some() {
let wrap = generators::wrap_return_with_mutex_mapped(
"result",
&func.return_type,
"",
opaque_types,
mutex_types,
false,
func.returns_ref,
false,
mapper,
);
crate::backends::magnus::template_env::render(
"function_result_body.rs.jinja",
minijinja::context! {
core_call => &core_call,
wrap => &wrap,
},
)
} else if variadic || force_result_for_deser {
// Result-returning wrapper with an infallible core call: variadic (scan_args uses ?) or
// force-wrapped because a param needs fallible serde deser. Wrap the plain value in Ok().
let inner = generators::wrap_return_with_mutex_mapped(
&core_call,
&func.return_type,
"",
opaque_types,
mutex_types,
false,
func.returns_ref,
false,
mapper,
);
crate::backends::magnus::template_env::render(
"function_variadic_ok_body.rs.jinja",
minijinja::context! {
inner => &inner,
},
)
} else {
generators::wrap_return_with_mutex_mapped(
&core_call,
&func.return_type,
"",
opaque_types,
mutex_types,
false,
func.returns_ref,
false,
mapper,
)
}
} else {
gen_magnus_unimplemented_body(&func.return_type, &func.name, func.error_type.is_some() || variadic)
};
// Add #[allow(unused_variables)] to functions with unimplemented bodies to suppress warnings for unused params
let allow_attr = if !can_delegate && !serde_recoverable {
"#[allow(unused_variables)]\n"
} else {
""
};
crate::backends::magnus::template_env::render(
"function_wrapper.rs.jinja",
minijinja::context! {
allow_attr => allow_attr,
name => &func.name,
params => ¶ms,
return_annotation => &return_annotation,
scan_args_prologue => &scan_args_prologue,
deser_preamble => &deser_preamble,
body => &body,
},
)
}
/// Generate a type-appropriate unsupported body for Magnus.
pub(in crate::backends::magnus::gen_bindings) fn gen_magnus_unimplemented_body(
return_type: &crate::core::ir::TypeRef,
fn_name: &str,
has_error: bool,
) -> String {
use crate::core::ir::TypeRef;
let err_msg = format!("Not implemented: {fn_name}");
if has_error {
crate::backends::magnus::template_env::render(
"function_unimplemented_error.rs.jinja",
minijinja::context! {
message => &err_msg,
},
)
} else {
match return_type {
TypeRef::Unit => "()".to_string(),
TypeRef::String | TypeRef::Char | TypeRef::Path => crate::backends::magnus::template_env::render(
"function_unimplemented_string.rs.jinja",
minijinja::context! {
name => fn_name,
},
),
TypeRef::Bytes => "Vec::new()".to_string(),
TypeRef::Primitive(p) => match p {
crate::core::ir::PrimitiveType::Bool => "false".to_string(),
_ => "0".to_string(),
},
TypeRef::Optional(_) => "None".to_string(),
TypeRef::Vec(_) => "Vec::new()".to_string(),
TypeRef::Map(_, _) => "Default::default()".to_string(),
TypeRef::Duration => "0u64".to_string(),
TypeRef::Named(_) | TypeRef::Json => crate::backends::magnus::template_env::render(
"function_unimplemented_panic.rs.jinja",
minijinja::context! {
name => fn_name,
},
),
}
}
}