aoc_toolkit/cli/output/progress/
markdown.rs1use crate::application::progress::{
2 DayAvailability, EditionProgress, ProgressDayReport, VerifiedStar,
3};
4
5#[must_use]
7pub fn render(reports: &[EditionProgress]) -> String {
8 let mut lines = vec![
9 "### Advent of Code progress".to_owned(),
10 String::new(),
11 "| Year | Stars | Completed days | Completion |".to_owned(),
12 "| ---: | ---: | ---: | ---: |".to_owned(),
13 ];
14 lines.extend(reports.iter().map(summary_row));
15
16 let max_days = reports
17 .iter()
18 .map(|report| report.days.len())
19 .max()
20 .unwrap_or(0);
21 if max_days > 0 {
22 lines.push(String::new());
23 lines.push(matrix_header(max_days));
24 lines.push(format!("| ---: |{}", " :---: |".repeat(max_days)));
25 lines.extend(reports.iter().map(|report| matrix_row(report, max_days)));
26 lines.push(String::new());
27 lines.push(
28 "`?` unknown · `L` locked · `—` outside the edition · `≥` partial snapshot".to_owned(),
29 );
30 }
31
32 lines.push(String::new());
33 lines.join("\n")
34}
35
36fn summary_row(report: &EditionProgress) -> String {
37 let year = report.year;
38 let Some(percent) = report.completion_percent() else {
39 return format!("| {year} | Unknown | Unknown | Unknown |");
40 };
41
42 let prefix = if report.has_complete_star_coverage() {
43 ""
44 } else {
45 "≥"
46 };
47
48 format!(
49 "| {year} | {prefix}{}/{} | {prefix}{}/{} | {prefix}{percent}% |",
50 report.earned_stars(),
51 report.possible_stars(),
52 report.completed_days(),
53 report.days.len(),
54 )
55}
56
57fn matrix_header(max_days: usize) -> String {
58 let mut cells = vec!["Year".to_owned()];
59 cells.extend((1..=max_days).map(|day| format!("{day:02}")));
60 format!("| {} |", cells.join(" | "))
61}
62
63fn matrix_row(report: &EditionProgress, max_days: usize) -> String {
64 let mut cells = vec![report.year.to_string()];
65 cells.extend((0..max_days).map(|index| day_cell(report.days.get(index)).to_owned()));
66 format!("| {} |", cells.join(" | "))
67}
68
69fn day_cell(day: Option<&ProgressDayReport>) -> &'static str {
70 let Some(day) = day else {
71 return "—";
72 };
73
74 match (day.local.availability, day.star_one, day.star_two) {
75 (DayAvailability::OutsideEdition, _, _) => "—",
76 (DayAvailability::Locked, _, _) => "L",
77 (_, VerifiedStar::Earned, VerifiedStar::Earned) => "2",
78 (_, VerifiedStar::Earned, VerifiedStar::Missing) => "1",
79 (_, VerifiedStar::Missing, VerifiedStar::Missing) => "0",
80 _ => "?",
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::render;
87 use crate::application::progress::{EditionProgress, LocalProgress, StarSnapshot};
88
89 #[test]
90 fn renders_partial_and_unknown_editions_with_different_lengths() {
91 let local = LocalProgress::default();
92 let snapshot = StarSnapshot::new(2025, 1_000, vec![2, 0, 1]).unwrap();
93 let reports = [
94 EditionProgress::new(2025, None, &local, Some(&snapshot)),
95 EditionProgress::new(2024, None, &local, None),
96 ];
97
98 let markdown = render(&reports);
99
100 assert!(markdown.contains("| 2025 | ≥3/24 | ≥1/12 | ≥13% |"));
101 assert!(markdown.contains("| 2024 | Unknown | Unknown | Unknown |"));
102 assert!(markdown.contains("| Year | 01 | 02 |"));
103 assert!(markdown.contains("| 24 | 25 |"));
104
105 let row = markdown
106 .lines()
107 .find(|line| line.starts_with("| 2025 | 2 |"))
108 .unwrap();
109 let cells: Vec<_> = row.trim_matches('|').split('|').map(str::trim).collect();
110 assert_eq!(&cells[..5], &["2025", "2", "0", "1", "?"]);
111 assert_eq!(cells[12], "?");
112 assert_eq!(cells[13], "—");
113 assert_eq!(cells[25], "—");
114 }
115
116 #[test]
117 fn renders_a_complete_snapshot_with_zero_stars() {
118 let snapshot = StarSnapshot::new(2025, 1_000, vec![0; 12]).unwrap();
119 let report = EditionProgress::new(2025, None, &LocalProgress::default(), Some(&snapshot));
120
121 assert!(render(&[report]).contains("| 2025 | 0/24 | 0/12 | 0% |"));
122 }
123}