aoc-toolkit 0.1.0-alpha.4

A toolkit for solving Advent of Code problems
Documentation
use std::time::Duration;

use crate::core::PartResult;
use crate::runner::error::DayError;

pub struct DayReport {
    pub year: usize,
    pub day: usize,
    pub completed: Vec<PartResult>,
    pub outcome: Result<(), DayError>,
}

pub struct ExecutionReport {
    pub days: Vec<DayReport>,
}

impl ExecutionReport {
    pub fn total_days(&self) -> usize {
        self.days.len()
    }

    pub fn successful_days(&self) -> usize {
        self.days.iter().filter(|day| day.outcome.is_ok()).count()
    }

    pub fn failed_days(&self) -> usize {
        self.days.iter().filter(|day| day.outcome.is_err()).count()
    }

    pub fn is_success(&self) -> bool {
        self.days.iter().all(|day| day.outcome.is_ok())
    }

    pub fn total_duration(&self) -> Duration {
        self.days
            .iter()
            .flat_map(|day| day.completed.iter())
            .map(|part| part.duration)
            .sum()
    }

    pub(crate) fn write_progress_report(&self, path: &std::path::Path) -> std::io::Result<()> {
        use std::fs::File;
        use std::io::Write;

        use serde_json::json;

        let days: Vec<_> = self
            .days
            .iter()
            .map(|day| {
                let mut parts: Vec<_> = day
                    .completed
                    .iter()
                    .map(|result| json!({ "part": result.part as u8, "status": "succeeded" }))
                    .collect();

                let day_error = match &day.outcome {
                    Ok(()) => None,
                    Err(DayError::Input(_)) => Some("input"),
                    Err(DayError::Panic(_)) => Some("panic"),
                    Err(DayError::PartPanic { part, .. }) => {
                        parts.push(json!({ "part": *part as u8, "status": "panicked" }));
                        None
                    }
                };

                json!({
                    "year": day.year,
                    "day": day.day,
                    "parts": parts,
                    "day_error": day_error,
                })
            })
            .collect();

        let report = json!({ "schema_version": 1, "days": days });
        File::create_new(path)?.write_all(report.to_string().as_bytes())
    }
}