code2graph 0.0.0-beta.4

Purpose-neutral code-graph extraction: source files → symbols, references, and cross-file edges. Tree-sitter based, no storage opinion.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
// SPDX-License-Identifier: Apache-2.0

//! Scala extractor — one tree-sitter pass yielding definitions and references.
//!
//! Definitions: type declarations (`class`, `trait`, `object`, `enum`) and
//! their members (`def`, `val`, `var`, `type`). Qualified identity follows
//! `package_clause` declarations, falling back to a path-derived namespace.
//!
//! References: callee identifiers from `call_expression` (free calls and
//! field-expression qualified calls), inheritance via `extends_clause`,
//! `import_declaration` imports, and return/parameter type references.
//!
//! Emits neutral [`FileFacts`] — no storage entries, no source bodies.

use tree_sitter::{Node, Parser};

use crate::error::{CodegraphError, Result};
use crate::graph::types::{
    Binding, BindingKind, ByteSpan, EntryPoint, FileFacts, RefRole, Reference, Scope, ScopeId,
    ScopeKind, Symbol, SymbolKind, TypeRefContext, Visibility,
};
use crate::lang::Language;
use crate::symbol::Descriptor;

use super::{
    ExtractCtx, Extractor, MIN_REF_LEN, attach_reference_scopes, collect_call_references,
    definition_bindings, field_text, import_bindings, innermost_scope, make_symbol, node_span,
    node_text, one_line_signature, push_binding, push_import_ref, push_ref, push_scope,
    push_type_ref, simple_type_name,
};

/// Tree-sitter query capturing call-callee identifiers.
///
/// Pattern 1: free call `foo()` — identifier directly as `function` field.
/// Pattern 2: generic-function call `foo[T]()` — generic_function wrapping identifier.
/// Pattern 3: qualified call `obj.foo()` — field_expression as `function` field;
///            the receiver is captured as `@qualifier` and the member name as `@callee`.
/// Pattern 4: qualified generic call `obj.foo[T]()`.
const CALL_QUERY: &str = r#"
[
  (call_expression function: (identifier) @callee)
  (call_expression function: (generic_function function: (identifier) @callee))
  (call_expression function: (field_expression value: (_) @qualifier field: (identifier) @callee))
  (call_expression function: (generic_function function: (field_expression value: (_) @qualifier field: (identifier) @callee)))
]
"#;

/// Extracts Scala symbols and references.
pub struct ScalaExtractor;

impl Extractor for ScalaExtractor {
    fn lang(&self) -> Language {
        Language::Scala
    }

    fn extract(&self, source: &str, file: &str) -> Result<FileFacts> {
        let ts_language = crate::grammar::scala();
        let mut parser = Parser::new();
        parser
            .set_language(&ts_language)
            .map_err(|_| CodegraphError::Parse {
                path: file.to_owned(),
            })?;
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| CodegraphError::Parse {
                path: file.to_owned(),
            })?;

        let root = tree.root_node();
        let bytes = source.as_bytes();
        let ctx = ExtractCtx {
            bytes,
            file,
            lang: Language::Scala,
        };
        let namespaces = scala_namespaces(&root, bytes, file);

        let defs = collect_symbols(&root, &ctx, &namespaces);
        let def_bindings = definition_bindings(&defs);
        let mut symbols = defs;
        let mod_sym = super::module_symbol(Language::Scala, &namespaces, file, source.len());
        let module_id = mod_sym.id.to_scip_string();
        symbols.push(mod_sym);

        let mut references = collect_call_references(
            &root,
            &ts_language,
            CALL_QUERY,
            Language::Scala,
            bytes,
            file,
        )?;
        collect_inheritance(&root, bytes, file, &mut references);
        collect_imports(&root, bytes, file, &mut references, &module_id);
        collect_type_references(&root, bytes, file, &mut references);
        collect_read_references(&root, bytes, file, &mut references);
        collect_write_references(&root, bytes, file, &mut references);

        let scopes = collect_scopes(&root, source.len());
        attach_reference_scopes(&mut references, &scopes);
        let mut bindings = collect_bindings(&root, bytes, &scopes);
        bindings.extend(def_bindings);
        bindings.extend(import_bindings(&references, &scopes));

        Ok(FileFacts {
            file: file.to_owned(),
            lang: Language::Scala.as_str().to_owned(),
            symbols,
            references,
            scopes,
            bindings,
            ffi_exports: Vec::new(),
        })
    }
}

// ── Namespace derivation ─────────────────────────────────────────────────────

/// Derive namespace descriptors from `package_clause` nodes, falling back to a
/// path-derived namespace.
///
/// A Scala file may have one or more `package_clause` nodes. Each clause's
/// `name` field can be a dotted identifier like `a.b.c`; we split on `.` and
/// accumulate segments across all clauses.
/// Fallback: strip `.scala`/`.sc`, strip leading `src/`, split on `/`.
fn scala_namespaces(root: &Node, bytes: &[u8], file: &str) -> Vec<String> {
    let mut segments: Vec<String> = Vec::new();

    for child in root.children(&mut root.walk()) {
        if child.kind() != "package_clause" {
            continue;
        }
        if let Some(name_node) = child.child_by_field_name("name") {
            let text = node_text(&name_node, bytes);
            for seg in text.split('.').filter(|s| !s.is_empty()) {
                segments.push(seg.to_owned());
            }
        }
    }

    if !segments.is_empty() {
        return segments;
    }

    // Fallback: derive from file path.
    let p = file
        .strip_suffix(".scala")
        .or_else(|| file.strip_suffix(".sc"))
        .unwrap_or(file);
    let p = p.strip_prefix("src/").unwrap_or(p);
    p.split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_owned)
        .collect()
}

