pyro-macro 0.1.0

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
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
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{
    GenericArgument, GenericParam, ItemStruct, Lifetime, LifetimeParam, Path, PathArguments,
    TraitBound, Type, TypeParamBound, TypePath,
};

use crate::format::deep_ref::map_type_to_ref;

/// Generates `impl TryFrom<PyroRow> for Struct` and `impl TryFrom<PyroValue> for Struct`
pub fn from_row(input: &ItemStruct, import_location: &Path) -> syn::Result<TokenStream> {
    // 1. Parse Input

    let struct_name = &input.ident;

    // 3. Prepare Generics
    // We need impl<'a, T: TryFrom<PyroValue<'a>>> ... for Struct<T>
    let mut impl_generics = input.generics.clone();
    let lifetime = Lifetime::new("'a", Span::call_site());

    // Construct the bound: std::convert::TryFrom<#import_location::PyroValue<'a>>
    let bound_tokens = quote! { std::convert::TryFrom<#import_location::PyroValue<'a>> };
    let bound: TypeParamBound = syn::parse2(bound_tokens)?;

    // Add the bound to every Type parameter in the impl generics
    for param in impl_generics.params.iter_mut() {
        if let GenericParam::Type(t) = param {
            t.bounds.push(bound.clone());
        }
    }

    // Insert the lifetime 'a at the beginning of the params list
    impl_generics.params.insert(
        0,
        GenericParam::Lifetime(LifetimeParam::new(lifetime.clone())),
    );

    let (impl_g, _, where_clause) = impl_generics.split_for_impl();
    let (_, ty_g, _) = input.generics.split_for_impl(); // Original generics for the Struct<T>

    // =========================================================================
    // Field Extraction Logic (Owned)
    // =========================================================================
    let mut owned_field_extractions = Vec::with_capacity(input.fields.len());

    for f in &input.fields {
        let name = f
            .ident
            .as_ref()
            .ok_or_else(|| syn::Error::new_spanned(f, "FromRow requires named fields"))?;

        let name_str = name.to_string();
        let ty = &f.ty;
        let missing_err = format!("Missing field: {}", name_str);
        let field_err = format!("Failed to convert field '{}'", name_str);

        let stream = generate_field_try_from_owned(
            name,
            &name_str,
            &missing_err,
            &field_err,
            ty,
            &import_location,
        )?;
        owned_field_extractions.push(stream);
    }

    let expanded = quote! {
        // -----------------------------------------------------------------
        // TryFrom<PyroRow<'a>> for Struct (Owned)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<#import_location::PyroRow<'a>> for #struct_name #ty_g #where_clause {
            type Error = #import_location::PyroRow<'a>;

            fn try_from(row: #import_location::PyroRow<'a>) -> Result<Self, Self::Error> {
                let result = (|| -> Result<Self, &'static str> {
                    Ok(Self {
                        #(#owned_field_extractions,)*
                    })
                })();

                result.map_err(|_| row)
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<&PyroRow<'a>> for Struct (Reference)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<& #import_location::PyroRow<'a>> for #struct_name #ty_g #where_clause {
            type Error = &'static str;

            fn try_from(row: & #import_location::PyroRow<'a>) -> Result<Self, Self::Error> {
                Ok(Self {
                    #(#owned_field_extractions,)*
                })
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<PyroValue<'a>> for Struct (Owned)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<#import_location::PyroValue<'a>> for #struct_name #ty_g #where_clause {
            type Error = #import_location::PyroValue<'a>;

            fn try_from(value: #import_location::PyroValue<'a>) -> Result<Self, Self::Error> {
                match value {
                    #import_location::PyroValue::Group(r) => match <Self as std::convert::TryFrom<#import_location::PyroRow<'a>>>::try_from(r) {
                        Ok(s) => Ok(s),
                        Err(r) => Err(#import_location::PyroValue::Group(r)),
                    },
                    v => Err(v)
                }
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<&PyroValue<'a>> for Struct (Reference)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<& #import_location::PyroValue<'a>> for #struct_name #ty_g #where_clause {
            type Error = &'static str;

            fn try_from(value: & #import_location::PyroValue<'a>) -> Result<Self, Self::Error> {
                match value {
                    #import_location::PyroValue::Group(r) => {
                        <Self as std::convert::TryFrom<& #import_location::PyroRow<'a>>>::try_from(r)
                    }
                    _ => Err("Expected Group")
                }
            }
        }
    };

    Ok(TokenStream::from(expanded))
}

/// Generates `impl TryFrom<PyroRow> for StructRef` and `impl TryFrom<PyroValue> for StructRef`
pub fn ref_from_row(input: &ItemStruct, import_location: &Path) -> syn::Result<TokenStream> {
    // 1. Parse Input
    let struct_name = &input.ident;
    let ref_struct_name = format_ident!("{}Ref", struct_name);

    // 2. Prepare Generics for Impl
    // We need impl<'a, T: DeepRef> ... for StructRef<'a, <T as DeepRef>::Ref<'a>>
    let mut impl_generics = input.generics.clone();
    let lifetime = Lifetime::new("'a", Span::call_site());

    // Add DeepRef bound to all Type params
    let mut deep_ref_bound_path = import_location.clone();
    deep_ref_bound_path
        .segments
        .push(syn::PathSegment::from(format_ident!("DeepRef")));

    for param in impl_generics.params.iter_mut() {
        if let GenericParam::Type(t) = param {
            t.bounds.push(TypeParamBound::Trait(TraitBound {
                paren_token: None,
                modifier: syn::TraitBoundModifier::None,
                lifetimes: None,
                path: deep_ref_bound_path.clone(),
            }));
            // Also ensure T lives as long as 'a if necessary, but usually DeepRef handles this projection
            t.bounds.push(TypeParamBound::Lifetime(lifetime.clone()));
        }
    }

    // Insert 'a into impl params
    impl_generics.params.insert(
        0,
        GenericParam::Lifetime(LifetimeParam::new(lifetime.clone())),
    );

    let (impl_g, _, where_clause) = impl_generics.split_for_impl();

    // 3. Prepare Arguments for the Ref Struct
    // StructRef < 'a, <T as DeepRef>::Ref<'a>, ... >
    let mut ref_struct_args = Vec::new();
    ref_struct_args.push(quote! { #lifetime });

    for param in &input.generics.params {
        match param {
            GenericParam::Type(t) => {
                let ident = &t.ident;
                ref_struct_args
                    .push(quote! { <#ident as #import_location::format::DeepRef>::Ref<#lifetime> });
            }
            GenericParam::Const(c) => {
                let ident = &c.ident;
                ref_struct_args.push(quote! { #ident });
            }
            GenericParam::Lifetime(l) => {
                let ident = &l.lifetime;
                ref_struct_args.push(quote! { #ident });
            }
        }
    }

    // =========================================================================
    // Field Extraction Logic
    // =========================================================================
    let mut ref_field_extractions = Vec::with_capacity(input.fields.len());
    let mut lifetime_used = false;

    for f in &input.fields {
        let name = f
            .ident
            .as_ref()
            .ok_or_else(|| syn::Error::new_spanned(f, "FromRow requires named fields"))?;

        let name_str = name.to_string();
        let ty = &f.ty;

        // IMPORTANT: The mapped_type provided here (via map_type_to_ref) is correct.
        // It correctly maps Option<String> -> Option<&'a str> and Option<i32> -> Option<i32>.
        let (mapped_type, is_primitive) = map_type_to_ref(ty);
        if !is_primitive {
            lifetime_used = true;
        }

        let missing_err = format!("Missing field: {}", name_str);
        let field_err = format!("Failed to convert field '{}'", name_str);

        let stream = generate_field_try_from_ref(
            name,
            &name_str,
            &missing_err,
            &field_err,
            &mapped_type,
            ty,
            &import_location,
        )?;
        ref_field_extractions.push(stream);
    }

    let phantom_init = if !lifetime_used {
        quote! { _phantom: std::marker::PhantomData }
    } else {
        quote! {}
    };

    let expanded = quote! {
        // -----------------------------------------------------------------
        // TryFrom<PyroRow<'a>> for StructRef<'a> (Owned)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<#import_location::PyroRow<'a>> for #ref_struct_name < #(#ref_struct_args),* > #where_clause {
            type Error = #import_location::PyroRow<'a>;

            fn try_from(row: #import_location::PyroRow<'a>) -> Result<Self, Self::Error> {
                let result = (|| -> Result<Self, &'static str> {
                    Ok(Self {
                        #(#ref_field_extractions,)*
                        #phantom_init
                    })
                })();

                result.map_err(|_| row)
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<&PyroRow<'a>> for StructRef<'a> (Reference)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<& #import_location::PyroRow<'a>> for #ref_struct_name < #(#ref_struct_args),* > #where_clause {
            type Error = &'static str;

            fn try_from(row: & #import_location::PyroRow<'a>) -> Result<Self, Self::Error> {
                Ok(Self {
                    #(#ref_field_extractions,)*
                    #phantom_init
                })
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<PyroValue<'a>> for StructRef<'a> (Owned)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<#import_location::PyroValue<'a>> for #ref_struct_name < #(#ref_struct_args),* > #where_clause {
            type Error = #import_location::PyroValue<'a>;

            fn try_from(value: #import_location::PyroValue<'a>) -> Result<Self, Self::Error> {
                match value {
                    #import_location::PyroValue::Group(r) => match <Self as std::convert::TryFrom<#import_location::PyroRow<'a>>>::try_from(r) {
                        Ok(s) => Ok(s),
                        Err(r) => Err(#import_location::PyroValue::Group(r)),
                    },
                    v => Err(v)
                }
            }
        }

        // -----------------------------------------------------------------
        // TryFrom<&PyroValue<'a>> for StructRef<'a> (Reference)
        // -----------------------------------------------------------------
        impl #impl_g std::convert::TryFrom<& #import_location::PyroValue<'a>> for #ref_struct_name < #(#ref_struct_args),* > #where_clause {
            type Error = &'static str;

            fn try_from(value: & #import_location::PyroValue<'a>) -> Result<Self, Self::Error> {
                match value {
                    #import_location::PyroValue::Group(r) => {
                        <Self as std::convert::TryFrom<& #import_location::PyroRow<'a>>>::try_from(r)
                    }
                    _ => Err("Expected Group")
                }
            }
        }
    };

    Ok(TokenStream::from(expanded))
}

// =============================================================================
// Code generation helpers for REF fields
// =============================================================================

fn generate_field_try_from_ref(
    name: &syn::Ident,
    name_str: &str,
    missing_err: &str,
    field_err: &str,
    _mapped_type: &TokenStream, // Unused variable, we calculate inner type logic manually
    original_ty: &Type,
    import_location: &Path,
) -> syn::Result<TokenStream> {
    if is_option(original_ty) {
        // For Option, we need to unwrap the inner type and map it to its ref type
        let inner_ty = get_option_inner(original_ty)
            .ok_or_else(|| syn::Error::new_spanned(original_ty, "Malformed Option type"))?;

        // FIX: Do not blindly project <inner_ty as DeepRef>.
        // Use map_type_to_ref to get the actual target type (e.g. i32, &'a str, or Ref).
        let (inner_mapped, _) = map_type_to_ref(inner_ty);

        Ok(quote! {
            #name: {
                match row.get(#name_str) {
                    Some(#import_location::PyroValue::Null) | None => None,
                    Some(val) => Some(
                        <#inner_mapped as std::convert::TryFrom<#import_location::PyroValue<'a>>>::try_from(val.clone())
                            .map_err(|_| #field_err)?
                    ),
                }
            }
        })
    } else {
        // For non-Option, mapped_type (from argument) is already correct.
        // Recalculating it to be safe and consistent with Option fix above.
        let (mapped_type, _) = map_type_to_ref(original_ty);

        Ok(quote! {
            #name: {
                let val = row.get(#name_str)
                    .ok_or_else(|| #missing_err)?
                    .clone();
                <#mapped_type as std::convert::TryFrom<#import_location::PyroValue<'a>>>::try_from(val)
                    .map_err(|_| #field_err)?
            }
        })
    }
}

// =============================================================================
// Code generation helpers for OWNED fields
// =============================================================================

fn generate_field_try_from_owned(
    name: &syn::Ident,
    name_str: &str,
    missing_err: &str,
    field_err: &str,
    ty: &Type,
    import_location: &Path,
) -> syn::Result<TokenStream> {
    if is_option(ty) {
        let inner_ty = get_option_inner(ty)
            .ok_or_else(|| syn::Error::new_spanned(ty, "Malformed Option type"))?;

        Ok(quote! {
            #name: {
                match row.get(#name_str) {
                    Some(#import_location::PyroValue::Null) | None => None,
                    Some(val) => {
                        let owned: #inner_ty = val.clone().try_into()
                            .map_err(|_| #field_err)?;
                        Some(owned)
                    }
                }
            }
        })
    } else if is_nested_struct(ty) {
        Ok(quote! {
            #name: {
                let val = row.get(#name_str)
                    .ok_or_else(|| #missing_err)?
                    .clone();
                val.try_into()
                    .map_err(|_| #field_err)?
            }
        })
    } else if is_vec_of_struct(ty) {
        let inner_ty =
            get_vec_inner(ty).ok_or_else(|| syn::Error::new_spanned(ty, "Malformed Vec type"))?;
        let fail = format!("Failed to convert element in field '{}'", name_str);
        let unexpected = format!("Expected List for field '{}'", name_str);

        // FIX: map_err must be applied inside the closure, on the Result returned by try_into()
        Ok(quote! {
            #name: {
                match row.get(#name_str)
                    .ok_or_else(|| #missing_err)?
                {
                    #import_location::PyroValue::List(items) => {
                        items.iter()
                            .map(|v| v.clone().try_into().map_err(|_| #fail))
                            .collect::<Result<Vec<#inner_ty>, _>>()?
                    }
                    _ => return Err(#unexpected),
                }
            }
        })
    } else {
        Ok(quote! {
            #name: {
                let val = row.get(#name_str)
                    .ok_or_else(|| #missing_err)?
                    .clone();
                val.try_into()
                    .map_err(|_| #field_err)?
            }
        })
    }
}

// =============================================================================
// Type inspection helpers
// =============================================================================

fn is_option(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(seg) = path.segments.last() {
            return seg.ident == "Option";
        }
    }
    false
}

fn get_option_inner(ty: &Type) -> Option<&Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(seg) = path.segments.last() {
            if let PathArguments::AngleBracketed(args) = &seg.arguments {
                if let Some(GenericArgument::Type(inner)) = args.args.first() {
                    return Some(inner);
                }
            }
        }
    }
    None
}

fn get_vec_inner(ty: &Type) -> Option<&Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(seg) = path.segments.last() {
            if let PathArguments::AngleBracketed(args) = &seg.arguments {
                if let Some(GenericArgument::Type(inner)) = args.args.first() {
                    return Some(inner);
                }
            }
        }
    }
    None
}

fn is_nested_struct(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(seg) = path.segments.last() {
            let ident_str = seg.ident.to_string();
            // Expanded list to be safe, but generic T will return true (good)
            return !matches!(
                ident_str.as_str(),
                "bool"
                    | "i8"
                    | "i16"
                    | "i32"
                    | "i64"
                    | "isize"
                    | "u8"
                    | "u16"
                    | "u32"
                    | "u64"
                    | "usize"
                    | "f16"
                    | "f32"
                    | "f64"
                    | "String"
                    | "Vec"
                    | "Option"
            );
        }
    }
    false
}

fn is_vec_of_struct(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(seg) = path.segments.last() {
            if seg.ident == "Vec" {
                if let PathArguments::AngleBracketed(args) = &seg.arguments {
                    if let Some(GenericArgument::Type(inner)) = args.args.first() {
                        return is_nested_struct(inner);
                    }
                }
            }
        }
    }
    false
}