nodyn 0.2.2

Easy polymorphism with enums
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use core::option::Option::None; // for analyzer
use std::collections::HashSet;

use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote};
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::{
    Attribute, FnArg, Generics, Ident, Meta, Path, Token, Type, Visibility, WherePredicate,
    punctuated::Punctuated, spanned::Spanned,
};

use crate::vec_wrapper::StandardVecWrapper;
use crate::{MethodImpl, OptionalImpl, TraitImpl, Variant, VecWrapper, keyword};

/// Represents the input for the `nodyn` procedural macro, defining a nodyn enum.
#[derive(Debug, Clone)]
pub(crate) struct NodynEnum {
    /// Attributes applied to the enum (e.g., `#[derive(Debug)]`).
    pub(crate) attrs: Vec<Attribute>,
    /// Visibility of the enum (e.g., `pub`, `pub(crate)`).
    pub(crate) visibility: Visibility,
    /// The identifier of the enum (e.g., `MyEnum`).
    pub(crate) ident: Ident,
    /// Generic parameters of the enum.
    pub(crate) generics: Generics,
    /// Variants of the enum, each holding a type and optional `#[into]` attributes.
    pub(crate) variants: Vec<Variant>,
    /// Method implementations for the enum.
    pub(crate) method_impls: Vec<MethodImpl>,
    /// Trait implementations for the enum.
    pub(crate) trait_impls: Vec<TraitImpl>,
    /// Enabled features (`TryInto`, `is_as`, `introspection`).
    pub(crate) optional_impl: OptionalImpl,
    /// Wrapper structs for collections (e.g., `Vec`-based structs).
    pub(crate) vec_wrappers: Vec<VecWrapper>,
    /// module path to where the macro is invoked, used for vec wrapper macro
    pub(crate) module_path: Option<Path>,
}

impl Parse for NodynEnum {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let (module_path, attrs) = Self::extract_module_path(&attrs);
        let visibility = input.parse::<Visibility>()?;
        let _ = input.parse::<syn::token::Enum>()?;
        let ident = input.parse::<Ident>()?;
        let generics = input.parse::<Generics>()?;

        let content;
        syn::braced!(content in input);
        let variants = Punctuated::<Variant, Token![,]>::parse_terminated(&content)?
            .into_iter()
            .collect::<Vec<_>>();

        // Ensure unique variant types
        let mut existing_types = HashSet::new();
        for variant in &variants {
            if !existing_types.insert(variant.ty.clone()) {
                return Err(syn::Error::new(
                    variant.ty.span(),
                    "Duplicate variant type detected",
                ));
            }
        }

        let derive_attrs = Self::extract_derive_attrs(&attrs);
        let mut impl_blocks = Vec::new();
        let mut trait_blocks = Vec::new();
        let mut features = OptionalImpl::default();
        let mut collection_structs = Vec::new();

        // Parse additional impl blocks and wrapper structs
        while !input.is_empty() {
            if input.peek(Token![impl]) {
                input.parse::<syn::token::Impl>()?;
                if input.peek(keyword::TryInto)
                    || input.peek(keyword::is_as)
                    || input.peek(keyword::introspection)
                {
                    features.merge(input.parse::<OptionalImpl>()?);
                } else if input.peek(Ident) {
                    trait_blocks.push(input.parse::<TraitImpl>()?);
                } else {
                    impl_blocks.push(input.parse::<MethodImpl>()?);
                }
            } else {
                let attrs = input.call(Attribute::parse_outer)?;
                if input.peek(keyword::vec) {
                    let mut standard_wrapper = input.parse::<StandardVecWrapper>()?;
                    standard_wrapper.attrs = attrs;
                    collection_structs.push(standard_wrapper.into_vec_wrapper(
                        &visibility,
                        &ident,
                        &generics,
                        &derive_attrs,
                    ));
                } else if let Ok(mut wrapper_struct) = input.parse::<VecWrapper>() {
                    let (attrs, vec_field) = VecWrapper::parse_custom_attrs(attrs);
                    if let Some(vec_field) = vec_field {
                        wrapper_struct.definition.attrs = attrs;
                        wrapper_struct.vec_field = vec_field;
                        collection_structs.push(wrapper_struct);
                    } else {
                        return Err(syn::Error::new(
                            wrapper_struct.definition.span(),
                            "Struct is missing #[vec] atrribute",
                        ));
                    }
                } else {
                    return Err(syn::Error::new(
                        input.span(),
                        "Expected 'impl' or struct item",
                    ));
                }
            }
        }