// ── Symbol collection ────────────────────────────────────────────────────────

fn collect_symbols(root: &Node, ctx: &ExtractCtx, namespaces: &[String]) -> Vec<Symbol> {
    let ns_descriptors: Vec<Descriptor> = namespaces
        .iter()
        .cloned()
        .map(Descriptor::Namespace)
        .collect();
    let mut out = Vec::new();
    collect_defs_in(root, ctx, &ns_descriptors, &mut out);
    out
}

/// Collect Scala definition nodes recursively, extending `prefix` as we descend
/// into type bodies.
fn collect_defs_in(node: &Node, ctx: &ExtractCtx, prefix: &[Descriptor], out: &mut Vec<Symbol>) {
    for child in node.children(&mut node.walk()) {
        match child.kind() {
            // Skip into the package body or compilation unit.
            "package_clause" => {
                if let Some(body) = child.child_by_field_name("body") {
                    collect_defs_in(&body, ctx, prefix, out);
                } else {
                    // Package without braces — siblings at the top level continue.
                }
            }

            "class_definition" => {
                emit_type_def(&child, ctx, prefix, SymbolKind::Class, out);
            }
            "trait_definition" => {
                emit_type_def(&child, ctx, prefix, SymbolKind::Trait, out);
            }
            "object_definition" | "package_object" => {
                emit_type_def(&child, ctx, prefix, SymbolKind::Module, out);
            }
            "enum_definition" => {
                emit_enum_def(&child, ctx, prefix, out);
            }
            "function_definition" => {
                emit_function(&child, ctx, prefix, out);
            }
            "val_definition" => {
                emit_val_or_var(&child, ctx, prefix, SymbolKind::Const, out);
            }
            "var_definition" => {
                emit_val_or_var(&child, ctx, prefix, SymbolKind::Static, out);
            }
            "type_definition" => {
                emit_type_alias(&child, ctx, prefix, out);
            }

            _ => {
                // Recurse into any other container (e.g. template_body at top level).
                collect_defs_in(&child, ctx, prefix, out);
            }
        }
    }
}

/// Emit a single type symbol (class/trait/object) and recurse into its body.
fn emit_type_def(
    node: &Node,
    ctx: &ExtractCtx,
    prefix: &[Descriptor],
    kind: SymbolKind,
    out: &mut Vec<Symbol>,
) {
    let Some(name) = name_text(node, ctx.bytes) else {
        return;
    };
    let mut descriptors = prefix.to_vec();
    descriptors.push(Descriptor::Type(name.clone()));
    out.push(make_symbol(
        ctx,
        node,
        name,
        kind,
        read_visibility(node, ctx.bytes),
        descriptors.clone(),
        one_line_signature(node_text(node, ctx.bytes), &['{', ';']),
    ));

    // Recurse into the template body for member definitions.
    if let Some(body) = node.child_by_field_name("body") {
        collect_members_in(&body, ctx, &descriptors, out);
    }
}

/// Emit an enum type and its cases.
fn emit_enum_def(node: &Node, ctx: &ExtractCtx, prefix: &[Descriptor], out: &mut Vec<Symbol>) {
    let Some(name) = name_text(node, ctx.bytes) else {
        return;
    };
    let mut descriptors = prefix.to_vec();
    descriptors.push(Descriptor::Type(name.clone()));
    out.push(make_symbol(
        ctx,
        node,
        name,
        SymbolKind::Enum,
        read_visibility(node, ctx.bytes),
        descriptors.clone(),
        one_line_signature(node_text(node, ctx.bytes), &['{', ';']),
    ));

    // Emit enum cases. They live under `enum_case_definitions` nodes nested in
    // the `enum_body`, and a single `case A, B` puts several `name` fields on one
    // case node — so descend one level and collect every `name` field.
    if let Some(body) = node.child_by_field_name("body") {
        for group in body.children(&mut body.walk()) {
            if group.kind() != "enum_case_definitions" {
                continue;
            }
            for case in group.children(&mut group.walk()) {
                if !matches!(case.kind(), "simple_enum_case" | "full_enum_case") {
                    continue;
                }
                let mut cursor = case.walk();
                for name_node in case
                    .children_by_field_name("name", &mut cursor)
                    .filter(|n| matches!(n.kind(), "identifier" | "operator_identifier"))
                {
                    let case_name = node_text(&name_node, ctx.bytes).to_owned();
                    let mut case_desc = descriptors.clone();
                    case_desc.push(Descriptor::Term(case_name.clone()));
                    out.push(make_symbol(
                        ctx,
                        &case,
                        case_name,
                        SymbolKind::Const,
                        read_visibility(&case, ctx.bytes),
                        case_desc,
                        one_line_signature(node_text(&case, ctx.bytes), &['{', ';', ',']),
                    ));
                }
            }
        }
        // Also descend into body for nested type/method members.
        collect_members_in(&body, ctx, &descriptors, out);
    }
}

