@@ -101,70 +101,83 @@ fn convert_members(
101
101
) ?) ;
102
102
}
103
103
Chunk :: Class { name, id } => {
104
- let ( _, _, mut methods) = convert_members (
105
- chunks_by_parent
106
- . get ( & id. as_str ( ) )
107
- . map ( Vec :: as_slice)
108
- . unwrap_or_default ( ) ,
109
- chunks_by_id,
110
- chunks_by_parent,
111
- ) ?;
112
- // We sort methods to get a stable output
113
- methods. sort_by ( |l, r| match l. name . cmp ( & r. name ) {
114
- Ordering :: Equal => {
115
- // We put the getter before the setter
116
- if l. decorators . iter ( ) . any ( |d| d == "property" ) {
117
- Ordering :: Less
118
- } else if r. decorators . iter ( ) . any ( |d| d == "property" ) {
119
- Ordering :: Greater
120
- } else {
121
- // We pick an ordering based on decorators
122
- l. decorators . cmp ( & r. decorators )
123
- }
124
- }
125
- o => o,
126
- } ) ;
127
- classes. push ( Class {
128
- name : name. into ( ) ,
129
- methods,
130
- } )
104
+ classes. push ( convert_class ( id, name, chunks_by_id, chunks_by_parent) ?)
131
105
}
132
106
Chunk :: Function {
133
107
name,
134
108
id : _,
135
109
arguments,
136
110
parent : _,
137
111
decorators,
138
- } => functions. push ( Function {
139
- name : name. into ( ) ,
140
- decorators : decorators. clone ( ) ,
141
- arguments : Arguments {
142
- positional_only_arguments : arguments
143
- . posonlyargs
144
- . iter ( )
145
- . map ( convert_argument)
146
- . collect ( ) ,
147
- arguments : arguments. args . iter ( ) . map ( convert_argument) . collect ( ) ,
148
- vararg : arguments
149
- . vararg
150
- . as_ref ( )
151
- . map ( convert_variable_length_argument) ,
152
- keyword_only_arguments : arguments
153
- . kwonlyargs
154
- . iter ( )
155
- . map ( convert_argument)
156
- . collect ( ) ,
157
- kwarg : arguments
158
- . kwarg
159
- . as_ref ( )
160
- . map ( convert_variable_length_argument) ,
161
- } ,
162
- } ) ,
112
+ } => functions. push ( convert_function ( name, arguments, decorators) ) ,
163
113
}
164
114
}
165
115
Ok ( ( modules, classes, functions) )
166
116
}
167
117
118
+ fn convert_class (
119
+ id : & str ,
120
+ name : & str ,
121
+ chunks_by_id : & HashMap < & str , & Chunk > ,
122
+ chunks_by_parent : & HashMap < & str , Vec < & Chunk > > ,
123
+ ) -> Result < Class > {
124
+ let ( nested_modules, nested_classes, mut methods) = convert_members (
125
+ chunks_by_parent
126
+ . get ( & id)
127
+ . map ( Vec :: as_slice)
128
+ . unwrap_or_default ( ) ,
129
+ chunks_by_id,
130
+ chunks_by_parent,
131
+ ) ?;
132
+ ensure ! (
133
+ nested_modules. is_empty( ) ,
134
+ "Classes cannot contain nested modules"
135
+ ) ;
136
+ ensure ! (
137
+ nested_classes. is_empty( ) ,
138
+ "Nested classes are not supported yet"
139
+ ) ;
140
+ // We sort methods to get a stable output
141
+ methods. sort_by ( |l, r| match l. name . cmp ( & r. name ) {
142
+ Ordering :: Equal => {
143
+ // We put the getter before the setter
144
+ if l. decorators . iter ( ) . any ( |d| d == "property" ) {
145
+ Ordering :: Less
146
+ } else if r. decorators . iter ( ) . any ( |d| d == "property" ) {
147
+ Ordering :: Greater
148
+ } else {
149
+ // We pick an ordering based on decorators
150
+ l. decorators . cmp ( & r. decorators )
151
+ }
152
+ }
153
+ o => o,
154
+ } ) ;
155
+ Ok ( Class {
156
+ name : name. into ( ) ,
157
+ methods,
158
+ } )
159
+ }
160
+
161
+ fn convert_function ( name : & str , arguments : & ChunkArguments , decorators : & [ String ] ) -> Function {
162
+ Function {
163
+ name : name. into ( ) ,
164
+ decorators : decorators. to_vec ( ) ,
165
+ arguments : Arguments {
166
+ positional_only_arguments : arguments. posonlyargs . iter ( ) . map ( convert_argument) . collect ( ) ,
167
+ arguments : arguments. args . iter ( ) . map ( convert_argument) . collect ( ) ,
168
+ vararg : arguments
169
+ . vararg
170
+ . as_ref ( )
171
+ . map ( convert_variable_length_argument) ,
172
+ keyword_only_arguments : arguments. kwonlyargs . iter ( ) . map ( convert_argument) . collect ( ) ,
173
+ kwarg : arguments
174
+ . kwarg
175
+ . as_ref ( )
176
+ . map ( convert_variable_length_argument) ,
177
+ } ,
178
+ }
179
+ }
180
+
168
181
fn convert_argument ( arg : & ChunkArgument ) -> Argument {
169
182
Argument {
170
183
name : arg. name . clone ( ) ,
0 commit comments