pyro-macro 0.2.1

Derive macros for Pyroduct
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
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{FnArg, ItemFn, Pat, Result, ReturnType, Type, parse_quote};

use super::parse::{ModuleAttrs, OutputSpec};

pub fn expand(attrs: ModuleAttrs, input_fn: ItemFn) -> Result<TokenStream> {
    let fn_name = &input_fn.sig.ident;
    let fn_vis = &input_fn.vis;
    let fn_block = &input_fn.block;
    let fn_attrs = &input_fn.attrs;

    // Extract parameters
    let params: Vec<_> = input_fn
        .sig
        .inputs
        .iter()
        .filter_map(|arg| {
            if let FnArg::Typed(pat_type) = arg
                && let Pat::Ident(pat_ident) = &*pat_type.pat
            {
                let name = pat_ident.ident.clone();
                let ty = (*pat_type.ty).clone();
                let attrs = pat_type.attrs.clone();
                let pat = pat_type.pat.clone();
                return Some((name, ty, attrs, pat));
            }
            None
        })
        .collect();

    // Validate module requirements and extract types
    let return_type = extract_result_ok_type(&input_fn.sig.output)?;

    // Generate __Output struct and mapping based on output spec
    let (output_struct, output_mapping, output_name) =
        generate_output(&attrs.output, &return_type)?;

    // Generate the call arguments (extract from input struct)
    let call_args: Vec<_> = params
        .iter()
        .map(|(name, ty, _, _)| {
            let name_str = name.to_string();
            quote! { input.get_value::<#ty>(#name_str).ok_or_else(|| ::pyroduct::CapturedError::new(format!("Missing {}", #name_str)))? }
        })
        .collect();

    // Generate the original function parameters
    let original_fn_params: Vec<_> = params
        .iter()
        .map(|(_, ty, attrs, pat)| quote! { #(#attrs)* #pat: #ty })
        .collect();

    let expanded = quote! {
        #[unsafe(no_mangle)]
        pub extern "C" fn call_extern(input_ptr: *mut u8) -> *const u8 {
            #output_struct

            let call = |input: ::pyroduct::PyroRow<'_>| {
                #fn_name(#(#call_args),*).map(|result| {
                    #output_mapping
                })
            };

            ::pyroduct::wasm::wasm_row_main::<#output_name, _>(input_ptr, call)
        }

        #(#fn_attrs)*
        #fn_vis fn #fn_name(#(#original_fn_params),*) -> ::pyroduct::wasm::ModuleResult<#return_type>
        #fn_block
    };

    Ok(expanded)
}

