11use super :: Diagnostic ;
2- use crate :: util:: { def_to_name, path_eq, strip_prefix, ItemMeta } ;
2+ use crate :: util:: { def_to_name, path_eq, strip_prefix, ItemIdent , ItemMeta } ;
33use proc_macro2:: { Span , TokenStream as TokenStream2 } ;
44use quote:: { quote, quote_spanned, ToTokens } ;
55use std:: collections:: { HashMap , HashSet } ;
66use syn:: {
77 parse_quote, spanned:: Spanned , Attribute , AttributeArgs , Ident , Index , Item , Lit , Meta ,
8- NestedMeta , Signature ,
8+ NestedMeta ,
99} ;
1010
1111fn meta_to_vec ( meta : Meta ) -> Result < Vec < NestedMeta > , Meta > {
@@ -54,7 +54,7 @@ impl Class {
5454 }
5555 }
5656
57- fn extract_method ( sig : & Signature , meta : Meta ) -> Result < ClassItem , Diagnostic > {
57+ fn extract_method ( ident : & Ident , meta : Meta ) -> Result < ClassItem , Diagnostic > {
5858 let nesteds = meta_to_vec ( meta) . map_err ( |meta| {
5959 err_span ! (
6060 meta,
@@ -64,30 +64,34 @@ impl Class {
6464 } ) ?;
6565
6666 let item_meta =
67- ItemMeta :: from_nested_meta ( "pymethod" , sig , & nesteds, ItemMeta :: ATTRIBUTE_NAMES ) ?;
67+ ItemMeta :: from_nested_meta ( "pymethod" , & ident , & nesteds, ItemMeta :: ATTRIBUTE_NAMES ) ?;
6868 Ok ( ClassItem :: Method {
69- item_ident : sig . ident . clone ( ) ,
69+ item_ident : ident. clone ( ) ,
7070 py_name : item_meta. method_name ( ) ?,
7171 } )
7272 }
7373
74- fn extract_classmethod ( sig : & Signature , meta : Meta ) -> Result < ClassItem , Diagnostic > {
74+ fn extract_classmethod ( ident : & Ident , meta : Meta ) -> Result < ClassItem , Diagnostic > {
7575 let nesteds = meta_to_vec ( meta) . map_err ( |meta| {
7676 err_span ! (
7777 meta,
7878 "#[pyclassmethod = \" ...\" ] cannot be a name/value, you probably meant \
7979 #[pyclassmethod(name = \" ...\" )]",
8080 )
8181 } ) ?;
82- let item_meta =
83- ItemMeta :: from_nested_meta ( "pyclassmethod" , sig, & nesteds, ItemMeta :: ATTRIBUTE_NAMES ) ?;
82+ let item_meta = ItemMeta :: from_nested_meta (
83+ "pyclassmethod" ,
84+ & ident,
85+ & nesteds,
86+ ItemMeta :: ATTRIBUTE_NAMES ,
87+ ) ?;
8488 Ok ( ClassItem :: ClassMethod {
85- item_ident : sig . ident . clone ( ) ,
89+ item_ident : ident. clone ( ) ,
8690 py_name : item_meta. method_name ( ) ?,
8791 } )
8892 }
8993
90- fn extract_property ( sig : & Signature , meta : Meta ) -> Result < ClassItem , Diagnostic > {
94+ fn extract_property ( ident : & Ident , meta : Meta ) -> Result < ClassItem , Diagnostic > {
9195 let nesteds = meta_to_vec ( meta) . map_err ( |meta| {
9296 err_span ! (
9397 meta,
@@ -96,26 +100,26 @@ impl Class {
96100 )
97101 } ) ?;
98102 let item_meta =
99- ItemMeta :: from_nested_meta ( "pyproperty" , sig , & nesteds, ItemMeta :: PROPERTY_NAMES ) ?;
103+ ItemMeta :: from_nested_meta ( "pyproperty" , & ident , & nesteds, ItemMeta :: PROPERTY_NAMES ) ?;
100104 Ok ( ClassItem :: Property {
101105 py_name : item_meta. property_name ( ) ?,
102- item_ident : sig . ident . clone ( ) ,
106+ item_ident : ident. clone ( ) ,
103107 setter : item_meta. setter ( ) ?,
104108 } )
105109 }
106110
107- fn extract_slot ( sig : & Signature , meta : Meta ) -> Result < ClassItem , Diagnostic > {
111+ fn extract_slot ( ident : & Ident , meta : Meta ) -> Result < ClassItem , Diagnostic > {
108112 let pyslot_err = "#[pyslot] must be of the form #[pyslot] or #[pyslot(slotname)]" ;
109113 let nesteds = meta_to_vec ( meta) . map_err ( |meta| err_span ! ( meta, "{}" , pyslot_err) ) ?;
110114 if nesteds. len ( ) > 1 {
111115 return Err ( Diagnostic :: spanned_error ( & quote ! ( #( #nesteds) * ) , pyslot_err) ) ;
112116 }
113117 let slot_ident = if nesteds. is_empty ( ) {
114- let ident_str = sig . ident . to_string ( ) ;
118+ let ident_str = ident. to_string ( ) ;
115119 if let Some ( stripped) = strip_prefix ( & ident_str, "tp_" ) {
116- proc_macro2:: Ident :: new ( stripped, sig . ident . span ( ) )
120+ proc_macro2:: Ident :: new ( stripped, ident. span ( ) )
117121 } else {
118- sig . ident . clone ( )
122+ ident. clone ( )
119123 }
120124 } else {
121125 match nesteds. into_iter ( ) . next ( ) . unwrap ( ) {
@@ -128,14 +132,14 @@ impl Class {
128132 } ;
129133 Ok ( ClassItem :: Slot {
130134 slot_ident,
131- item_ident : sig . ident . clone ( ) ,
135+ item_ident : ident. clone ( ) ,
132136 } )
133137 }
134138
135139 fn extract_item_from_syn (
136140 & mut self ,
137141 attrs : & mut Vec < Attribute > ,
138- sig : & Signature ,
142+ ident : & Ident ,
139143 ) -> Result < ( ) , Diagnostic > {
140144 let mut attr_idxs = Vec :: new ( ) ;
141145 for ( i, meta) in attrs
@@ -149,10 +153,10 @@ impl Class {
149153 None => continue ,
150154 } ;
151155 let item = match name. to_string ( ) . as_str ( ) {
152- "pymethod" => Self :: extract_method ( sig , meta) ?,
153- "pyclassmethod" => Self :: extract_classmethod ( sig , meta) ?,
154- "pyproperty" => Self :: extract_property ( sig , meta) ?,
155- "pyslot" => Self :: extract_slot ( sig , meta) ?,
156+ "pymethod" => Self :: extract_method ( ident , meta) ?,
157+ "pyclassmethod" => Self :: extract_classmethod ( ident , meta) ?,
158+ "pyproperty" => Self :: extract_property ( ident , meta) ?,
159+ "pyslot" => Self :: extract_slot ( ident , meta) ?,
156160 _ => {
157161 continue ;
158162 }
@@ -177,20 +181,15 @@ impl Class {
177181 }
178182}
179183
180- struct ItemSig < ' a > {
181- attrs : & ' a mut Vec < Attribute > ,
182- sig : & ' a Signature ,
183- }
184-
185- fn extract_impl_items ( mut items : Vec < ItemSig > ) -> Result < TokenStream2 , Diagnostic > {
184+ fn extract_impl_items ( mut items : Vec < ItemIdent > ) -> Result < TokenStream2 , Diagnostic > {
186185 let mut diagnostics: Vec < Diagnostic > = Vec :: new ( ) ;
187186
188187 let mut class = Class :: default ( ) ;
189188
190189 for item in items. iter_mut ( ) {
191190 push_diag_result ! (
192191 diagnostics,
193- class. extract_item_from_syn( & mut item. attrs, item. sig ) ,
192+ class. extract_item_from_syn( & mut item. attrs, & item. ident ) ,
194193 ) ;
195194 }
196195
@@ -356,7 +355,10 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream2, Diag
356355 . iter_mut ( )
357356 . filter_map ( |item| match item {
358357 syn:: ImplItem :: Method ( syn:: ImplItemMethod { attrs, sig, .. } ) => {
359- Some ( ItemSig { attrs, sig } )
358+ Some ( ItemIdent {
359+ attrs,
360+ ident : & sig. ident ,
361+ } )
360362 }
361363 _ => None ,
362364 } )
@@ -386,7 +388,10 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> Result<TokenStream2, Diag
386388 . iter_mut ( )
387389 . filter_map ( |item| match item {
388390 syn:: TraitItem :: Method ( syn:: TraitItemMethod { attrs, sig, .. } ) => {
389- Some ( ItemSig { attrs, sig } )
391+ Some ( ItemIdent {
392+ attrs,
393+ ident : & sig. ident ,
394+ } )
390395 }
391396 _ => None ,
392397 } )
0 commit comments