@@ -196,9 +196,7 @@ impl<'a> GeneratorContext<'a> {
196
196
"{}_capnp" ,
197
197
path_to_stem_string( importpath) ?. replace( '-' , "_" )
198
198
) ;
199
- populate_scope_map (
200
- & gen. node_map ,
201
- & mut gen. scope_map ,
199
+ gen. populate_scope_map (
202
200
default_parent_module_scope. clone ( ) ,
203
201
root_name,
204
202
NameKind :: Verbatim ,
@@ -208,9 +206,7 @@ impl<'a> GeneratorContext<'a> {
208
206
209
207
let root_name = path_to_stem_string ( requested_file. get_filename ( ) ?) ?;
210
208
let root_mod = format ! ( "{}_capnp" , root_name. replace( '-' , "_" ) ) ;
211
- populate_scope_map (
212
- & gen. node_map ,
213
- & mut gen. scope_map ,
209
+ gen. populate_scope_map (
214
210
default_parent_module_scope. clone ( ) ,
215
211
root_mod,
216
212
NameKind :: Verbatim ,
@@ -229,6 +225,75 @@ impl<'a> GeneratorContext<'a> {
229
225
} ,
230
226
}
231
227
}
228
+
229
+ fn populate_scope_map (
230
+ & mut self ,
231
+ mut ancestor_scope_names : Vec < String > ,
232
+ mut current_node_name : String ,
233
+ current_name_kind : NameKind ,
234
+ node_id : u64 ,
235
+ ) -> :: capnp:: Result < ( ) > {
236
+ // unused nodes in imported files might be omitted from the node map
237
+ let Some ( & node_reader) = self . node_map . get ( & node_id) else { return Ok ( ( ) ) } ;
238
+
239
+ for annotation in node_reader. get_annotations ( ) ?. iter ( ) {
240
+ if annotation. get_id ( ) == NAME_ANNOTATION_ID {
241
+ current_node_name = name_annotation_value ( annotation) ?. to_string ( ) ;
242
+ } else if annotation. get_id ( ) == PARENT_MODULE_ANNOTATION_ID {
243
+ ancestor_scope_names = vec ! [ "crate" . to_string( ) ] ;
244
+ ancestor_scope_names. append ( & mut get_parent_module ( annotation) ?) ;
245
+ }
246
+ }
247
+
248
+ let mut scope_names = ancestor_scope_names;
249
+ scope_names. push ( capnp_name_to_rust_name (
250
+ & current_node_name,
251
+ current_name_kind,
252
+ ) ) ;
253
+
254
+ self . scope_map . insert ( node_id, scope_names. clone ( ) ) ;
255
+
256
+ let nested_nodes = node_reader. get_nested_nodes ( ) ?;
257
+ for nested_node in nested_nodes. iter ( ) {
258
+ let nested_node_id = nested_node. get_id ( ) ;
259
+ match self . node_map . get ( & nested_node_id) {
260
+ None => { }
261
+ Some ( node_reader) => match node_reader. which ( ) {
262
+ Ok ( schema_capnp:: node:: Enum ( _enum_reader) ) => {
263
+ self . populate_scope_map (
264
+ scope_names. clone ( ) ,
265
+ nested_node. get_name ( ) ?. to_string ( ) ,
266
+ NameKind :: Verbatim ,
267
+ nested_node_id,
268
+ ) ?;
269
+ }
270
+ _ => {
271
+ self . populate_scope_map (
272
+ scope_names. clone ( ) ,
273
+ nested_node. get_name ( ) ?. to_string ( ) ,
274
+ NameKind :: Module ,
275
+ nested_node_id,
276
+ ) ?;
277
+ }
278
+ } ,
279
+ }
280
+ }
281
+
282
+ if let Ok ( schema_capnp:: node:: Struct ( struct_reader) ) = node_reader. which ( ) {
283
+ let fields = struct_reader. get_fields ( ) ?;
284
+ for field in fields. iter ( ) {
285
+ if let Ok ( schema_capnp:: field:: Group ( group) ) = field. which ( ) {
286
+ self . populate_scope_map (
287
+ scope_names. clone ( ) ,
288
+ get_field_name ( field) ?. to_string ( ) ,
289
+ NameKind :: Module ,
290
+ group. get_type_id ( ) ,
291
+ ) ?;
292
+ }
293
+ }
294
+ }
295
+ Ok ( ( ) )
296
+ }
232
297
}
233
298
234
299
fn path_to_stem_string < P : AsRef < :: std:: path:: Path > > ( path : P ) -> :: capnp:: Result < String > {
@@ -432,82 +497,6 @@ fn capnp_name_to_rust_name(capnp_name: &str, name_kind: NameKind) -> String {
432
497
}
433
498
}
434
499
435
- fn populate_scope_map (
436
- node_map : & collections:: hash_map:: HashMap < u64 , schema_capnp:: node:: Reader > ,
437
- scope_map : & mut collections:: hash_map:: HashMap < u64 , Vec < String > > ,
438
- mut ancestor_scope_names : Vec < String > ,
439
- mut current_node_name : String ,
440
- current_name_kind : NameKind ,
441
- node_id : u64 ,
442
- ) -> :: capnp:: Result < ( ) > {
443
- // unused nodes in imported files might be omitted from the node map
444
- let Some ( node_reader) = node_map. get ( & node_id) else { return Ok ( ( ) ) } ;
445
-
446
- for annotation in node_reader. get_annotations ( ) ?. iter ( ) {
447
- if annotation. get_id ( ) == NAME_ANNOTATION_ID {
448
- current_node_name = name_annotation_value ( annotation) ?. to_string ( ) ;
449
- } else if annotation. get_id ( ) == PARENT_MODULE_ANNOTATION_ID {
450
- ancestor_scope_names = vec ! [ "crate" . to_string( ) ] ;
451
- ancestor_scope_names. append ( & mut get_parent_module ( annotation) ?) ;
452
- }
453
- }
454
-
455
- let mut scope_names = ancestor_scope_names;
456
- scope_names. push ( capnp_name_to_rust_name (
457
- & current_node_name,
458
- current_name_kind,
459
- ) ) ;
460
-
461
- scope_map. insert ( node_id, scope_names. clone ( ) ) ;
462
-
463
- let nested_nodes = node_reader. get_nested_nodes ( ) ?;
464
- for nested_node in nested_nodes. iter ( ) {
465
- let nested_node_id = nested_node. get_id ( ) ;
466
- match node_map. get ( & nested_node_id) {
467
- None => { }
468
- Some ( node_reader) => match node_reader. which ( ) {
469
- Ok ( schema_capnp:: node:: Enum ( _enum_reader) ) => {
470
- populate_scope_map (
471
- node_map,
472
- scope_map,
473
- scope_names. clone ( ) ,
474
- nested_node. get_name ( ) ?. to_string ( ) ,
475
- NameKind :: Verbatim ,
476
- nested_node_id,
477
- ) ?;
478
- }
479
- _ => {
480
- populate_scope_map (
481
- node_map,
482
- scope_map,
483
- scope_names. clone ( ) ,
484
- nested_node. get_name ( ) ?. to_string ( ) ,
485
- NameKind :: Module ,
486
- nested_node_id,
487
- ) ?;
488
- }
489
- } ,
490
- }
491
- }
492
-
493
- if let Ok ( schema_capnp:: node:: Struct ( struct_reader) ) = node_reader. which ( ) {
494
- let fields = struct_reader. get_fields ( ) ?;
495
- for field in fields. iter ( ) {
496
- if let Ok ( schema_capnp:: field:: Group ( group) ) = field. which ( ) {
497
- populate_scope_map (
498
- node_map,
499
- scope_map,
500
- scope_names. clone ( ) ,
501
- get_field_name ( field) ?. to_string ( ) ,
502
- NameKind :: Module ,
503
- group. get_type_id ( ) ,
504
- ) ?;
505
- }
506
- }
507
- }
508
- Ok ( ( ) )
509
- }
510
-
511
500
fn prim_default ( value : & schema_capnp:: value:: Reader ) -> :: capnp:: Result < Option < String > > {
512
501
use crate :: schema_capnp:: value;
513
502
match value. which ( ) ? {
0 commit comments