fn wrap_in_vec(ty: &Type) -> Type {
    parse_quote!(Vec<#ty>)
}

pub fn expand_session(attrs: ModuleAttrs, input_fn: ItemFn) -> Result<TokenStream> {
    let fn_name = &input_fn.sig.ident;
    let fn_vis = &input_fn.vis;
    let fn_block = &input_fn.block;
    let fn_attrs = &input_fn.attrs;

    // Extract parameters
    let params: Vec<_> = input_fn
        .sig
        .inputs
        .iter()
        .filter_map(|arg| {
            if let FnArg::Typed(pat_type) = arg
                && let Pat::Ident(pat_ident) = &*pat_type.pat
            {
                let name = pat_ident.ident.clone();
                let ty = (*pat_type.ty).clone();
                let attrs = pat_type.attrs.clone();
                let pat = pat_type.pat.clone();
                return Some((name, ty, attrs, pat));
            }
            None
        })
        .collect();

    // Generate __Output struct and mapping based on output spec
    let output_type = extract_session_inner_type(&input_fn.sig.output)?;
    let output_vec = wrap_in_vec(&output_type);
    let output_spec = attrs.output;
    let (output_struct, output_mapping, output_name) = generate_output(&output_spec, &output_type)?;

    // Generate the original function parameters
    let original_fn_params: Vec<_> = params
        .iter()
        .map(|(_, ty, attrs, pat)| quote! { #(#attrs)* #pat: #ty })
        .collect();

    // Validate session module requirements and extract types
    let expanded = match params.len() {
        2 => {
            let input_vec = wrap_in_vec(&params[1].1);
            if !(params[0].0 == "prior" || params[0].0 == "_prior") {
                return Err(syn::Error::new(
                    params[0].0.span(),
                    "If 2 inputs, then the first parameter of session module must be named `prior`",
                ));
            }

            if params[1].1 != output_type {
                return Err(syn::Error::new(
                    params[1].0.span(),
                    "The type of the output must be the same as input and prior",
                ));
            }

            if params[0].1 != input_vec || params[0].1 != output_vec {
                return Err(syn::Error::new(
                    params[0].0.span(),
                    format!("The type of the prior must be type: {:?}", input_vec),
                ));
            }

            quote! {
                #[unsafe(no_mangle)]
                pub extern "C" fn call_session_extern(session_id: u32) -> *const u8 {
                    #output_struct

                    let call = |prior: &[::pyroduct::PyroRow<'_>], input: ::pyroduct::PyroRow<'_>| {
                        let prior = prior.iter().map(|p| p.clone().try_into()).collect::<Result<Vec<#output_type>, _>>().map_err(|e| {
                            ::pyroduct::CapturedError::new("Unable to extract prior data")
                                .with_source(e)
                        })?;
                        let input = input.try_into().map_err(|e| {
                            ::pyroduct::CapturedError::new("Unable to extract input data")
                                .with_source(e)
                        })?;
                        #fn_name(prior, input).map(|result| {
                            match result {
                                ::pyroduct::session::SessionResponse::Continue(result) => {
                                    ::pyroduct::session::SessionResponse::Continue(#output_mapping)
                                }
                                ::pyroduct::session::SessionResponse::End(result) => {
                                    ::pyroduct::session::SessionResponse::End(#output_mapping)
                                }
                                ::pyroduct::session::SessionResponse::Terminate => {
                                    ::pyroduct::session::SessionResponse::Terminate
                                }
                            }
                        })
                    };

                    ::pyroduct::wasm::wasm_row_main_session::<#output_name, _>(session_id, call)
                }

                #(#fn_attrs)*
                #fn_vis fn #fn_name(#(#original_fn_params),*) -> ::pyroduct::wasm::ModuleResult<::pyroduct::session::SessionResponse<#output_type>>
                #fn_block
            }
        }
        3 => {
            if !(params[0].0 == "prior_input" || params[0].0 == "_prior_input") {
                return Err(syn::Error::new(
                    params[0].0.span(),
                    "If 3 inputs, then the first parameter of session module must be named `prior_input`",
                ));
            }
            if !(params[1].0 == "prior_output" || params[1].0 == "_prior_output") {
                return Err(syn::Error::new(
                    params[1].0.span(),
                    "If 3 inputs, then the second parameter of session module must be named `prior_output`",
                ));
            }
            let input_type = &params[2].1;
            let input_vec = wrap_in_vec(&params[2].1);
            if params[0].1 != input_vec {
                return Err(syn::Error::new(
                    params[0].0.span(),
                    format!(
                        "First parameter of session module must have the type: {:?}",
                        input_vec
                    ),
                ));
            }
            if params[1].1 != output_vec {
                return Err(syn::Error::new(
                    params[1].0.span(),
                    format!(
                        "Second parameter of session module must have the type: {:?}",
                        output_type
                    ),
                ));
            }

            quote! {
                #[unsafe(no_mangle)]
                pub extern "C" fn call_session_extern(session_id: u32) -> *const u8 {
                    #output_struct

                    let call = |prior_inputs: &[::pyroduct::PyroRow<'_>], prior_outputs: &[::pyroduct::PyroRow<'_>], input: ::pyroduct::PyroRow<'_>| {
                        let prior_inputs = prior_inputs.iter().map(|p| p.clone().try_into()).collect::<Result<Vec<#input_type>, _>>().map_err(|e| {
                            ::pyroduct::CapturedError::new("Unable to extract prior input data")
                                .with_source(e)
                        })?;
                        let prior_outputs = prior_outputs.iter().map(|p| p.clone().try_into()).collect::<Result<Vec<#output_type>, _>>().map_err(|e| {
                            ::pyroduct::CapturedError::new("Unable to extract prior output data")
                                .with_source(e)
                        })?;
                        let input = input.try_into().map_err(|e| {
                            ::pyroduct::CapturedError::new("Unable to extract input data")
                                .with_source(e)
                        })?;
                        #fn_name(prior_inputs, prior_outputs, input).map(|result| {
                            match result {
                                ::pyroduct::session::SessionResponse::Continue(result) => {
                                    ::pyroduct::session::SessionResponse::Continue(#output_mapping)
                                }
                                ::pyroduct::session::SessionResponse::End(result) => {
                                    ::pyroduct::session::SessionResponse::End(#output_mapping)
                                }
                                ::pyroduct::session::SessionResponse::Terminate => {
                                    ::pyroduct::session::SessionResponse::Terminate
                                }
                            }
                        })
                    };

                    ::pyroduct::wasm::wasm_row_main_session_diff::<#output_name, _>(session_id, call)
                }

                #(#fn_attrs)*
                #fn_vis fn #fn_name(#(#original_fn_params),*) -> ::pyroduct::wasm::ModuleResult<::pyroduct::session::SessionResponse<#output_type>>
                #fn_block
            }
        }
        _ => {
            return Err(syn::Error::new(
                Span::call_site(),
                "Session module functions must have either 3 parameters (prior_input, prior_output, and input), or 2 parameters (prior, and input) with the same type for input and output",
            ));
        }
    };

    Ok(expanded)
}

