devscout-rs 0.9.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
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
use crate::query;

use super::blocks::{compact_block, ref_kind_block, ref_kind_block_if_any};
use super::markers::{
    compact_marker, count_suffix, heuristic_suffix, source_suffix, BUS_HOP_UNVERIFIED,
};
use super::ts_tables;

// The first line both `refs` renderers open with: the resolved symbol, its
// kind, and how many inbound references the answer counted against how many it
// printed. `read` splices its own `def:` line in below this one, so the count
// reaches that verb too.
fn refs_head_line(model: &query::RefsModel) -> String {
    let (total, shown) = query::inbound_reference_counts(&model.inbound);
    format!(
        "{}  ({}){}",
        model.id,
        model.kind,
        count_suffix("inbound", total, shown)
    )
}

// The one line that splits an enum's inbound member edges by which MEMBER they
// land on. `refs Toggles` already counted them all under `uses-member`; this
// says how many of that total were member-level and which members carried them,
// which is the half of the answer a caller otherwise has to run a second query
// (or a grep) to get. Printed only for an enum with at least one member-level
// reference, so no other symbol's output moves.
fn member_refs_line(m: &query::MemberRefs) -> String {
    let named = m
        .members
        .iter()
        .map(|e| format!("{} {}", e.name, e.count))
        .collect::<Vec<_>>()
        .join(", ");
    let more = if m.dropped != 0 {
        format!(" +{} more", m.dropped)
    } else {
        String::new()
    };
    format!(
        "member refs: {} across {} member(s): {named}{more}",
        m.total, m.member_count
    )
}

// One `bus-hop` provenance row: publisher (`file:line`), direction, the
// resolved message, the handler (`to`/`to_file`), the evidence word, and how
// many handlers the message reaches in all -- every fact a bus-hop row must
// show, regardless of which side of the edge the queried symbol sits on (see
// `BusHopRow`'s own doc comment). `handlers` is what separates one route
// from one shared contract every publisher appears to reach. `pub(super)` so
// `render::coverage`'s own `tests` bus table reuses this verbatim rather
// than respelling the disclosure a second time.
pub(super) fn bus_hop_line(r: &query::BusHopRow) -> String {
    format!(
        "{}:{}  {}  message={}  handler={}  handlerFile={}  evidence={}  handlers={}  \
         ({BUS_HOP_UNVERIFIED})",
        r.file,
        r.line,
        r.direction.as_str(),
        r.message,
        r.to,
        r.to_file,
        r.evidence,
        r.message_handlers
    )
}