/// Collect members inside a `template_body` or `enum_body`.
fn collect_members_in(
    body: &Node,
    ctx: &ExtractCtx,
    type_prefix: &[Descriptor],
    out: &mut Vec<Symbol>,
) {
    for member in body.children(&mut body.walk()) {
        match member.kind() {
            "function_definition" => {
                emit_function(&member, ctx, type_prefix, out);
            }
            "val_definition" => {
                emit_val_or_var(&member, ctx, type_prefix, SymbolKind::Const, out);
            }
            "var_definition" => {
                emit_val_or_var(&member, ctx, type_prefix, SymbolKind::Static, out);
            }
            "type_definition" => {
                emit_type_alias(&member, ctx, type_prefix, out);
            }
            "class_definition" => {
                emit_type_def(&member, ctx, type_prefix, SymbolKind::Class, out);
            }
            "trait_definition" => {
                emit_type_def(&member, ctx, type_prefix, SymbolKind::Trait, out);
            }
            "object_definition" => {
                emit_type_def(&member, ctx, type_prefix, SymbolKind::Module, out);
            }
            "enum_definition" => {
                emit_enum_def(&member, ctx, type_prefix, out);
            }
            // skip simple_enum_case / full_enum_case — handled by emit_enum_def
            _ => {}
        }
    }
}

/// Emit a `function_definition` → `SymbolKind::Method`.
fn emit_function(node: &Node, ctx: &ExtractCtx, prefix: &[Descriptor], out: &mut Vec<Symbol>) {
    let Some(name) = name_text(node, ctx.bytes) else {
        return;
    };
    // Name-only `main` detection (covers `def main` in an `object`).
    let is_main = name == "main";
    let mut descriptors = prefix.to_vec();
    descriptors.push(Descriptor::Method {
        name: name.clone(),
        disambiguator: String::new(),
    });
    out.push(make_symbol(
        ctx,
        node,
        name,
        SymbolKind::Method,
        read_visibility(node, ctx.bytes),
        descriptors,
        one_line_signature(node_text(node, ctx.bytes), &['{', ';', '=']),
    ));
    if is_main {
        if let Some(s) = out.last_mut() {
            s.entry_points.push(EntryPoint::Main);
        }
    }
}

/// Emit a `val_definition` or `var_definition` → `SymbolKind::Const`/`Static`.
///
/// The `pattern` field is typically an `identifier` for the simple case.
/// Destructuring patterns are skipped.
fn emit_val_or_var(
    node: &Node,
    ctx: &ExtractCtx,
    prefix: &[Descriptor],
    kind: SymbolKind,
    out: &mut Vec<Symbol>,
) {
    // The name is in the `pattern` field — try it as an identifier directly,
    // or fall back to checking if the pattern node is an identifier.
    let name: Option<String> = if let Some(pat) = node.child_by_field_name("pattern") {
        if pat.kind() == "identifier" {
            Some(node_text(&pat, ctx.bytes).to_owned())
        } else {
            // Could be a tuple pattern, etc. — skip.
            None
        }
    } else {
        // Try `name` field as fallback.
        field_text(node, "name", ctx.bytes)
    };

    let Some(name) = name else { return };
    let mut descriptors = prefix.to_vec();
    descriptors.push(Descriptor::Term(name.clone()));
    out.push(make_symbol(
        ctx,
        node,
        name,
        kind,
        read_visibility(node, ctx.bytes),
        descriptors,
        one_line_signature(node_text(node, ctx.bytes), &['{', ';', '=']),
    ));
}

/// Emit a `type_definition` → `SymbolKind::TypeAlias`.
fn emit_type_alias(node: &Node, ctx: &ExtractCtx, prefix: &[Descriptor], out: &mut Vec<Symbol>) {
    // `type_definition` has a `name` field = `type_identifier`.
    let Some(name) = field_text(node, "name", ctx.bytes) else {
        return;
    };
    let mut descriptors = prefix.to_vec();
    descriptors.push(Descriptor::Type(name.clone()));
    out.push(make_symbol(
        ctx,
        node,
        name,
        SymbolKind::TypeAlias,
        read_visibility(node, ctx.bytes),
        descriptors,
        one_line_signature(node_text(node, ctx.bytes), &['{', ';', '=']),
    ));
}

/// Get the name text for a definition node.
///
/// Scala definitions use a `name` field that is usually an `identifier` or
/// `operator_identifier`. We simply take whatever text is there.
fn name_text(node: &Node, bytes: &[u8]) -> Option<String> {
    let n = node.child_by_field_name("name")?;
    Some(node_text(&n, bytes).to_owned())
}