        Ok(Self {
            attrs,
            visibility,
            // enum_token,
            ident,
            generics,
            variants,
            method_impls: impl_blocks,
            trait_impls: trait_blocks,
            optional_impl: features,
            vec_wrappers: collection_structs,
            module_path,
        })
    }
}

impl NodynEnum {
    pub(crate) fn to_token_stream(&self) -> TokenStream {
        let enum_definition = self.enum_definition_tokens();
        let default = self.default_tokens();
        let optional = self.optional_tokens();
        let methods = self.method_tokens();
        let traits = self.trait_tokens();
        let vec_wrappers = self
            .vec_wrappers
            .iter()
            .map(|s| s.to_token_stream(self))
            .collect::<Vec<_>>();

        quote! {
            #enum_definition
            #default
            #optional
            #(#methods)*
            #(#traits)*
            #(#vec_wrappers)*
        }
    }

    /// Extract `nodyn_path` attribute from provide attributes.
    fn extract_module_path(attrs: &[Attribute]) -> (Option<Path>, Vec<Attribute>) {
        (attrs.iter()
            .find(|attr| matches!(&attr.meta, Meta::NameValue(meta) if meta.path.is_ident("module_path")))
            .map(|attr| {
                if let Meta::NameValue(meta) = &attr.meta {
                    syn::parse_str::<Path>(&meta.value.to_token_stream().to_string().replace([' ', '"'],"")).expect("nodyn_path value is no path")
                } else {
                    panic!("attribute is not #[module_path = \"some::module\"]");
                }
            }),

        attrs
            .iter()
            .filter(|attr| !matches!(&attr.meta, Meta::NameValue(meta) if meta.path.is_ident("module_path")))
            .cloned()
            .collect()
            )
    }

