@@ -137,10 +137,10 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
137
137
}
138
138
139
139
pub fn impl_pyclass ( attr : AttributeArgs , item : Item ) -> TokenStream2 {
140
- let struc = if let Item :: Struct ( struc ) = item {
141
- struc
142
- } else {
143
- panic ! ( "#[pyclass] can only be on a struct declaration" ) ;
140
+ let ( ident , attrs ) = match item. clone ( ) {
141
+ Item :: Struct ( struc) => ( struc . ident , struc . attrs ) ,
142
+ Item :: Enum ( enu ) => ( enu . ident , enu . attrs ) ,
143
+ _ => panic ! ( "#[pyclass] can only be on a struct or enum declaration" ) ,
144
144
} ;
145
145
146
146
let rp_path = rustpython_path_attr ( & attr) ;
@@ -159,10 +159,10 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
159
159
}
160
160
}
161
161
}
162
- let class_name = class_name. unwrap_or_else ( || struc . ident . to_string ( ) ) ;
162
+ let class_name = class_name. unwrap_or_else ( || ident. to_string ( ) ) ;
163
163
164
164
let mut doc: Option < Vec < String > > = None ;
165
- for attr in struc . attrs . iter ( ) {
165
+ for attr in attrs. iter ( ) {
166
166
if attr. path . is_ident ( "doc" ) {
167
167
let meta = attr. parse_meta ( ) . expect ( "expected doc attr to be a meta" ) ;
168
168
if let Meta :: NameValue ( name_value) = meta {
@@ -186,11 +186,9 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
186
186
None => quote ! ( None ) ,
187
187
} ;
188
188
189
- let ty = & struc. ident ;
190
-
191
189
quote ! {
192
- #struc
193
- impl #rp_path:: pyobject:: PyClassDef for #ty {
190
+ #item
191
+ impl #rp_path:: pyobject:: PyClassDef for #ident {
194
192
const NAME : & ' static str = #class_name;
195
193
const DOC : Option <& ' static str > = #doc;
196
194
}
0 commit comments