@@ -15,7 +15,7 @@ fn meta_to_vec(meta: Meta) -> Result<Vec<NestedMeta>, Meta> {
1515
1616#[ derive( Default ) ]
1717struct Module {
18- items : HashSet < ModuleItem > ,
18+ items : HashSet < ( ModuleItem , Vec < Meta > ) > ,
1919}
2020
2121#[ derive( PartialEq , Eq , Hash ) ]
@@ -25,13 +25,13 @@ enum ModuleItem {
2525}
2626
2727impl Module {
28- fn add_item ( & mut self , item : ModuleItem , span : Span ) -> Result < ( ) , Diagnostic > {
28+ fn add_item ( & mut self , item : ( ModuleItem , Vec < Meta > ) , span : Span ) -> Result < ( ) , Diagnostic > {
2929 if self . items . insert ( item) {
3030 Ok ( ( ) )
3131 } else {
3232 Err ( Diagnostic :: span_error (
3333 span,
34- "Duplicate #[py*] attribute on pyimpl " . to_owned ( ) ,
34+ "Duplicate #[py*] attribute on pymodule " . to_owned ( ) ,
3535 ) )
3636 }
3737 }
@@ -70,12 +70,35 @@ impl Module {
7070 } )
7171 }
7272
73+ fn extract_struct_sequence ( ident : & Ident , meta : Meta ) -> Result < ModuleItem , Diagnostic > {
74+ let nesteds = meta_to_vec ( meta) . map_err ( |meta| {
75+ err_span ! (
76+ meta,
77+ "#[pystruct_sequence = \" ...\" ] cannot be a name/value, you probably meant \
78+ #[pystruct_sequence(name = \" ...\" )]",
79+ )
80+ } ) ?;
81+
82+ let item_meta = ItemMeta :: from_nested_meta (
83+ "pystruct_sequence" ,
84+ & ident,
85+ & nesteds,
86+ ItemMeta :: STRUCT_SEQUENCE_NAMES ,
87+ ) ?;
88+ Ok ( ModuleItem :: Class {
89+ item_ident : ident. clone ( ) ,
90+ py_name : item_meta. simple_name ( ) ?,
91+ } )
92+ }
93+
7394 fn extract_item_from_syn (
7495 & mut self ,
7596 attrs : & mut Vec < Attribute > ,
7697 ident : & Ident ,
7798 ) -> Result < ( ) , Diagnostic > {
7899 let mut attr_idxs = Vec :: new ( ) ;
100+ let mut items = Vec :: new ( ) ;
101+ let mut cfgs = Vec :: new ( ) ;
79102 for ( i, meta) in attrs
80103 . iter ( )
81104 . filter_map ( |attr| attr. parse_meta ( ) . ok ( ) )
@@ -86,17 +109,28 @@ impl Module {
86109 Some ( name) => name,
87110 None => continue ,
88111 } ;
89- let item = match name. to_string ( ) . as_str ( ) {
112+ match name. to_string ( ) . as_str ( ) {
90113 "pyfunction" => {
91114 attr_idxs. push ( i) ;
92- Self :: extract_function ( ident, meta) ?
115+ items. push ( ( Self :: extract_function ( ident, meta) ?, meta_span) ) ;
116+ }
117+ "pyclass" => {
118+ items. push ( ( Self :: extract_class ( ident, meta) ?, meta_span) ) ;
119+ }
120+ "pystruct_sequence" => {
121+ items. push ( ( Self :: extract_struct_sequence ( ident, meta) ?, meta_span) ) ;
122+ }
123+ "cfg" => {
124+ cfgs. push ( meta) ;
125+ continue ;
93126 }
94- "pyclass" => Self :: extract_class ( ident, meta) ?,
95127 _ => {
96128 continue ;
97129 }
98130 } ;
99- self . add_item ( item, meta_span) ?;
131+ }
132+ for ( item, meta) in items {
133+ self . add_item ( ( item, cfgs. clone ( ) ) , meta) ?;
100134 }
101135 let mut i = 0 ;
102136 let mut attr_idxs = & * attr_idxs;
@@ -130,7 +164,7 @@ fn extract_module_items(
130164 ) ;
131165 }
132166
133- let functions = module. items . into_iter ( ) . map ( |item| match item {
167+ let functions = module. items . into_iter ( ) . map ( |( item, cfgs ) | match item {
134168 ModuleItem :: Function {
135169 item_ident,
136170 py_name,
@@ -140,6 +174,7 @@ fn extract_module_items(
140174 #module_name. to_owned( ) ,
141175 #py_name. to_owned( ) ) ) ;
142176 quote ! {
177+ #( #[ #cfgs ] ) *
143178 vm. __module_set_attr( & module, #py_name, vm. ctx#new_func) . unwrap( ) ;
144179 }
145180 }
0 commit comments