/// Read the declared visibility from a Scala definition node.
///
/// Scala's access modifiers live in a `modifiers` named child, which in turn
/// contains an `access_modifier` node. The `access_modifier` is structured as:
///
/// ```text
/// access_modifier
///   "private" | "protected"   ← anonymous keyword token
///   access_qualifier?          ← present when [qualifier] is written
///     identifier               ← the package/this name
/// ```
///
/// Mapping:
/// - `private` (bare)         → `Visibility::Private`
/// - `protected` (bare)       → `Visibility::Protected`
/// - `private[...]`           → `Visibility::Internal`  (scoped/package access)
/// - `protected[...]`         → `Visibility::Internal`
/// - no access modifier       → `Visibility::Public`    (Scala's default)
fn read_visibility(node: &Node, bytes: &[u8]) -> Visibility {
    // Walk unnamed children to find the `modifiers` named child.
    let modifiers = node
        .children(&mut node.walk())
        .find(|c| c.kind() == "modifiers");

    let modifiers = match modifiers {
        Some(m) => m,
        None => return Visibility::Public,
    };

    // Within `modifiers`, look for an `access_modifier` child.
    let access_mod = modifiers
        .children(&mut modifiers.walk())
        .find(|c| c.kind() == "access_modifier");

    let access_mod = match access_mod {
        Some(a) => a,
        None => return Visibility::Public,
    };

    // The first child of `access_modifier` is the anonymous keyword token
    // (`private` or `protected`). We read its text from the source bytes.
    let keyword_node = match access_mod.child(0) {
        Some(n) => n,
        None => return Visibility::Public,
    };
    let keyword = node_text(&keyword_node, bytes);

    // Check whether a `[qualifier]` is present — indicated by an
    // `access_qualifier` named child.
    let has_qualifier = access_mod
        .children(&mut access_mod.walk())
        .any(|c| c.kind() == "access_qualifier");

    if has_qualifier {
        // `private[pkg]`, `protected[pkg]`, `private[this]` → package/scoped.
        Visibility::Internal
    } else {
        match keyword {
            "private" => Visibility::Private,
            "protected" => Visibility::Protected,
            _ => Visibility::Public,
        }
    }
}

// ── Inheritance (extends_clause) ─────────────────────────────────────────────

/// Walk the tree and emit `IsImplementation` references for types listed in an
/// `extends_clause` node (both `class X extends Base with Mixin`).
fn collect_inheritance(node: &Node, bytes: &[u8], file: &str, out: &mut Vec<Reference>) {
    if node.kind() == "extends_clause" {
        for child in node.named_children(&mut node.walk()) {
            match child.kind() {
                "type_identifier" | "identifier" => {
                    push_ref(
                        out,
                        node_text(&child, bytes),
                        &child,
                        file,
                        RefRole::IsImplementation,
                    );
                }
                // Qualified or generic type like `scala.collection.Seq[T]`.
                "generic_type" | "stable_type_identifier" => {
                    let name = simple_type_name(node_text(&child, bytes), ".");
                    push_ref(out, name, &child, file, RefRole::IsImplementation);
                }
                _ => {}
            }
        }
    }
    for child in node.children(&mut node.walk()) {
        collect_inheritance(&child, bytes, file, out);
    }
}

// ── Imports (import_declaration) ─────────────────────────────────────────────

/// Walk the tree emitting `Import` references for `import_declaration` nodes.
///
/// Handles:
/// - `import a.b.C` — leaf `C`, from_path `a.b`.
/// - `import a.b.{C, D}` — two import refs, each with from_path `a.b`.
/// - Wildcard `import a.b._` / `import a.b.*` — skipped.
/// - Renames `import a.{B => C}` — skipped to avoid ambiguity.
fn collect_imports(
    node: &Node,
    bytes: &[u8],
    file: &str,
    out: &mut Vec<Reference>,
    module_id: &str,
) {
    if node.kind() == "import_declaration" {
        // The `path` field is the dotted path; may end with selectors in braces.
        // We look at named children of the import_declaration to find the structure.
        collect_import_node(node, bytes, file, out, module_id);
        return;
    }
    for child in node.children(&mut node.walk()) {
        collect_imports(&child, bytes, file, out, module_id);
    }
}

fn collect_import_node(
    node: &Node,
    bytes: &[u8],
    file: &str,
    out: &mut Vec<Reference>,
    module_id: &str,
) {
    // Walk children to find a potential import_selectors (braced list).
    // Otherwise, the last identifier is the leaf.
    let mut prefix_parts: Vec<&str> = Vec::new();
    let mut selector_node: Option<Node> = None;
    let mut last_id: Option<Node> = None;

    for child in node.named_children(&mut node.walk()) {
        match child.kind() {
            "identifier" | "operator_identifier" => {
                if let Some(prev) = last_id.take() {
                    prefix_parts.push(node_text(&prev, bytes));
                }
                last_id = Some(child);
            }
            "import_selectors" => {
                selector_node = Some(child);
            }
            _ => {}
        }
    }

    if let Some(sel) = selector_node {
        // Braced selectors: build from_path from prefix_parts + last_id.
        if let Some(last) = last_id {
            prefix_parts.push(node_text(&last, bytes));
        }
        let from_path = prefix_parts.join(".");
        for sel_child in sel.named_children(&mut sel.walk()) {
            match sel_child.kind() {
                "identifier" => {
                    let name = node_text(&sel_child, bytes);
                    if name == "_" || name == "*" {
                        continue;
                    }
                    push_import_ref(out, name, &sel_child, file, module_id, &from_path);
                }
                "import_selector" => {
                    // rename or specific selector
                    if let Some(first) = sel_child.named_children(&mut sel_child.walk()).next() {
                        if first.kind() == "identifier" {
                            let name = node_text(&first, bytes);
                            if name == "_" || name == "*" {
                                continue;
                            }
                            // Check if it's a rename (has `=>` child).
                            let has_rename = sel_child
                                .children(&mut sel_child.walk())
                                .any(|c| c.kind() == "=>");
                            if has_rename {
                                continue; // skip renames
                            }
                            push_import_ref(out, name, &first, file, module_id, &from_path);
                        }
                    }
                }
                _ => {}
            }
        }
    } else if let Some(leaf) = last_id {
        // Simple dotted import: `import a.b.C`
        let leaf_text = node_text(&leaf, bytes);
        if leaf_text == "_" || leaf_text == "*" {
            return; // wildcard import
        }
        let from_path = prefix_parts.join(".");
        push_import_ref(out, leaf_text, &leaf, file, module_id, &from_path);
    }
}