    /// Extracts `#[derive]` attributes from the provided attributes.
    fn extract_derive_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
        attrs
            .iter()
            .filter(|attr| matches!(&attr.meta, Meta::List(list) if list.path.is_ident("derive")))
            .cloned()
            .collect()
    }

    /// Generates a `TokenStream` for the generic parameters, combining enum and additional generics.
    pub(crate) fn merged_generics_tokens(&self, other: &Generics) -> TokenStream {
        let params = self
            .generics
            .params
            .iter()
            .chain(other.params.iter())
            .collect::<Vec<_>>();
        if params.is_empty() {
            TokenStream::new()
        } else {
            quote! { <#(#params),*> }
        }
    }

    /// Generates a `TokenStream` for the enum's generic parameters.
    pub(crate) fn generics_tokens(&self) -> TokenStream {
        let generics = self.generics.params.iter().collect::<Vec<_>>();
        if generics.is_empty() {
            TokenStream::new()
        } else {
            quote! { <#(#generics,)*> }
        }
    }

    pub(crate) fn where_and_predicate_tokens(&self, predicate: &WherePredicate) -> TokenStream {
        let mut where_clause = if let Some(clause) = &self.generics.where_clause {
            clause.predicates.iter().collect::<Vec<_>>()
        } else {
            Vec::new()
        };
        where_clause.push(predicate);
        if where_clause.is_empty() {
            TokenStream::new()
        } else {
            quote! { where #(#where_clause,)*  }
        }
    }

    pub(crate) fn merged_where_tokens(&self, generics: &Generics) -> TokenStream {
        // (Option<&'static str>, Vec<&'a WherePredicate>) {
        let predicates = match (&self.generics.where_clause, &generics.where_clause) {
            (Some(w), None) | (None, Some(w)) => w.predicates.iter().collect::<Vec<_>>(),
            (Some(w1), Some(w2)) => w1
                .predicates
                .iter()
                .chain(w2.predicates.iter())
                .collect::<Vec<_>>(),

            (None, None) => return TokenStream::new(),
        };
        quote! {where #(#predicates ,)* }
    }

    pub(crate) fn merged_where_and_predicate_tokens(
        &self,
        generics: &Generics,
        predicate: &WherePredicate,
    ) -> TokenStream {
        let mut predicates = match (&self.generics.where_clause, &generics.where_clause) {
            (Some(w), None) | (None, Some(w)) => w.predicates.iter().collect::<Vec<_>>(),
            (Some(w1), Some(w2)) => w1
                .predicates
                .iter()
                .chain(w2.predicates.iter())
                .collect::<Vec<_>>(),

            (None, None) => Vec::new(),
        };
        predicates.push(predicate);
        quote! {where #(#predicates ,)* }
    }

    fn enum_definition_tokens(&self) -> TokenStream {
        let variants = self.variants.iter().map(Variant::enum_variant_tokens);
        let attrs = &self.attrs;
        let visibility = &self.visibility;
        let ident = &self.ident;
        let generics = &self.generics;

        quote! {
            #(#attrs)*
            #visibility enum #ident #generics {
                #(#variants,)*
            }
        }
    }

    #[allow(clippy::wrong_self_convention)]
    fn from_tokens(&self) -> Vec<TokenStream> {
        let ident = &self.ident;
        let generics = &self.generics;
        self.variants
            .iter()
            .map(|variant| {
                let ty = &variant.ty;
                let variant_ident = &variant.ident;
                quote! {
                    impl #generics ::core::convert::From<#ty> for #ident #generics {
                        fn from(value: #ty) -> Self {
                            #ident::#variant_ident(value)
                        }
                    }
                }
            })
            .collect()
    }

    fn try_from_tokens(&self) -> Vec<TokenStream> {
        let ident = &self.ident;
        let generics = &self.generics;
        self.variants
            .iter()
            .map(|outer| {
                let ty = &outer.ty;
                let arms: Vec<TokenStream> = self
                    .variants
                    .iter()
                    .map(|inner| inner.try_from_arm_tokens(outer, ident))
                    .collect();
                quote! {
                    impl #generics ::core::convert::TryFrom<#ident #generics> for #ty {
                        type Error = &'static str;
                        fn try_from(other: #ident #generics) -> ::core::result::Result< Self, Self::Error >
                        {
                            match other {
                                #(#arms)*
                            }
                        }
                    }
                }
            })
            .collect()
    }

    /// Generate delegation methods for shared methods.
    fn method_tokens(&self) -> Vec<TokenStream> {
        let ident = &self.ident;
        let generics = &self.generics;
        self.method_impls
            .iter()
            .map(|block| {
                let items = &block.items;
                let methods = block
                    .functions
                    .iter()
                    .filter_map(|f| {
                        if let Some(FnArg::Receiver(_)) = f.sig.inputs.first() {
                            let arms = self
                                .variants
                                .iter()
                                .map(|v| v.fn_call_arm_tokens(ident, &f.sig.ident, &f.sig.inputs));
                            let attrs = &f.attrs;
                            let vis = &f.vis;
                            let signature = &f.sig;
                            Some(quote! {
                                #(#attrs)*
                                #vis #signature {
                                    match self {
                                        #(#arms)*
                                    }
                                }
                            })
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>();

                quote! {
                    impl #generics #ident #generics {
                        #(#items)*
                        #(#methods)*
                    }
                }
            })
            .collect()
    }

    fn trait_tokens(&self) -> Vec<TokenStream> {
        let wrapper = &self.ident;
        let lt = &self.generics;
        self.trait_impls
            .iter()
            .map(|b| {
                let trait_path = &b.path;
                let items = &b.block.items;
                let fns = b.block.expand_methods_tokens(self);
                quote! {
                    impl #lt #trait_path for #wrapper #lt {
                         #(#items)*
                         #(#fns)*
                    }
                }
            })
            .collect()
    }

    /// Generates type introspection methods (`count`, `types`, `type_name`).
    fn introspection_tokens(&self) -> TokenStream {
        let ident = &self.ident;
        let generics = &self.generics;
        let visibility = &self.visibility;
        let variant_count = self.variants.len();
        let type_names = self
            .variants
            .iter()
            .map(Variant::type_to_string)
            .collect::<Vec<_>>();
        let arms = self
            .variants
            .iter()
            .map(|v| v.type_as_str_arm_tokens(ident));

        quote! {
            impl #generics #ident #generics {
                /// Returns the number of variants in the enum.
                #visibility const fn count() -> usize {
                    #variant_count
                }

                /// Returns an array of variant type names as `&'static str`.
                #visibility const fn types() -> [&'static str; #variant_count] {
                    [#(#type_names),*]
                }

                /// Returns the type name of the current variant as `&'static str`.
                #visibility const fn type_name(&self) -> &'static str {
                    match self {
                        #(#arms)*
                    }
                }
            }
        }
    }

    /// Generates type checking and conversion methods (`is_`, `try_as_`, etc.).
    ///
    /// Skips `try_as_ref` and `try_as_mut` for reference types to avoid redundant implementations.
    fn is_as_tokens(&self) -> syn::Result<TokenStream> {
        let ident = &self.ident;
        let generics = &self.generics;
        let methods = self
            .variants
            .iter()
            .map(|variant| {
                let ty = &variant.ty;
                let snake = variant.ident_to_snake();
                let type_name = variant.type_to_string();

                let is_fn = format_ident!("is_{}", snake);
                let is_doc = format!("Returns `true` if the variant is `{type_name}`.");
                let is_arms = self
                    .variants
                    .iter()
                    .map(|v| v.is_type_arm_tokens(ident, ty));

                let as_fn = format_ident!("try_as_{}", snake);
                let as_doc = format!("Converts to `Option<{type_name}>` if possible.");
                let as_arms = self
                    .variants
                    .iter()
                    .map(|v| v.as_type_arm_tokens(ident, ty));

                let ref_mut_methods = if matches!(ty, Type::Reference(_)) {
                    quote! {}
                } else {
                    let as_ref_fn = format_ident!("try_as_{}_ref", snake);
                    let as_ref_doc =
                        format!("Returns `Option<&{type_name}>` if the variant is `{type_name}`.");
                    let as_ref_arms = self.variants.iter().map(|v| v.as_ref_arm_tokens(ident, ty));

                    let as_mut_fn = format_ident!("try_as_{}_mut", snake);
                    let as_mut_doc = format!(
                        "Returns `Option<&mut {type_name}>` if the variant is `{type_name}`."
                    );
                    let as_mut_arms = self.variants.iter().map(|v| v.as_mut_arm_tokens(ident, ty));

                    quote! {
                        #[doc = #as_ref_doc]
                        pub fn #as_ref_fn(&self) -> ::core::option::Option<&#ty> {
                            match self {
                                #(#as_ref_arms)*
                                _ => ::core::option::Option::None,
                            }
                        }

                        #[doc = #as_mut_doc]
                        pub fn #as_mut_fn(&mut self) -> ::core::option::Option<&mut #ty> {
                            match self {
                                #(#as_mut_arms)*
                                _ => ::core::option::Option::None,
                            }
                        }
                    }
                };

                Ok(quote! {
                    #[doc = #is_doc]
                    pub fn #is_fn(&self) -> bool {
                        match self {
                            #(#is_arms)*
                            _ => false,
                        }
                    }

                    #[doc = #as_doc]
                    pub fn #as_fn(self) -> ::core::option::Option<#ty> {
                        match self {
                            #(#as_arms)*
                            _ => ::core::option::Option::None,
                        }
                    }

                    #ref_mut_methods
                })
            })
            .collect::<syn::Result<Vec<_>>>()?;

        Ok(quote! {
            impl #generics #ident #generics {
                #(#methods)*
            }
        })
    }

    /// Generates vector accessor methods for a given `Vec` field in a vec wrapper.
    pub(crate) fn variant_vec_tokens(&self, vec_field: &Ident) -> TokenStream {
        let methods = self
            .variants
            .iter()
            .map(|v| v.vec_methods_tokens(&self.ident, vec_field));
        quote! { #(#methods)* }
    }

    /// returns a `TokenStream` that is always included
    fn default_tokens(&self) -> TokenStream {
        let from = self.from_tokens();
        quote! {
            #(#from)*
        }
    }

    fn optional_tokens(&self) -> TokenStream {
        if self.optional_impl.none() {
            // depreciated feature flags only if no features are set
            #[cfg(feature = "try_into")]
            eprintln!(
                "Warning: The `try_into` cargo feature is deprecated. Use `impl TryInto` in the nodyn! macro."
            );
            #[cfg(feature = "introspection")]
            eprintln!(
                "Warning: The `introspection` cargo feature is deprecated. Use `impl introspection` in the nodyn! macro."
            );
            #[cfg(feature = "is_as")]
            eprintln!(
                "Warning: The `is_as` cargo feature is deprecated. Use `impl is_as` in the nodyn! macro."
            );
            #[cfg(feature = "try_into")]
            let try_into = self.try_from_tokens();
            #[cfg(not(feature = "try_into"))]
            let try_into = Vec::<proc_macro2::TokenStream>::new();

            #[cfg(feature = "introspection")]
            let type_fns = self.introspection_tokens();
            #[cfg(not(feature = "introspection"))]
            let type_fns = proc_macro2::TokenStream::new();

            #[cfg(feature = "is_as")]
            let is_as_fn = self.is_as_tokens().unwrap();
            #[cfg(not(feature = "is_as"))]
            let is_as_fn = proc_macro2::TokenStream::new();

            quote! {
                #(#try_into)*
                #type_fns
                #is_as_fn
            }
        } else {
            let try_into = if self.optional_impl.try_into {
                self.try_from_tokens()
            } else {
                Vec::new()
            };
            let type_fns = if self.optional_impl.introspection {
                self.introspection_tokens()
            } else {
                proc_macro2::TokenStream::new()
            };
            let is_as_fn = if self.optional_impl.is_as {
                self.is_as_tokens().unwrap()
            } else {
                proc_macro2::TokenStream::new()
            };
            quote! {
                #(#try_into)*
                #type_fns
                #is_as_fn
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use syn::parse_str;

    #[test]
    fn test_nodyn_enum_parsing() {
        let input = parse_str::<NodynEnum>(
            "
            #[derive(Debug)]
            pub enum MyEnum {
                Number(i32),
                String,
            }
            ",
        )
        .unwrap();

        assert_eq!(input.ident.to_string(), "MyEnum");
        assert_eq!(input.variants.len(), 2);
        assert_eq!(input.variants[0].ident.to_string(), "Number");
        assert_eq!(input.variants[0].type_to_string(), "i32");
        assert_eq!(input.variants[1].ident.to_string(), "String");
        assert_eq!(input.variants[1].type_to_string(), "String");
        assert_eq!(input.method_impls.len(), 0);
        assert_eq!(input.trait_impls.len(), 0);
        assert_eq!(input.vec_wrappers.len(), 0);
        assert_eq!(input.attrs.len(), 1); // #[derive(Debug)]
    }

    #[test]
    fn test_duplicate_variant_types() {
        let result = parse_str::<NodynEnum>(
            "
            pub enum MyEnum {
                Number(i32),
                Another(i32),
            }
            ",
        );
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Duplicate variant type")
        );
    }

    #[test]
    fn test_vec_wrapper_parsing() {
        let input = parse_str::<NodynEnum>(
            "
            pub enum MyEnum {
                Number(i32),
            }
            vec MyEnumVec;
            ",
        )
        .unwrap();

        assert_eq!(input.vec_wrappers.len(), 1);
        assert_eq!(
            input.vec_wrappers[0].definition.ident.to_string(),
            "MyEnumVec"
        );
    }

    #[test]
    fn test_features_parsing() {
        let input = parse_str::<NodynEnum>(
            "
            pub enum MyEnum {
                Number(i32),
            }
            impl TryInto;
            ",
        )
        .unwrap();

        assert!(input.optional_impl.try_into);
        assert!(!input.optional_impl.is_as);
        assert!(!input.optional_impl.introspection);
    }
}