Skip to main content

core_api/repograph/
render.rs

1//! Turning graph facts into lines an assistant reads.
2//!
3//! Everything here is generic over what is being rendered: the digests in this
4//! module's siblings share the line budget, the number formatting, the path
5//! shortening, and — above all — [`sanitize`], which every string that came
6//! out of the graph must pass through before it reaches a rendered line.
7
8use crate::repograph::context::{ContextReport, Target};
9use crate::repograph::impact::{FileImpact, ImpactReport, Partner};
10use crate::repograph::map::RepoMap;
11use crate::repograph::owners::OwnersReport;
12use crate::repograph::why::{WhyLink, WhyReport};
13use std::fmt::Write as _;
14
15/// Longest digest any `repograph` tool may print, in lines.
16pub const MAX_MAP_LINES: usize = 40;
17/// Longest [`render_context`] digest, in lines. Wider than the others because
18/// it quotes source.
19pub const MAX_CONTEXT_LINES: usize = 60;
20/// Longest digest every other tool here prints, in lines.
21pub const MAX_TOOL_LINES: usize = 25;
22
23/// Separator between the items of a one-line list.
24pub const SEP: &str = " · ";
25
26/// Replace every ASCII control character (`0x00-0x1f` and `0x7f`, tabs and
27/// newlines included) with a space, so a value read out of the graph cannot
28/// forge a line break, a section header, or a terminal escape sequence.
29///
30/// One byte in, one byte out, so a caller's size budget is unaffected.
31#[must_use]
32pub fn sanitize(s: &str) -> String {
33    s.chars()
34        .map(|c| if c.is_ascii_control() { ' ' } else { c })
35        .collect()
36}
37
38/// `1204` → `1,204`. Groups of three, ASCII digits only.
39#[must_use]
40pub fn thousands(n: usize) -> String {
41    let digits = n.to_string();
42    let mut out = String::with_capacity(digits.len() + digits.len() / 3);
43    for (i, c) in digits.chars().enumerate() {
44        if i > 0 && (digits.len() - i).is_multiple_of(3) {
45            out.push(',');
46        }
47        out.push(c);
48    }
49    out
50}
51
52/// `n` of `word`, pluralised by adding an `s`. `1 file`, `2 files`.
53#[must_use]
54pub fn plural(n: usize, word: &str) -> String {
55    if n == 1 {
56        format!("{n} {word}")
57    } else {
58        format!("{} {word}s", thousands(n))
59    }
60}
61
62/// A duration in seconds as one coarse unit: `45s`, `12m`, `3h`, `20d`.
63/// Negative input — a clock that ran backwards — reads as `0s`.
64#[must_use]
65pub fn age(secs: i64) -> String {
66    let s = secs.max(0);
67    if s < 60 {
68        format!("{s}s")
69    } else if s < 3_600 {
70        format!("{}m", s / 60)
71    } else if s < 86_400 {
72        format!("{}h", s / 3_600)
73    } else {
74        format!("{}d", s / 86_400)
75    }
76}
77
78/// Seconds in a day.
79const DAY: i64 = 86_400;
80
81/// The civil `(year, month, day)` a count of days since 1970-01-01 falls on,
82/// proleptic Gregorian. Days before the epoch are negative and convert the
83/// same way.
84///
85/// This is the days-to-civil algorithm every calendar library implements; it
86/// is here rather than behind a dependency because two dozen lines of integer
87/// arithmetic is the whole of what these digests need a calendar for.
88fn civil_from_days(days: i64) -> (i64, u32, u32) {
89    // Shift the epoch to 0000-03-01, so a leap day is always the last day of
90    // the (shifted) year and the month arithmetic below needs no special case.
91    let z = days + 719_468;
92    let era = z.div_euclid(146_097);
93    let doe = z.rem_euclid(146_097); // day of era, 0..=146_096
94    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; // 0..=399
95    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // day of shifted year
96    let mp = (5 * doy + 2) / 153; // shifted month, 0..=11 with March = 0
97    let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
98    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
99    let year = yoe + era * 400 + i64::from(month <= 2);
100    (year, month, day)
101}
102
103/// A Unix timestamp as a calendar date in UTC: `2026-09-04`.
104#[must_use]
105pub fn ymd(ts: i64) -> String {
106    let (y, m, d) = civil_from_days(ts.div_euclid(DAY));
107    format!("{y:04}-{m:02}-{d:02}")
108}
109
110/// The quarter a timestamp falls in, counted from year 0 so that subtracting
111/// one index from another gives a number of quarters.
112#[must_use]
113pub fn quarter_index(ts: i64) -> i64 {
114    let (y, m, _) = civil_from_days(ts.div_euclid(DAY));
115    y * 4 + i64::from((m - 1) / 3)
116}
117
118/// A quarter index as its label: `2026Q3`.
119#[must_use]
120pub fn quarter_label(index: i64) -> String {
121    format!("{}Q{}", index.div_euclid(4), index.rem_euclid(4) + 1)
122}
123
124/// The last `/`-separated segment of a key: `src/core/db.rs` → `db.rs`.
125#[must_use]
126pub fn basename(key: &str) -> &str {
127    key.rsplit_once('/').map_or(key, |(_, base)| base)
128}
129
130/// The directory segments of a key: `src/core/db.rs` → `["src", "core"]`.
131/// A key with no `/` has none.
132#[must_use]
133pub fn dir_components(key: &str) -> Vec<&str> {
134    let mut parts: Vec<&str> = key.split('/').collect();
135    parts.pop();
136    parts
137}
138
139/// The longest directory prefix every key shares, `/`-joined. Empty when the
140/// keys share no leading directory at all.
141#[must_use]
142pub fn common_dir_prefix(keys: &[String]) -> String {
143    let mut iter = keys.iter().map(|k| dir_components(k));
144    let Some(mut prefix) = iter.next() else {
145        return String::new();
146    };
147    for comps in iter {
148        let shared = prefix
149            .iter()
150            .zip(comps.iter())
151            .take_while(|(a, b)| a == b)
152            .count();
153        prefix.truncate(shared);
154        if prefix.is_empty() {
155            break;
156        }
157    }
158    prefix.join("/")
159}
160
161/// The `n` path segments most keys carry, ignoring `prefix`.
162///
163/// A segment is counted once per key, so a directory that appears in twenty
164/// keys beats a filename that appears in one. Ties go to the segment that
165/// sorts first, which is what makes the answer stable. With `dirs_only` the
166/// basename is skipped, leaving the segments that say where a file lives.
167#[must_use]
168pub fn top_tokens(keys: &[String], prefix: &str, n: usize, dirs_only: bool) -> Vec<String> {
169    let mut counts: std::collections::BTreeMap<&str, usize> = std::collections::BTreeMap::new();
170    for key in keys {
171        let rest = match prefix.is_empty() {
172            true => key.as_str(),
173            false => key
174                .strip_prefix(prefix)
175                .unwrap_or(key)
176                .trim_start_matches('/'),
177        };
178        let mut seen: Vec<&str> = rest.split('/').filter(|s| !s.is_empty()).collect();
179        if dirs_only {
180            seen.pop();
181        }
182        seen.sort_unstable();
183        seen.dedup();
184        for token in seen {
185            *counts.entry(token).or_default() += 1;
186        }
187    }
188    let mut ranked: Vec<(&str, usize)> = counts.into_iter().collect();
189    ranked.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(b.0)));
190    ranked
191        .into_iter()
192        .take(n)
193        .map(|(t, _)| t.to_string())
194        .collect()
195}
196
197/// What a set of files with no shared directory is called.
198pub const MIXED: &str = "<mixed>";
199
200/// What to call a set of files.
201///
202/// The directory they all sit under, when there is one — that is the name a
203/// person would use — followed by the two subdirectories most of them sit in,
204/// which is what tells two clusters under the same root apart. Files that
205/// share no directory get [`MIXED`] in the prefix's place.
206///
207/// Files sitting directly in the shared directory add nothing to it, so a
208/// cluster that is exactly one directory deep is named by that directory
209/// alone.
210#[must_use]
211pub fn cluster_name(keys: &[String]) -> String {
212    let prefix = common_dir_prefix(keys);
213    let head = if prefix.is_empty() {
214        MIXED.to_string()
215    } else {
216        prefix.clone()
217    };
218    let mut tokens = top_tokens(keys, &prefix, 2, true);
219    if tokens.is_empty() && prefix.is_empty() {
220        // Everything is at the root: the filenames are all there is to say.
221        tokens = top_tokens(keys, &prefix, 2, false);
222    }
223    if tokens.is_empty() {
224        head
225    } else {
226        format!("{head} {}", tokens.join(", "))
227    }
228}
229
230/// Shorten keys to their filenames, keeping the full path for any filename
231/// that would otherwise appear twice.
232///
233/// `mod.rs, mod.rs` names nothing; `src/net/mod.rs, src/io/mod.rs` names two
234/// files. Sanitized, since the result is printed.
235#[must_use]
236pub fn short_names(keys: &[String]) -> Vec<String> {
237    let mut seen: std::collections::BTreeMap<&str, usize> = std::collections::BTreeMap::new();
238    for key in keys {
239        *seen.entry(basename(key)).or_default() += 1;
240    }
241    keys.iter()
242        .map(|k| match seen.get(basename(k)) {
243            Some(1) => sanitize(basename(k)),
244            _ => sanitize(k),
245        })
246        .collect()
247}
248
249/// Keep at most `max` lines, dropping the rest.
250#[must_use]
251pub fn cap_lines(text: &str, max: usize) -> String {
252    let mut out = String::with_capacity(text.len());
253    for line in text.lines().take(max) {
254        out.push_str(line);
255        out.push('\n');
256    }
257    out
258}
259
260/// The one line a store with nothing in it gets: what is missing, and the
261/// command that fixes it.
262pub const EMPTY_MAP: &str =
263    "mushroomdb map — empty store; run: mushroomdb ingest-git <db> <repo>\n";
264
265/// Render a [`RepoMap`] as the digest an assistant reads: at most
266/// [`MAX_MAP_LINES`] lines, byte-identical for the same map.
267///
268/// Every value that came out of the graph is sanitized again here, so the
269/// output is safe whether or not the map was built by
270/// [`repo_map`](crate::repograph::repo_map).
271#[must_use]
272pub fn render_map(m: &RepoMap) -> String {
273    if m.files == 0 {
274        return EMPTY_MAP.to_string();
275    }
276    let mut out = String::new();
277
278    // Header: the size of the graph, and how current it is.
279    let sync = match &m.last_sync {
280        None => "not synced".to_string(),
281        Some(s) => {
282            let sha = sanitize(&s.sha);
283            let short: String = sha.chars().take(7).collect();
284            match s.age_secs {
285                Some(secs) => format!("synced {} ago at {short}", age(secs)),
286                None => format!("synced at {short}"),
287            }
288        }
289    };
290    let _ = writeln!(
291        out,
292        "mushroomdb map — {}, {}, {}, {} · {sync}{}",
293        plural(m.files, "file"),
294        plural(m.symbols, "symbol"),
295        plural(m.commits, "commit"),
296        plural(m.authors, "author"),
297        if m.truncated { " (truncated)" } else { "" }
298    );
299
300    if !m.communities.is_empty() {
301        out.push_str("clusters (co-change + imports)\n");
302        for (i, c) in m.communities.iter().enumerate() {
303            let samples = short_names(&c.samples);
304            let _ = writeln!(
305                out,
306                "  {}. {}  ({}, cohesion {:.2}){}{}",
307                i + 1,
308                sanitize(&c.name),
309                plural(c.size, "file"),
310                c.cohesion,
311                if samples.is_empty() { "" } else { "  " },
312                samples.join(", ")
313            );
314        }
315    }
316
317    if !m.key_files.is_empty() {
318        out.push_str("key files (most depended-on)\n");
319        // Two decimals, like every other float here. A PageRank score is a
320        // ranking, and the order it is printed in already carries that; the
321        // number is there for the gap between one file and the next.
322        let items: Vec<String> = m
323            .key_files
324            .iter()
325            .map(|(k, s)| format!("{} {s:.2}", sanitize(k)))
326            .collect();
327        let _ = writeln!(out, "  {}", items.join(SEP));
328    }
329
330    if !m.owners.is_empty() {
331        out.push_str("owners\n");
332        let items: Vec<String> = m
333            .owners
334            .iter()
335            .enumerate()
336            .map(|(i, (name, n))| match i {
337                // The unit is stated once, on the first entry.
338                0 => format!("{} {}", sanitize(name), plural(*n, "file")),
339                _ => format!("{} {n}", sanitize(name)),
340            })
341            .collect();
342        let _ = writeln!(out, "  {}", items.join(SEP));
343    }
344
345    if !m.hot_files.is_empty() {
346        let _ = writeln!(out, "hot (last {} days)", m.hot_days);
347        let items: Vec<String> = m
348            .hot_files
349            .iter()
350            .map(|(k, n)| format!("{} {n}", sanitize(k)))
351            .collect();
352        let _ = writeln!(out, "  {}", items.join(SEP));
353    }
354
355    if m.stale_concepts > 0 {
356        let (noun, verb) = if m.stale_concepts == 1 {
357            ("concept", "needs")
358        } else {
359            ("concepts", "need")
360        };
361        let _ = writeln!(
362            out,
363            "notes: {} {noun} {verb} re-learning (source changed)",
364            m.stale_concepts
365        );
366    }
367
368    if !m.questions.is_empty() {
369        let asks: Vec<String> = m.questions.iter().map(|q| sanitize(q)).collect();
370        let _ = writeln!(out, "ask me: {}", asks.join(SEP));
371    }
372
373    cap_lines(&out, MAX_MAP_LINES)
374}
375
376// ── the four per-node digests ───────────────────────────────────────────────
377
378/// Source lines [`render_context`] prints before it says how many are left.
379/// The report keeps up to
380/// [`MAX_SOURCE_LINES`](crate::repograph::MAX_SOURCE_LINES); a digest that
381/// quoted all of them would have room for nothing else.
382const MAX_SOURCE_PRINTED: usize = 40;
383/// Candidates [`render_context`] lists for an ambiguous name. Past this many
384/// the list is not a choice anyone can make from a digest, and the caller wants
385/// a longer key rather than a longer list.
386const MAX_CANDIDATES: usize = 20;
387/// Files [`render_impact`] prints in full.
388const MAX_IMPACT_FILES: usize = 5;
389/// Links [`render_why`] prints in full.
390const MAX_WHY_LINKS: usize = 5;
391
392/// Write a `name  a · b · c` section, or nothing when there is nothing to say.
393fn section(out: &mut String, name: &str, items: &[String]) {
394    if !items.is_empty() {
395        let _ = writeln!(out, "{name}  {}", items.join(SEP));
396    }
397}
398
399/// `(sha, ts, subject)` as one line of a digest.
400fn commit_line(sha: &str, ts: i64, subject: &str) -> String {
401    let short: String = sanitize(sha).chars().take(7).collect();
402    format!("{short} {} {}", ymd(ts), sanitize(subject))
403}
404
405/// Render a [`ContextReport`] as the digest an assistant reads: at most
406/// [`MAX_CONTEXT_LINES`] lines, byte-identical for the same report.
407#[must_use]
408pub fn render_context(c: &ContextReport) -> String {
409    let mut out = String::new();
410    match &c.target {
411        Target::Unknown { target } if c.candidates.is_empty() => {
412            let _ = writeln!(out, "mushroomdb context — unknown: {}", sanitize(target));
413            return out;
414        }
415        Target::Unknown { target } => {
416            let _ = writeln!(
417                out,
418                "mushroomdb context — {} is ambiguous: {}",
419                sanitize(target),
420                plural(c.candidates.len(), "symbol")
421            );
422            for key in c.candidates.iter().take(MAX_CANDIDATES) {
423                let _ = writeln!(out, "  {}", sanitize(key));
424            }
425            if c.candidates.len() > MAX_CANDIDATES {
426                let _ = writeln!(
427                    out,
428                    "  … {} not shown",
429                    plural(c.candidates.len() - MAX_CANDIDATES, "symbol")
430                );
431            }
432            return cap_lines(&out, MAX_CONTEXT_LINES);
433        }
434        Target::File { path } => {
435            let _ = writeln!(out, "mushroomdb context — file {}", sanitize(path));
436        }
437        Target::Symbol { key } => {
438            let _ = writeln!(
439                out,
440                "mushroomdb context — symbol {} in {}",
441                sanitize(key),
442                sanitize(&c.file)
443            );
444        }
445    }
446
447    if let Some(sig) = &c.signature {
448        let _ = writeln!(out, "signature  {}", sanitize(sig));
449    }
450    if let Some(doc) = &c.doc {
451        let _ = writeln!(out, "doc  {}", sanitize(doc));
452    }
453    let mut about: Vec<String> = Vec::new();
454    if let Some((first, last)) = c.lines {
455        about.push(format!("lines {first}-{last}"));
456    }
457    if let Some(owner) = &c.owner {
458        about.push(format!("owner {}", sanitize(owner)));
459    }
460    section(&mut out, "where", &about);
461
462    if let Some(source) = &c.source {
463        let first = c.lines.map_or(1, |(first, _)| first);
464        let total = source.lines().count();
465        let _ = writeln!(out, "source");
466        for (i, line) in source.lines().take(MAX_SOURCE_PRINTED).enumerate() {
467            let n = first as usize + i;
468            let _ = writeln!(out, "  {n:>5} | {}", sanitize(line));
469        }
470        if total > MAX_SOURCE_PRINTED {
471            let _ = writeln!(
472                out,
473                "  … {} more",
474                plural(total - MAX_SOURCE_PRINTED, "line")
475            );
476        }
477    }
478
479    let calls = |items: &[(String, u32)]| -> Vec<String> {
480        items
481            .iter()
482            .map(|(key, line)| match line {
483                0 => sanitize(key),
484                n => format!("{} line {n}", sanitize(key)),
485            })
486            .collect()
487    };
488    section(&mut out, "callers", &calls(&c.callers));
489    section(&mut out, "callees", &calls(&c.callees));
490    section(
491        &mut out,
492        "imports",
493        &c.imports.iter().map(|k| sanitize(k)).collect::<Vec<_>>(),
494    );
495    section(
496        &mut out,
497        "importers",
498        &c.importers.iter().map(|k| sanitize(k)).collect::<Vec<_>>(),
499    );
500    section(
501        &mut out,
502        "co-change",
503        &c.partners
504            .iter()
505            .map(|(k, s)| format!("{} {s:.2}", sanitize(k)))
506            .collect::<Vec<_>>(),
507    );
508    section(
509        &mut out,
510        "commits",
511        &c.recent_commits
512            .iter()
513            .map(|(sha, ts, subject)| commit_line(sha, *ts, subject))
514            .collect::<Vec<_>>(),
515    );
516    for (key, text) in &c.notes {
517        let _ = writeln!(out, "note  {} {}", sanitize(key), sanitize(text));
518    }
519    for (key, name) in &c.concepts {
520        let _ = writeln!(out, "concept  {} {}", sanitize(key), sanitize(name));
521    }
522    cap_lines(&out, MAX_CONTEXT_LINES)
523}
524
525/// One partner or importer as `path score modified`, with the parts that say
526/// nothing left off.
527fn partner_item(p: &Partner, with_score: bool) -> String {
528    let mut item = sanitize(&p.path);
529    if with_score {
530        let _ = write!(item, " {:.2}", p.score);
531    }
532    if p.modified {
533        item.push_str(" modified");
534    }
535    item
536}
537
538/// Render an [`ImpactReport`]: at most [`MAX_TOOL_LINES`] lines.
539#[must_use]
540pub fn render_impact(r: &ImpactReport) -> String {
541    let mut out = String::new();
542    let _ = writeln!(
543        out,
544        "mushroomdb impact — {}",
545        plural(r.files.len(), "changed file")
546    );
547    for f in r.files.iter().take(MAX_IMPACT_FILES) {
548        render_file_impact(&mut out, f);
549    }
550    if r.files.len() > MAX_IMPACT_FILES {
551        let _ = writeln!(
552            out,
553            "… {} not shown",
554            plural(r.files.len() - MAX_IMPACT_FILES, "file")
555        );
556    }
557    for path in &r.unknown {
558        let _ = writeln!(out, "unknown: {}", sanitize(path));
559    }
560    cap_lines(&out, MAX_TOOL_LINES)
561}
562
563fn render_file_impact(out: &mut String, f: &FileImpact) {
564    match &f.owner {
565        Some(owner) => {
566            let _ = writeln!(out, "{} ({})", sanitize(&f.path), sanitize(owner));
567        }
568        None => {
569            let _ = writeln!(out, "{}", sanitize(&f.path));
570        }
571    }
572    section(
573        out,
574        "  partners ",
575        &f.partners
576            .iter()
577            .map(|p| partner_item(p, true))
578            .collect::<Vec<_>>(),
579    );
580    section(
581        out,
582        "  importers",
583        &f.importers
584            .iter()
585            .map(|p| partner_item(p, false))
586            .collect::<Vec<_>>(),
587    );
588    section(
589        out,
590        "  used by  ",
591        &f.symbols_used_elsewhere
592            .iter()
593            .map(|(key, n)| format!("{} {}", sanitize(key), plural(*n, "caller")))
594            .collect::<Vec<_>>(),
595    );
596}
597
598/// Render an [`OwnersReport`]: at most [`MAX_TOOL_LINES`] lines.
599///
600/// The author key is printed once, on the `top` line and in parentheses, so a
601/// reader can address the person the graph means without every other line
602/// carrying a mail address.
603#[must_use]
604pub fn render_owners(o: &OwnersReport) -> String {
605    let mut out = String::new();
606    let _ = writeln!(out, "mushroomdb owners — {}", sanitize(&o.path));
607    if let Some((name, key, share)) = &o.top {
608        let _ = writeln!(
609            out,
610            "top  {} ({}) {share:.2} of the file's commits",
611            sanitize(name),
612            sanitize(key)
613        );
614    }
615    section(
616        &mut out,
617        "knows",
618        &o.knows
619            .iter()
620            .map(|(name, score)| format!("{} {score:.2}", sanitize(name)))
621            .collect::<Vec<_>>(),
622    );
623    if let Some((sha, ts, subject)) = &o.last_touch {
624        let _ = writeln!(out, "last touch  {}", commit_line(sha, *ts, subject));
625    }
626    section(
627        &mut out,
628        "by quarter",
629        &o.by_quarter
630            .iter()
631            .map(|(q, name, n)| format!("{} {} {n}", sanitize(q), sanitize(name)))
632            .collect::<Vec<_>>(),
633    );
634    cap_lines(&out, MAX_TOOL_LINES)
635}
636
637/// Render a [`WhyReport`]: at most [`MAX_TOOL_LINES`] lines.
638#[must_use]
639pub fn render_why(w: &WhyReport) -> String {
640    let mut out = String::new();
641    let _ = writeln!(
642        out,
643        "mushroomdb why — {} ↔ {}",
644        sanitize(&w.a),
645        sanitize(&w.b)
646    );
647    for key in &w.unknown {
648        let _ = writeln!(out, "unknown: {}", sanitize(key));
649    }
650    if !w.unknown.is_empty() {
651        return cap_lines(&out, MAX_TOOL_LINES);
652    }
653    let links = pair_up(&w.links);
654    for (link, both_ways) in links.iter().take(MAX_WHY_LINKS) {
655        render_link(&mut out, link, *both_ways);
656    }
657    if links.len() > MAX_WHY_LINKS {
658        let _ = writeln!(
659            out,
660            "… {} not shown",
661            plural(links.len() - MAX_WHY_LINKS, "link")
662        );
663    }
664    if !w.path.is_empty() {
665        let mut walk = sanitize(&w.a);
666        for (edge_type, node) in &w.path {
667            let _ = write!(walk, " -[{}]-> {}", sanitize(edge_type), sanitize(node));
668        }
669        let _ = writeln!(out, "path  {walk}");
670    }
671    if w.links.is_empty() && w.path.is_empty() {
672        let _ = writeln!(out, "no link");
673    }
674    cap_lines(&out, MAX_TOOL_LINES)
675}
676
677/// Pair off two edges that say the same thing in opposite directions.
678///
679/// A rule such as `co_changed` matches both ways round and the engine reports
680/// an edge each way, scored the same and evidenced by the same commits.
681/// Printing those commits twice says nothing the first printing did not, so the
682/// second is folded into the first, which then reads `a↔b`.
683///
684/// The fold requires the score **and** the evidence to be equal, which is what
685/// makes it safe: two files that import each other, or two documents that
686/// mention each other, also have an edge each way, but each carries its own
687/// line of a different file, and each of those lines is printed. The report
688/// itself always keeps both edges — they are what the graph holds.
689fn pair_up(links: &[WhyLink]) -> Vec<(&WhyLink, bool)> {
690    let mut out: Vec<(&WhyLink, bool)> = Vec::new();
691    let mut folded: Vec<bool> = vec![false; links.len()];
692    for (i, link) in links.iter().enumerate() {
693        if folded[i] {
694            continue;
695        }
696        let mut both_ways = false;
697        for (j, other) in links.iter().enumerate().skip(i + 1) {
698            if !folded[j]
699                && other.rule == link.rule
700                && other.edge_type == link.edge_type
701                && other.direction != link.direction
702                && other.score == link.score
703                && other.evidence == link.evidence
704            {
705                folded[j] = true;
706                both_ways = true;
707                break;
708            }
709        }
710        out.push((link, both_ways));
711    }
712    out
713}
714
715fn render_link(out: &mut String, link: &WhyLink, both_ways: bool) {
716    let mut head = format!(
717        "{} {}  {}",
718        sanitize(&link.edge_type),
719        if both_ways {
720            "a↔b".to_string()
721        } else {
722            sanitize(&link.direction)
723        },
724        sanitize(&link.rule)
725    );
726    if let Some(score) = link.score {
727        let _ = write!(head, " {score:.2}");
728    }
729    if let Some(via) = &link.via {
730        let _ = write!(head, " via {}", sanitize(via));
731    }
732    let _ = writeln!(out, "{head}");
733    for line in &link.evidence {
734        let _ = writeln!(out, "  {}", sanitize(line));
735    }
736}
737
738#[cfg(test)]
739mod tests {
740    use super::*;
741
742    #[test]
743    fn a_timestamp_reads_as_a_utc_date_and_a_quarter() {
744        // Epoch, a leap day, the end of a century that is not a leap year, and
745        // a date before the epoch.
746        for (ts, date, quarter) in [
747            (0_i64, "1970-01-01", "1970Q1"),
748            (1_582_934_400, "2020-02-29", "2020Q1"),
749            (951_782_400, "2000-02-29", "2000Q1"),
750            (1_600_000_000, "2020-09-13", "2020Q3"),
751            (1_609_459_199, "2020-12-31", "2020Q4"),
752            (1_609_459_200, "2021-01-01", "2021Q1"),
753            (-1, "1969-12-31", "1969Q4"),
754        ] {
755            assert_eq!(ymd(ts), date, "{ts}");
756            assert_eq!(quarter_label(quarter_index(ts)), quarter, "{ts}");
757        }
758    }
759
760    #[test]
761    fn quarter_indices_are_a_count_a_window_can_be_measured_in() {
762        let q3 = quarter_index(1_600_000_000); // 2020Q3
763        assert_eq!(quarter_label(q3 - 3), "2019Q4");
764        assert_eq!(quarter_label(q3 + 1), "2020Q4");
765        assert_eq!(quarter_label(q3 + 2), "2021Q1");
766    }
767
768    #[test]
769    fn sanitize_replaces_every_control_character_one_for_one() {
770        let forged = "Ada\nmushroomdb map\t— 9 files\u{7f}\u{1b}[31m";
771        let clean = sanitize(forged);
772        assert_eq!(clean.len(), forged.len(), "one byte in, one byte out");
773        assert!(!clean.contains('\n') && !clean.contains('\t') && !clean.contains('\u{1b}'));
774        assert_eq!(clean, "Ada mushroomdb map — 9 files  [31m");
775    }
776
777    #[test]
778    fn thousands_groups_from_the_right() {
779        for (n, want) in [
780            (0, "0"),
781            (7, "7"),
782            (999, "999"),
783            (1_000, "1,000"),
784            (1_204, "1,204"),
785            (999_999, "999,999"),
786            (1_830_412, "1,830,412"),
787        ] {
788            assert_eq!(thousands(n), want, "{n}");
789        }
790    }
791
792    #[test]
793    fn plural_says_one_file_and_two_files() {
794        assert_eq!(plural(1, "file"), "1 file");
795        assert_eq!(plural(0, "file"), "0 files");
796        assert_eq!(plural(1_204, "commit"), "1,204 commits");
797    }
798
799    #[test]
800    fn age_picks_one_coarse_unit() {
801        for (secs, want) in [
802            (-5, "0s"),
803            (0, "0s"),
804            (59, "59s"),
805            (60, "1m"),
806            (720, "12m"),
807            (3_600, "1h"),
808            (86_399, "23h"),
809            (86_400, "1d"),
810            (20 * 86_400, "20d"),
811        ] {
812            assert_eq!(age(secs), want, "{secs}");
813        }
814    }
815
816    #[test]
817    fn paths_split_into_a_base_and_its_directories() {
818        assert_eq!(basename("src/core/db.rs"), "db.rs");
819        assert_eq!(basename("README.md"), "README.md");
820        assert_eq!(dir_components("src/core/db.rs"), vec!["src", "core"]);
821        assert!(dir_components("README.md").is_empty());
822    }
823
824    #[test]
825    fn a_cluster_is_named_by_the_directory_its_files_share() {
826        // One directory deep: the directory is the whole name.
827        let same = vec![
828            "crates/core-api/src/db.rs".to_string(),
829            "crates/core-api/src/algo.rs".to_string(),
830        ];
831        assert_eq!(cluster_name(&same), "crates/core-api/src");
832        // Split across subdirectories: they are what tells this cluster from
833        // another one under the same root.
834        let partial = vec![
835            "crates/core-api/src/db.rs".to_string(),
836            "crates/core-api/tests/algo.rs".to_string(),
837        ];
838        assert_eq!(cluster_name(&partial), "crates/core-api src, tests");
839    }
840
841    #[test]
842    fn files_sharing_no_directory_are_named_by_their_commonest_segments() {
843        let mixed = vec![
844            "docs/site/algorithms.md".to_string(),
845            "docs/site/install.md".to_string(),
846            "site/index.html".to_string(),
847            "README.md".to_string(),
848        ];
849        // Nothing is shared at the root, so the name falls back to segments:
850        // `site` appears in three keys, and `docs` in two.
851        assert_eq!(cluster_name(&mixed), "<mixed> site, docs");
852        assert_eq!(cluster_name(&["a.rs".to_string()]), "<mixed> a.rs");
853        assert_eq!(cluster_name(&[]), "<mixed>");
854    }
855
856    #[test]
857    fn a_segment_counts_once_per_key_however_often_it_repeats() {
858        let keys = vec!["a/a/a/a.rs".to_string(), "b/x.rs".to_string()];
859        assert_eq!(top_tokens(&keys, "", 1, true), vec!["a".to_string()]);
860        // Without `dirs_only` the filenames join the count and `a` still wins.
861        assert_eq!(top_tokens(&keys, "", 1, false), vec!["a".to_string()]);
862    }
863
864    #[test]
865    fn short_names_keep_the_path_only_where_a_filename_repeats() {
866        let keys = vec![
867            "src/net/mod.rs".to_string(),
868            "src/io/mod.rs".to_string(),
869            "src/db.rs".to_string(),
870        ];
871        assert_eq!(
872            short_names(&keys),
873            vec!["src/net/mod.rs", "src/io/mod.rs", "db.rs"]
874        );
875    }
876
877    #[test]
878    fn cap_lines_keeps_the_first_lines_and_a_trailing_newline() {
879        assert_eq!(cap_lines("a\nb\nc\n", 2), "a\nb\n");
880        assert_eq!(cap_lines("a\nb", 9), "a\nb\n");
881        assert_eq!(cap_lines("", 9), "");
882    }
883}