@@ -3,7 +3,7 @@ use std::ops::Not;
3
3
use darling:: { util:: IdentString , FromAttributes , Result } ;
4
4
use proc_macro2:: TokenStream ;
5
5
use quote:: quote;
6
- use syn:: ItemEnum ;
6
+ use syn:: { Generics , ItemEnum } ;
7
7
8
8
use crate :: {
9
9
attrs:: container:: NestedContainerAttributes ,
@@ -46,6 +46,7 @@ impl Container {
46
46
} ;
47
47
48
48
Ok ( Self :: Enum ( Enum {
49
+ generics : item_enum. generics ,
49
50
variants : versioned_variants,
50
51
common,
51
52
} ) )
@@ -82,6 +83,7 @@ impl Container {
82
83
} ;
83
84
84
85
Ok ( Self :: Enum ( Enum {
86
+ generics : item_enum. generics ,
85
87
variants : versioned_variants,
86
88
common,
87
89
} ) )
@@ -96,12 +98,16 @@ pub(crate) struct Enum {
96
98
97
99
/// Common container data which is shared between enums and structs.
98
100
pub ( crate ) common : CommonContainerData ,
101
+
102
+ /// Generic types of the enum
103
+ pub generics : Generics ,
99
104
}
100
105
101
106
// Common token generation
102
107
impl Enum {
103
108
/// Generates code for the enum definition.
104
109
pub ( crate ) fn generate_definition ( & self , version : & VersionDefinition ) -> TokenStream {
110
+ let ( _, type_generics, where_clause) = self . generics . split_for_impl ( ) ;
105
111
let original_attributes = & self . common . original_attributes ;
106
112
let ident = & self . common . idents . original ;
107
113
let version_docs = & version. docs ;
@@ -114,7 +120,7 @@ impl Enum {
114
120
quote ! {
115
121
#( #[ doc = #version_docs] ) *
116
122
#( #original_attributes) *
117
- pub enum #ident {
123
+ pub enum #ident #type_generics #where_clause {
118
124
#variants
119
125
}
120
126
}
@@ -133,6 +139,12 @@ impl Enum {
133
139
134
140
match next_version {
135
141
Some ( next_version) => {
142
+ // TODO (@Techassi): Support generic types which have been removed in newer versions,
143
+ // but need to exist for older versions How do we represent that? Because the
144
+ // defined struct always represents the latest version. I guess we could generally
145
+ // advise against using generic types, but if you have to, avoid removing it in
146
+ // later versions.
147
+ let ( impl_generics, type_generics, where_clause) = self . generics . split_for_impl ( ) ;
136
148
let enum_ident = & self . common . idents . original ;
137
149
let from_ident = & self . common . idents . from ;
138
150
@@ -158,8 +170,10 @@ impl Enum {
158
170
Some ( quote ! {
159
171
#automatically_derived
160
172
#allow_attribute
161
- impl :: std:: convert:: From <#version_ident:: #enum_ident> for #next_version_ident:: #enum_ident {
162
- fn from( #from_ident: #version_ident:: #enum_ident) -> Self {
173
+ impl #impl_generics :: std:: convert:: From <#version_ident:: #enum_ident #type_generics> for #next_version_ident:: #enum_ident #type_generics
174
+ #where_clause
175
+ {
176
+ fn from( #from_ident: #version_ident:: #enum_ident #type_generics) -> Self {
163
177
match #from_ident {
164
178
#variants
165
179
}
0 commit comments