// ── Read / Write references ──────────────────────────────────────────────────

/// Returns `true` when `node` (an `identifier`) is in a position already captured
/// by another collector and must NOT also be emitted as a Read reference.
///
/// Excluded positions:
/// - Call callee: the `function:` field of `call_expression` or the inner
///   `function:` field of `generic_function` inside a `call_expression`.
/// - Declaration names: `name:` field of `function_definition`,
///   `class_definition`, `trait_definition`, `object_definition`,
///   `package_object`, `enum_definition`, `type_definition`.
/// - Val/var binding: `pattern:` field of `val_definition` / `var_definition`
///   (the bound name, not a re-assignment).
/// - Parameter names: `name:` field of `parameter` / `class_parameter`.
/// - Import binding names: direct parent is `import_declaration`,
///   `import_selectors`, or `import_selector` (already captured as
///   `RefRole::Import`).
/// - Member-access leaf: the `field:` field of `field_expression` (e.g. the
///   `.foo` in `obj.foo`) — the receiver (`value:`) IS a read; only the leaf
///   member name is skipped.
/// - Assignment LHS: the `left:` field of `assignment_expression` — handled by
///   `collect_write_references`.
fn is_non_read_position(node: &Node) -> bool {
    let parent = match node.parent() {
        Some(p) => p,
        None => return true, // root — not a read
    };
    match parent.kind() {
        // Call callee — `function:` field of `call_expression`.
        "call_expression" => parent.child_by_field_name("function").as_ref() == Some(node),
        // Inner callee of a generic call `foo[T]()` — generic_function's `function:`.
        "generic_function" => parent.child_by_field_name("function").as_ref() == Some(node),
        // Declaration names.
        "function_definition"
        | "class_definition"
        | "trait_definition"
        | "object_definition"
        | "package_object"
        | "enum_definition"
        | "type_definition" => parent.child_by_field_name("name").as_ref() == Some(node),
        // Val/var binding name (binding introduction, not re-assignment).
        "val_definition" | "var_definition" => {
            parent.child_by_field_name("pattern").as_ref() == Some(node)
        }
        // Parameter bound name.
        "parameter" | "class_parameter" => {
            parent.child_by_field_name("name").as_ref() == Some(node)
        }
        // Import binding — already an Import ref.
        "import_declaration" | "import_selectors" | "import_selector" => true,
        // Member-access leaf (`obj.foo` — skip `foo`, keep `obj`).
        "field_expression" => parent.child_by_field_name("field").as_ref() == Some(node),
        // Assignment LHS — handled by collect_write_references.
        "assignment_expression" => parent.child_by_field_name("left").as_ref() == Some(node),
        _ => false,
    }
}

/// Recursively walk `node` and emit [`RefRole::Read`] references for bare
/// `identifier` nodes used in value/expression positions.
///
/// Skips identifiers that are:
/// - Call callees (already [`RefRole::Call`])
/// - Declaration names (function / class / trait / object / val binding / param)
/// - Import binding names (already [`RefRole::Import`])
/// - Member-access leaves (the `field:` of `field_expression`)
/// - Assignment LHS (handled by [`collect_write_references`])
///
/// Applies [`MIN_REF_LEN`] (same threshold as calls).
fn collect_read_references(node: &Node, bytes: &[u8], file: &str, out: &mut Vec<Reference>) {
    if node.kind() == "identifier" {
        let name = node_text(node, bytes);
        if name.len() >= MIN_REF_LEN && !is_non_read_position(node) {
            push_ref(out, name, node, file, RefRole::Read);
        }
        // identifiers have no meaningful children; return early.
        return;
    }
    for child in node.children(&mut node.walk()) {
        collect_read_references(&child, bytes, file, out);
    }
}

/// Recursively walk `node` and emit [`RefRole::Write`] references for the
/// bare-identifier LHS of `assignment_expression` nodes (e.g. `x = expr`).
///
/// Note: `val x = …` / `var x = …` definitions are `val_definition` /
/// `var_definition` nodes — distinct from `assignment_expression` — and are
/// deliberately excluded. Only a re-assignment `x = expr` is a Write.
///
/// Member/index LHS (`obj.field = …`) is not covered in v1. Applies [`MIN_REF_LEN`].
fn collect_write_references(node: &Node, bytes: &[u8], file: &str, out: &mut Vec<Reference>) {
    if node.kind() == "assignment_expression" {
        if let Some(lhs) = node.child_by_field_name("left") {
            if lhs.kind() == "identifier" {
                let name = node_text(&lhs, bytes);
                if name.len() >= MIN_REF_LEN {
                    push_ref(out, name, &lhs, file, RefRole::Write);
                }
            }
        }
    }
    for child in node.children(&mut node.walk()) {
        collect_write_references(&child, bytes, file, out);
    }
}

// ── TypeRef edges ────────────────────────────────────────────────────────────