/// Renders a reference-query model as human-readable text.
#[allow(
    clippy::too_many_lines,
    reason = "one ordered pass emitting every section of the refs model in the fixed order the text output promises"
)]
pub fn render_refs_text(model: &query::RefsModel) -> String {
    let mut out: Vec<String> = Vec::new();
    out.push(refs_head_line(model));
    out.push(format!(
        "def: {}",
        model
            .sites
            .iter()
            .map(|s| format!("{}:{}", s.file, s.line))
            .collect::<Vec<_>>()
            .join("  ")
    ));

    out.push("inbound:".to_string());
    ref_kind_block(
        &mut out,
        "inherits",
        Some(&model.inbound.inherits),
        |r: &query::InboundRow| {
            format!(
                "{}:{}  inherits{}{}",
                r.file,
                r.line,
                heuristic_suffix(r.heuristic, r.tier),
                source_suffix(&r.source)
            )
        },
    );
    ref_kind_block(
        &mut out,
        "uses-type",
        Some(&model.inbound.uses_type),
        |r: &query::InboundRow| {
            format!(
                "{}:{}  uses-type{}{}",
                r.file,
                r.line,
                heuristic_suffix(r.heuristic, r.tier),
                source_suffix(&r.source)
            )
        },
    );
    ref_kind_block(
        &mut out,
        "uses-member",
        Some(&model.inbound.uses_member),
        |r: &query::InboundRow| {
            format!(
                "{}:{}  uses-member{}{}",
                r.file,
                r.line,
                heuristic_suffix(r.heuristic, r.tier),
                source_suffix(&r.source)
            )
        },
    );
    ref_kind_block_if_any(
        &mut out,
        "implements",
        &model.inbound.implements,
        |r: &query::InboundRow| {
            format!(
                "{}:{}  implements{}",
                r.file,
                r.line,
                source_suffix(&r.source)
            )
        },
    );
    ref_kind_block_if_any(
        &mut out,
        "overrides",
        &model.inbound.overrides,
        |r: &query::InboundRow| {
            format!(
                "{}:{}  overrides{}",
                r.file,
                r.line,
                source_suffix(&r.source)
            )
        },
    );
    ts_tables::inbound_blocks(&mut out, &model.inbound.ts);
    // One trailer for every inbound kind, because they share one cap: the
    // per-kind headers say how much each kind lost, this says what the call as
    // a whole did not return. `--all` lifts this cap too, the same lever the
    // outbound trailer below names, but the text here already reports the true
    // drop count and is unchanged whether or not `--all` is set.
    let inbound_dropped = model.inbound.inherits.dropped
        + model.inbound.uses_type.dropped
        + model.inbound.uses_member.dropped
        + model.inbound.implements.dropped
        + model.inbound.overrides.dropped
        + ts_tables::sums(&model.inbound.ts).2;
    if inbound_dropped != 0 {
        out.push(format!("  +{inbound_dropped} more"));
    }
    if let Some(m) = &model.member_refs {
        out.push(member_refs_line(m));
    }
    // Prints only when the symbol carries at least one bus-hop row -- the
    // same present-only-when-non-empty rule `implements`/`overrides` follow,
    // so a symbol untouched by a bus hop renders exactly as it did before
    // this section existed.
    ref_kind_block_if_any(&mut out, "bus-hop", &model.bus, bus_hop_line);

    if let Some(ob) = &model.outbound {
        out.push("outbound:".to_string());
        ref_kind_block(
            &mut out,
            "inherits",
            Some(&ob.inherits),
            |r: &query::OutboundRow| {
                format!(
                    "{}:{}  inherits  -> {}{}{}",
                    r.file,
                    r.line,
                    r.to_file,
                    heuristic_suffix(r.heuristic, r.tier),
                    source_suffix(&r.source)
                )
            },
        );
        ref_kind_block(
            &mut out,
            "uses-type",
            Some(&ob.uses_type),
            |r: &query::OutboundRow| {
                format!(
                    "{}:{}  uses-type  -> {}{}{}",
                    r.file,
                    r.line,
                    r.to_file,
                    heuristic_suffix(r.heuristic, r.tier),
                    source_suffix(&r.source)
                )
            },
        );
        ref_kind_block(
            &mut out,
            "uses-member",
            Some(&ob.uses_member),
            |r: &query::OutboundRow| {
                format!(
                    "{}:{}  uses-member  -> {}{}{}",
                    r.file,
                    r.line,
                    r.to_file,
                    heuristic_suffix(r.heuristic, r.tier),
                    source_suffix(&r.source)
                )
            },
        );
        ref_kind_block_if_any(
            &mut out,
            "implements",
            &ob.implements,
            |r: &query::OutboundRow| {
                format!(
                    "{}:{}  implements  -> {}{}",
                    r.file,
                    r.line,
                    r.to_file,
                    source_suffix(&r.source)
                )
            },
        );
        ref_kind_block_if_any(
            &mut out,
            "overrides",
            &ob.overrides,
            |r: &query::OutboundRow| {
                format!(
                    "{}:{}  overrides  -> {}{}",
                    r.file,
                    r.line,
                    r.to_file,
                    source_suffix(&r.source)
                )
            },
        );
        ref_kind_block(
            &mut out,
            "imports",
            Some(&ob.imports),
            |r: &query::ImportRow| {
                format!(
                    "{}:{}  imports  -> {}{}",
                    r.file,
                    r.line,
                    r.target,
                    source_suffix(&r.source)
                )
            },
        );
        ts_tables::outbound_blocks(&mut out, &ob.ts);
        // One trailer for every outbound kind, because they share one cap
        // (mirrors the inbound trailer above). Printing only ever happens when
        // `--all` would actually return more rows, so the hint is never dead
        // advice -- true of the inbound trailer above too, which is why that one
        // stays text-only rather than gaining the same hint.
        let outbound_dropped = ob.inherits.dropped
            + ob.uses_type.dropped
            + ob.uses_member.dropped
            + ob.implements.dropped
            + ob.overrides.dropped
            + ob.imports.dropped
            + ts_tables::sums(&ob.ts).2;
        if outbound_dropped != 0 {
            out.push(format!("  +{outbound_dropped} more, use --all"));
        }
    }

    let amb_in = &model.ambiguous.inbound;
    let amb_out = &model.ambiguous.outbound;
    if amb_in.total != 0 || amb_out.total != 0 {
        out.push("ambiguous:".to_string());
        let amb_row = |r: &query::AmbiguousRow| {
            format!(
                "{}:{}  {}  raw=\"{}\"  candidates={}",
                r.file, r.line, r.origin, r.raw, r.candidate_count
            )
        };
        if amb_in.total != 0 {
            ref_kind_block(&mut out, "inbound", Some(amb_in), amb_row);
        }
        if amb_out.total != 0 {
            ref_kind_block(&mut out, "outbound", Some(amb_out), amb_row);
        }
    }

    if model.manifest_gap != 0 {
        out.push(format!(
            "manifest gap: {} graph file(s) not in manifest",
            model.manifest_gap
        ));
    }
    out.join("\n")
}