/// Extract the Ok type from Result<T, E>
fn extract_result_ok_type(ret: &ReturnType) -> Result<Type> {
    match ret {
        ReturnType::Default => Err(syn::Error::new(
            Span::call_site(),
            "Module function must return Result<T>",
        )),
        ReturnType::Type(_, ty) => {
            if let Type::Path(type_path) = &**ty
                && let Some(segment) = type_path.path.segments.last()
                && segment.ident == "Result"
                && let syn::PathArguments::AngleBracketed(args) = &segment.arguments
                && let Some(syn::GenericArgument::Type(ok_ty)) = args.args.first()
            {
                return Ok(ok_ty.clone());
            }
            Err(syn::Error::new(
                Span::call_site(),
                "Module function must return Result<T>",
            ))
        }
    }
}

/// Generate the __Output struct and the mapping expression
fn generate_output(
    spec: &OutputSpec,
    return_type: &Type,
) -> Result<(TokenStream, TokenStream, Type)> {
    match spec {
        // Pattern 1: Single named field
        OutputSpec::SingleField(field_name) => {
            let struct_def = quote! {
                #[derive(::pyroduct::format::ToRow, ::pyroduct::format::Document)]
                struct __Output {
                    #field_name: #return_type,
                }
            };

            let mapping = quote! {
                __Output {
                    #field_name: result,
                }
            };

            let output_name = parse_quote!(__Output);

            Ok((struct_def, mapping, output_name))
        }

        // Pattern 2: Tuple with named fields
        OutputSpec::TupleFields(field_names) => {
            // Extract tuple element types from return_type
            let tuple_types = extract_tuple_types(return_type)?;

            if tuple_types.len() != field_names.len() {
                return Err(syn::Error::new(
                    Span::call_site(),
                    format!(
                        "Output field count ({}) doesn't match tuple element count ({})",
                        field_names.len(),
                        tuple_types.len()
                    ),
                ));
            }

            let field_defs: Vec<_> = field_names
                .iter()
                .zip(tuple_types.iter())
                .map(|(name, ty)| quote! { #name: #ty })
                .collect();

            let struct_def = quote! {
                #[derive(::pyroduct::format::ToRow, ::pyroduct::format::Document)]
                struct __Output {
                    #(#field_defs,)*
                }
            };

            let field_mappings: Vec<_> = field_names
                .iter()
                .enumerate()
                .map(|(i, name)| {
                    let idx = syn::Index::from(i);
                    quote! { #name: result.#idx }
                })
                .collect();

            let mapping = quote! {
                __Output {
                    #(#field_mappings,)*
                }
            };

            Ok((struct_def, mapping, parse_quote!(__Output)))
        }

        // Pattern 3: Existing struct that implements ToRow
        OutputSpec::Struct => Ok((quote! {}, quote! { result }, return_type.clone())),
    }
}

/// Extract element types from a tuple type
fn extract_tuple_types(ty: &Type) -> Result<Vec<&Type>> {
    if let Type::Tuple(tuple) = ty {
        Ok(tuple.elems.iter().collect())
    } else {
        Err(syn::Error::new(
            Span::call_site(),
            "Expected tuple return type for multi-field output",
        ))
    }
}

/// Extract the inner type T from Result<SessionResponse<T>>
fn extract_session_inner_type(ret: &ReturnType) -> Result<Type> {
    match ret {
        ReturnType::Default => Err(syn::Error::new(
            Span::call_site(),
            "Session module function must return Result<T>",
        )),
        ReturnType::Type(_, ty) => {
            if let Type::Path(type_path) = &**ty
                && let Some(segment) = type_path.path.segments.last()
                && segment.ident == "Result"
                && let syn::PathArguments::AngleBracketed(args) = &segment.arguments
                && let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first()
            {
                // inner_ty should be SessionResponse<T>
                if let Type::Path(inner_path) = inner_ty
                    && let Some(seg) = inner_path.path.segments.last()
                    && seg.ident == "SessionResponse"
                    && let syn::PathArguments::AngleBracketed(inner_args) = &seg.arguments
                    && let Some(syn::GenericArgument::Type(output_ty)) = inner_args.args.first()
                {
                    return Ok(output_ty.clone());
                }
            }
            Err(syn::Error::new(
                Span::call_site(),
                "Session module must return Result<SessionResponse<T>>",
            ))
        }
    }
}