/// Recursively walk `node` emitting [`RefRole::TypeRef`] references for
/// user-defined type names in typed positions.
///
/// Covers: function return types (`return_type` field), parameter types,
/// val/var type annotations (`type` field).
fn collect_type_references(node: &Node, bytes: &[u8], file: &str, out: &mut Vec<Reference>) {
    match node.kind() {
        "function_definition" => {
            if let Some(ret) = node.child_by_field_name("return_type") {
                type_leaf(&ret, bytes, file, TypeRefContext::ReturnType, out);
            }
        }
        "val_definition" | "var_definition" => {
            if let Some(ty) = node.child_by_field_name("type") {
                type_leaf(&ty, bytes, file, TypeRefContext::Field, out);
            }
        }
        "parameter" | "class_parameter" => {
            if let Some(ty) = node.child_by_field_name("type") {
                type_leaf(&ty, bytes, file, TypeRefContext::ParameterType, out);
            }
        }
        _ => {}
    }
    for child in node.children(&mut node.walk()) {
        collect_type_references(&child, bytes, file, out);
    }
}

fn type_leaf(node: &Node, bytes: &[u8], file: &str, ctx: TypeRefContext, out: &mut Vec<Reference>) {
    match node.kind() {
        // Primitive / builtin scalar types — skip.
        "unit_type" | "tuple_type" | "function_type" => {}
        "type_identifier" | "identifier" => {
            let name = node_text(node, bytes);
            push_type_ref(out, name, node, file, ctx);
        }
        "stable_type_identifier" | "generic_type" => {
            let name = simple_type_name(node_text(node, bytes), ".");
            push_type_ref(out, name, node, file, ctx);
        }
        _ => {
            for child in node.named_children(&mut node.walk()) {
                type_leaf(&child, bytes, file, ctx, out);
            }
        }
    }
}

// ── Scope tree ───────────────────────────────────────────────────────────────

fn collect_scopes(root: &Node, source_len: usize) -> Vec<Scope> {
    let mut scopes = Vec::new();
    push_scope(
        &mut scopes,
        None,
        ByteSpan {
            start: 0,
            end: source_len,
        },
        ScopeKind::Module,
    );
    for child in root.children(&mut root.walk()) {
        scope_dfs(&child, 0, &mut scopes);
    }
    scopes
}

fn scope_dfs(node: &Node, parent_id: ScopeId, scopes: &mut Vec<Scope>) {
    match node.kind() {
        "class_definition" | "trait_definition" | "object_definition" | "package_object"
        | "enum_definition" => {
            let type_id = push_scope(scopes, Some(parent_id), node_span(node), ScopeKind::Type);
            if let Some(body) = node.child_by_field_name("body") {
                for child in body.children(&mut body.walk()) {
                    scope_dfs(&child, type_id, scopes);
                }
            }
        }
        "function_definition" => {
            let fn_id = push_scope(
                scopes,
                Some(parent_id),
                node_span(node),
                ScopeKind::Function,
            );
            if let Some(body) = node.child_by_field_name("body") {
                for child in body.children(&mut body.walk()) {
                    scope_dfs(&child, fn_id, scopes);
                }
            }
        }
        "block" => {
            let block_id = push_scope(scopes, Some(parent_id), node_span(node), ScopeKind::Block);
            for child in node.children(&mut node.walk()) {
                scope_dfs(&child, block_id, scopes);
            }
        }
        _ => {
            for child in node.children(&mut node.walk()) {
                scope_dfs(&child, parent_id, scopes);
            }
        }
    }
}

// ── Bindings ─────────────────────────────────────────────────────────────────

fn collect_bindings(root: &Node, bytes: &[u8], scopes: &[Scope]) -> Vec<Binding> {
    let mut out = Vec::new();
    collect_bindings_dfs(root, bytes, scopes, &mut out);
    out
}

fn collect_bindings_dfs(node: &Node, bytes: &[u8], scopes: &[Scope], out: &mut Vec<Binding>) {
    match node.kind() {
        "function_definition" => {
            // Collect parameters.
            for child in node.children(&mut node.walk()) {
                if child.kind() == "parameters" || child.kind() == "parameter_clause" {
                    collect_params(&child, bytes, scopes, out);
                }
            }
        }
        "val_definition" | "var_definition" => {
            // Local val/var inside a block scope.
            if let Some(pat) = node.child_by_field_name("pattern") {
                if pat.kind() == "identifier" {
                    let name = node_text(&pat, bytes);
                    let intro = pat.start_byte();
                    if name.len() >= MIN_REF_LEN && innermost_scope(intro, scopes) != Some(0) {
                        push_binding(out, name.to_owned(), intro, BindingKind::Local, scopes);
                    }
                }
            }
        }
        _ => {}
    }
    for child in node.children(&mut node.walk()) {
        collect_bindings_dfs(&child, bytes, scopes, out);
    }
}