/// `--compact` `refs` rendering.
#[allow(
    clippy::too_many_lines,
    reason = "one ordered pass mirroring render_refs_text's section order in the compact shape"
)]
pub fn render_refs_compact(model: &query::RefsModel) -> String {
    let mut out: Vec<String> = Vec::new();
    out.push(refs_head_line(model));
    out.push(format!(
        "def: {}",
        model
            .sites
            .iter()
            .map(|s| format!("{}:{}", s.file, s.line))
            .collect::<Vec<_>>()
            .join("  ")
    ));

    // `file_of_*` are explicitly typed as `fn(&R) -> &str` (not left to
    // inference) so the closure is assigned its higher-ranked signature
    // (`for<'a> fn(&'a R) -> &'a str`) at the point of declaration --
    // without the annotation, an unannotated `let` binds the closure to
    // one concrete (non-reusable) lifetime instead, which then fails to
    // coerce back down at `compact_block`'s `fn` parameter on the second
    // and later call sites below.
    // The one-character marker is `compact_marker`'s (`x` extension, `h`
    // guess); neither collides with a line number, and the run-length collapse
    // treats `5`, `5h` and `5x` as the three distinct entries they are -- a `5x`
    // seen twice reads `5xx2`.
    //
    // Compact groups a file's hits into one `path:line,line` entry, which
    // leaves no slot for a per-hit source line -- the snippet is a
    // default-renderer and `--json` affordance only.
    let file_of_ib: fn(&query::InboundRow) -> &str = |r| r.file.as_str();
    let line_ib =
        |r: &query::InboundRow| format!("{}{}", r.line, compact_marker(r.heuristic, r.tier));
    compact_block(
        &mut out,
        "in:inherits",
        Some(&model.inbound.inherits),
        file_of_ib,
        line_ib,
    );
    compact_block(
        &mut out,
        "in:uses-type",
        Some(&model.inbound.uses_type),
        file_of_ib,
        line_ib,
    );
    compact_block(
        &mut out,
        "in:uses-member",
        Some(&model.inbound.uses_member),
        file_of_ib,
        line_ib,
    );
    compact_block(
        &mut out,
        "in:implements",
        Some(&model.inbound.implements),
        file_of_ib,
        line_ib,
    );
    compact_block(
        &mut out,
        "in:overrides",
        Some(&model.inbound.overrides),
        file_of_ib,
        line_ib,
    );
    ts_tables::compact_blocks(&mut out, "in", &model.inbound.ts, file_of_ib, |r| r.line);
    // The direction letter ('i'/'o') stands in for the full `in`/`out` word --
    // compact strips message/evidence to the same terse shape every other
    // block here already takes. The trailing '?' is the possible-route
    // marker: every bus-hop row is unverified, and compact mode has no room
    // for the full disclosure text, only a marker plus where to find it.
    let file_of_bus: fn(&query::BusHopRow) -> &str = |r| r.file.as_str();
    let line_bus = |r: &query::BusHopRow| format!("{}{}?", r.line, &r.direction.as_str()[..1]);
    compact_block(
        &mut out,
        "bus-hop (? = possible route, rerun without --compact for the full row)",
        Some(&model.bus),
        file_of_bus,
        line_bus,
    );

    if let Some(ob) = &model.outbound {
        let file_of_ob: fn(&query::OutboundRow) -> &str = |r| r.file.as_str();
        let line_ob =
            |r: &query::OutboundRow| format!("{}{}", r.line, compact_marker(r.heuristic, r.tier));
        compact_block(
            &mut out,
            "out:inherits",
            Some(&ob.inherits),
            file_of_ob,
            line_ob,
        );
        compact_block(
            &mut out,
            "out:uses-type",
            Some(&ob.uses_type),
            file_of_ob,
            line_ob,
        );
        compact_block(
            &mut out,
            "out:uses-member",
            Some(&ob.uses_member),
            file_of_ob,
            line_ob,
        );
        compact_block(
            &mut out,
            "out:implements",
            Some(&ob.implements),
            file_of_ob,
            line_ob,
        );
        compact_block(
            &mut out,
            "out:overrides",
            Some(&ob.overrides),
            file_of_ob,
            line_ob,
        );

        let file_of_imp: fn(&query::ImportRow) -> &str = |r| r.file.as_str();
        let line_imp = |r: &query::ImportRow| r.line.to_string();
        compact_block(
            &mut out,
            "out:imports",
            Some(&ob.imports),
            file_of_imp,
            line_imp,
        );
        ts_tables::compact_blocks(&mut out, "out", &ob.ts, file_of_ob, |r| r.line);
    }

    let amb_in = &model.ambiguous.inbound;
    let amb_out = &model.ambiguous.outbound;
    let file_of_amb: fn(&query::AmbiguousRow) -> &str = |r| r.file.as_str();
    let line_amb =
        |r: &query::AmbiguousRow| format!("{}(candidates={})", r.line, r.candidate_count);
    compact_block(&mut out, "amb:in", Some(amb_in), file_of_amb, line_amb);
    compact_block(&mut out, "amb:out", Some(amb_out), file_of_amb, line_amb);

    // The missing-table tolerance documented at the top of this file is what an
    // absent `outbound` falls through here: the three outbound tables plus
    // imports contribute nothing to any of the three sums when the caller did not
    // ask for them.
    let ob = model.outbound.as_ref();
    let ob_sum = |f: fn(&query::OutboundTables) -> usize| ob.map_or(0, f);
    let (ts_total, ts_shown, ts_dropped) = ts_tables::sums(&model.inbound.ts);
    let edges = ts_total
        + model.inbound.inherits.total
        + model.inbound.uses_type.total
        + model.inbound.uses_member.total
        + model.inbound.implements.total
        + model.inbound.overrides.total
        + model.bus.total
        + ob_sum(|o| {
            o.inherits.total
                + o.uses_type.total
                + o.uses_member.total
                + o.implements.total
                + o.overrides.total
                + o.imports.total
                + ts_tables::sums(&o.ts).0
        });
    let shown = ts_shown
        + model.inbound.inherits.rows.len()
        + model.inbound.uses_type.rows.len()
        + model.inbound.uses_member.rows.len()
        + model.inbound.implements.rows.len()
        + model.inbound.overrides.rows.len()
        + model.bus.rows.len()
        + ob_sum(|o| {
            o.inherits.rows.len()
                + o.uses_type.rows.len()
                + o.uses_member.rows.len()
                + o.implements.rows.len()
                + o.overrides.rows.len()
                + o.imports.rows.len()
                + ts_tables::sums(&o.ts).1
        });
    let dropped = ts_dropped
        + model.inbound.inherits.dropped
        + model.inbound.uses_type.dropped
        + model.inbound.uses_member.dropped
        + model.inbound.implements.dropped
        + model.inbound.overrides.dropped
        + model.bus.dropped
        + ob_sum(|o| {
            o.inherits.dropped
                + o.uses_type.dropped
                + o.uses_member.dropped
                + o.implements.dropped
                + o.overrides.dropped
                + o.imports.dropped
                + ts_tables::sums(&o.ts).2
        });
    let ambiguous = amb_in.total + amb_out.total;
    let gap = if model.manifest_gap != 0 {
        format!(" gap={}", model.manifest_gap)
    } else {
        String::new()
    };
    // Same fact as the default renderer's line, spelled the way every other
    // compact block is: `name=count`, no prose, and absent entirely when there is
    // nothing to say.
    if let Some(m) = &model.member_refs {
        let named = m
            .members
            .iter()
            .map(|e| format!("{}={}", e.name, e.count))
            .collect::<Vec<_>>()
            .join(",");
        let more = if m.dropped != 0 {
            format!(",+{}", m.dropped)
        } else {
            String::new()
        };
        out.push(format!("mem: {named}{more}"));
    }
    out.push(format!(
        "summary: edges={edges} shown={shown} dropped={dropped} ambiguous={ambiguous}{gap}"
    ));
    out.join("\n")
}