aoc-toolkit 0.1.0-alpha.4

A toolkit for solving Advent of Code problems
Documentation
use crate::application::progress::{
    DayAvailability, EditionProgress, ProgressDayReport, VerifiedStar,
};

/// Renders a Markdown summary and daily matrix for the supplied editions.
#[must_use]
pub fn render(reports: &[EditionProgress]) -> String {
    let mut lines = vec![
        "### Advent of Code progress".to_owned(),
        String::new(),
        "| Year | Stars | Completed days | Completion |".to_owned(),
        "| ---: | ---: | ---: | ---: |".to_owned(),
    ];
    lines.extend(reports.iter().map(summary_row));

    let max_days = reports
        .iter()
        .map(|report| report.days.len())
        .max()
        .unwrap_or(0);
    if max_days > 0 {
        lines.push(String::new());
        lines.push(matrix_header(max_days));
        lines.push(format!("| ---: |{}", " :---: |".repeat(max_days)));
        lines.extend(reports.iter().map(|report| matrix_row(report, max_days)));
        lines.push(String::new());
        lines.push(
            "`?` unknown · `L` locked · `—` outside the edition · `≥` partial snapshot".to_owned(),
        );
    }

    lines.push(String::new());
    lines.join("\n")
}

fn summary_row(report: &EditionProgress) -> String {
    let year = report.year;
    let Some(percent) = report.completion_percent() else {
        return format!("| {year} | Unknown | Unknown | Unknown |");
    };

    let prefix = if report.has_complete_star_coverage() {
        ""
    } else {
        "≥"
    };

    format!(
        "| {year} | {prefix}{}/{} | {prefix}{}/{} | {prefix}{percent}% |",
        report.earned_stars(),
        report.possible_stars(),
        report.completed_days(),
        report.days.len(),
    )
}

fn matrix_header(max_days: usize) -> String {
    let mut cells = vec!["Year".to_owned()];
    cells.extend((1..=max_days).map(|day| format!("{day:02}")));
    format!("| {} |", cells.join(" | "))
}

fn matrix_row(report: &EditionProgress, max_days: usize) -> String {
    let mut cells = vec![report.year.to_string()];
    cells.extend((0..max_days).map(|index| day_cell(report.days.get(index)).to_owned()));
    format!("| {} |", cells.join(" | "))
}

fn day_cell(day: Option<&ProgressDayReport>) -> &'static str {
    let Some(day) = day else {
        return "—";
    };

    match (day.local.availability, day.star_one, day.star_two) {
        (DayAvailability::OutsideEdition, _, _) => "—",
        (DayAvailability::Locked, _, _) => "L",
        (_, VerifiedStar::Earned, VerifiedStar::Earned) => "2",
        (_, VerifiedStar::Earned, VerifiedStar::Missing) => "1",
        (_, VerifiedStar::Missing, VerifiedStar::Missing) => "0",
        _ => "?",
    }
}

#[cfg(test)]
mod tests {
    use super::render;
    use crate::application::progress::{EditionProgress, LocalProgress, StarSnapshot};

    #[test]
    fn renders_partial_and_unknown_editions_with_different_lengths() {
        let local = LocalProgress::default();
        let snapshot = StarSnapshot::new(2025, 1_000, vec![2, 0, 1]).unwrap();
        let reports = [
            EditionProgress::new(2025, None, &local, Some(&snapshot)),
            EditionProgress::new(2024, None, &local, None),
        ];

        let markdown = render(&reports);

        assert!(markdown.contains("| 2025 | ≥3/24 | ≥1/12 | ≥13% |"));
        assert!(markdown.contains("| 2024 | Unknown | Unknown | Unknown |"));
        assert!(markdown.contains("| Year | 01 | 02 |"));
        assert!(markdown.contains("| 24 | 25 |"));

        let row = markdown
            .lines()
            .find(|line| line.starts_with("| 2025 | 2 |"))
            .unwrap();
        let cells: Vec<_> = row.trim_matches('|').split('|').map(str::trim).collect();
        assert_eq!(&cells[..5], &["2025", "2", "0", "1", "?"]);
        assert_eq!(cells[12], "?");
        assert_eq!(cells[13], "—");
        assert_eq!(cells[25], "—");
    }

    #[test]
    fn renders_a_complete_snapshot_with_zero_stars() {
        let snapshot = StarSnapshot::new(2025, 1_000, vec![0; 12]).unwrap();
        let report = EditionProgress::new(2025, None, &LocalProgress::default(), Some(&snapshot));

        assert!(render(&[report]).contains("| 2025 | 0/24 | 0/12 | 0% |"));
    }
}