fn collect_params(params: &Node, bytes: &[u8], scopes: &[Scope], out: &mut Vec<Binding>) {
    for child in params.named_children(&mut params.walk()) {
        if child.kind() == "parameter" || child.kind() == "class_parameter" {
            // The name field may be `name` or the first identifier child.
            let name_opt = child
                .child_by_field_name("name")
                .map(|n| node_text(&n, bytes).to_owned());
            if let Some(name) = name_opt {
                let intro = child.start_byte();
                push_binding(out, name, intro, BindingKind::Param, scopes);
            }
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn extract(src: &str, file: &str) -> FileFacts {
        ScalaExtractor.extract(src, file).unwrap()
    }

    fn by_name(facts: &FileFacts, name: &str) -> Option<Symbol> {
        facts.symbols.iter().find(|s| s.name == name).cloned()
    }

    #[test]
    fn main_method_is_entry_point() {
        let facts = extract(
            "object M { def main(args: Array[String]): Unit = {} }",
            "src/M.scala",
        );
        let main = by_name(&facts, "main").unwrap();
        assert!(
            main.entry_points
                .iter()
                .any(|e| matches!(e, EntryPoint::Main))
        );
    }

    // ── Definitions ──────────────────────────────────────────────────────────

    #[test]
    fn class_and_method_get_correct_scip_strings() {
        let src = r#"
package com.example

class SessionManager {
  def validate(token: String): Boolean = true
}
"#;
        let facts = extract(src, "src/com/example/SessionManager.scala");

        let sm = by_name(&facts, "SessionManager").unwrap();
        assert_eq!(sm.kind, SymbolKind::Class);
        assert_eq!(
            sm.id.to_scip_string(),
            "codegraph . . . com/example/SessionManager#"
        );

        let validate = by_name(&facts, "validate").unwrap();
        assert_eq!(validate.kind, SymbolKind::Method);
        assert_eq!(
            validate.id.to_scip_string(),
            "codegraph . . . com/example/SessionManager#validate()."
        );

        assert_eq!(facts.lang, "scala");
    }

    #[test]
    fn package_declaration_yields_namespace_descriptors() {
        let src = r#"
package a.b

class C {}
"#;
        let facts = extract(src, "src/a/b/C.scala");
        let c = by_name(&facts, "C").unwrap();
        assert_eq!(c.id.to_scip_string(), "codegraph . . . a/b/C#");
    }

    #[test]
    fn trait_is_extracted_as_trait_kind() {
        let src = r#"
package io.example

trait Readable {
  def read(): String
}
"#;
        let facts = extract(src, "src/io/example/Readable.scala");
        let t = by_name(&facts, "Readable").unwrap();
        assert_eq!(t.kind, SymbolKind::Trait);
        assert_eq!(
            t.id.to_scip_string(),
            "codegraph . . . io/example/Readable#"
        );
    }

    #[test]
    fn object_is_extracted_as_module_with_nested_member() {
        let src = r#"
package app

object Config {
  val host: String = "localhost"
}
"#;
        let facts = extract(src, "src/app/Config.scala");
        let obj = by_name(&facts, "Config").unwrap();
        assert_eq!(obj.kind, SymbolKind::Module);
        assert_eq!(obj.id.to_scip_string(), "codegraph . . . app/Config#");

        let host = by_name(&facts, "host").unwrap();
        assert_eq!(host.kind, SymbolKind::Const);
        assert_eq!(host.id.to_scip_string(), "codegraph . . . app/Config#host.");
    }

    #[test]
    fn scala3_enum_yields_enum_and_cases() {
        let src = r#"
package colors

enum Color {
  case Red
  case Green
  case Blue
}
"#;
        let facts = extract(src, "src/colors/Color.scala");

        let color = by_name(&facts, "Color").unwrap();
        assert_eq!(color.kind, SymbolKind::Enum);
        assert_eq!(color.id.to_scip_string(), "codegraph . . . colors/Color#");

        let red = by_name(&facts, "Red").unwrap();
        assert_eq!(red.kind, SymbolKind::Const);
        assert_eq!(red.id.to_scip_string(), "codegraph . . . colors/Color#Red.");
    }

    #[test]
    fn type_alias_is_extracted() {
        let src = r#"
package myapp

type Id = Int
"#;
        let facts = extract(src, "src/myapp/Types.scala");
        let id = by_name(&facts, "Id").unwrap();
        assert_eq!(id.kind, SymbolKind::TypeAlias);
        assert_eq!(id.id.to_scip_string(), "codegraph . . . myapp/Id#");
    }

    // ── References ───────────────────────────────────────────────────────────

    #[test]
    fn parameter_type_yields_type_reference() {
        let src = r#"
package myapp

class Handler {
  def run(req: Request): Unit = {}
}
"#;
        let facts = extract(src, "src/myapp/Handler.scala");
        assert!(
            facts
                .references
                .iter()
                .any(|r| r.role == RefRole::TypeRef && r.name == "Request"),
            "expected a TypeRef to parameter type 'Request': {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn qualified_call_captures_qualifier() {
        let src = r#"
class Client {
  def run(): Unit = {
    val svc = new Service()
    svc.process()
  }
}
"#;
        let facts = extract(src, "src/Client.scala");

        let process = facts
            .references
            .iter()
            .find(|r| r.name == "process")
            .expect("expected Call ref for 'process'");
        assert_eq!(process.role, RefRole::Call);
        assert_eq!(
            process.qualifier.as_deref(),
            Some("svc"),
            "expected qualifier 'svc' on the process call ref",
        );
    }

    #[test]
    fn import_produces_import_reference_with_from_path() {
        let src = r#"
import scala.collection.mutable.ArrayBuffer

class Foo {}
"#;
        let facts = extract(src, "src/Foo.scala");
        let import_refs: Vec<&Reference> = facts
            .references
            .iter()
            .filter(|r| r.role == RefRole::Import)
            .collect();

        let arr = import_refs
            .iter()
            .find(|r| r.name == "ArrayBuffer")
            .expect("expected import ref for 'ArrayBuffer'");
        assert_eq!(
            arr.from_path,
            Some("scala.collection.mutable".to_owned()),
            "from_path should be 'scala.collection.mutable', got {:?}",
            arr.from_path
        );
    }

    // ── Read / Write references ──────────────────────────────────────────────

    #[test]
    fn reassignment_emits_write_and_reads_for_rhs() {
        let src = r#"
object O {
  def m(): Unit = {
    var total = 0
    val bonus = 10
    total = total + bonus
  }
}
"#;
        let facts = extract(src, "src/O.scala");
        let refs = &facts.references;

        // `total =` on the LHS of assignment_expression → Write.
        assert!(
            refs.iter()
                .any(|r| r.role == RefRole::Write && r.name == "total"),
            "expected Write ref for 'total': {:?}",
            refs.iter().map(|r| (&r.role, &r.name)).collect::<Vec<_>>()
        );
        // `bonus` on the RHS → Read.
        assert!(
            refs.iter()
                .any(|r| r.role == RefRole::Read && r.name == "bonus"),
            "expected Read ref for 'bonus': {:?}",
            refs.iter().map(|r| (&r.role, &r.name)).collect::<Vec<_>>()
        );
        // `total` on the RHS of `total + bonus` → Read.
        let read_totals: Vec<_> = refs
            .iter()
            .filter(|r| r.role == RefRole::Read && r.name == "total")
            .collect();
        assert!(
            !read_totals.is_empty(),
            "expected at least one Read ref for 'total' (RHS usage)"
        );
    }

    #[test]
    fn val_definition_does_not_emit_write_for_binding_name() {
        let src = r#"
object O {
  def m(): Unit = {
    val result = compute()
  }
  def compute(): Int = 42
}
"#;
        let facts = extract(src, "src/O.scala");
        assert!(
            !facts
                .references
                .iter()
                .any(|r| r.role == RefRole::Write && r.name == "result"),
            "val binding 'result' must NOT emit a Write ref: {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn free_call_arg_is_read_but_callee_is_not() {
        let src = r#"
object O {
  def m(): Unit = {
    val config = Config()
    logger(config)
  }
  def logger(x: Any): Unit = {}
}
"#;
        let facts = extract(src, "src/O.scala");
        // `config` passed as argument → Read.
        assert!(
            facts
                .references
                .iter()
                .any(|r| r.role == RefRole::Read && r.name == "config"),
            "expected Read ref for argument 'config': {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
        // `logger` is the callee → Call, NOT Read.
        assert!(
            !facts
                .references
                .iter()
                .any(|r| r.role == RefRole::Read && r.name == "logger"),
            "callee 'logger' must NOT emit a Read ref: {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
    }

    // ── Visibility ───────────────────────────────────────────────────────────

    #[test]
    fn plain_def_has_public_visibility() {
        let src = r#"
package app

class Svc {
  def open(): Unit = {}
}
"#;
        let facts = extract(src, "src/app/Svc.scala");
        let sym = by_name(&facts, "open").unwrap();
        assert_eq!(
            sym.visibility,
            Visibility::Public,
            "plain `def` must be Public (Scala default)"
        );
    }

    #[test]
    fn private_def_has_private_visibility() {
        let src = r#"
package app

class Svc {
  private def secret(): Unit = {}
}
"#;
        let facts = extract(src, "src/app/Svc.scala");
        let sym = by_name(&facts, "secret").unwrap();
        assert_eq!(
            sym.visibility,
            Visibility::Private,
            "`private def` must be Private"
        );
    }

    #[test]
    fn protected_def_has_protected_visibility() {
        let src = r#"
package app

class Svc {
  protected def hook(): Unit = {}
}
"#;
        let facts = extract(src, "src/app/Svc.scala");
        let sym = by_name(&facts, "hook").unwrap();
        assert_eq!(
            sym.visibility,
            Visibility::Protected,
            "`protected def` must be Protected"
        );
    }

    #[test]
    fn private_qualified_def_has_internal_visibility() {
        let src = r#"
package app

class Svc {
  private[app] def pkg(): Unit = {}
}
"#;
        let facts = extract(src, "src/app/Svc.scala");
        let sym = by_name(&facts, "pkg").unwrap();
        assert_eq!(
            sym.visibility,
            Visibility::Internal,
            "`private[pkg] def` must be Internal"
        );
    }

    #[test]
    fn field_expression_receiver_is_read_but_leaf_is_not() {
        let src = r#"
object O {
  def m(): Unit = {
    var value = 0
    val source = Src()
    value = source.field
  }
  class Src { val field: Int = 1 }
}
"#;
        let facts = extract(src, "src/O.scala");
        // `source` is the receiver (value: field of field_expression) → Read.
        assert!(
            facts
                .references
                .iter()
                .any(|r| r.role == RefRole::Read && r.name == "source"),
            "expected Read ref for receiver 'source': {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
        // `field` is the member-access leaf → must NOT be a Read.
        assert!(
            !facts
                .references
                .iter()
                .any(|r| r.role == RefRole::Read && r.name == "field"),
            "member-access leaf 'field' must NOT emit a Read ref: {:?}",
            facts
                .references
                .iter()
                .map(|r| (&r.role, &r.name))
                .collect::<Vec<_>>()
        );
    }
}