Skip to main content

aoc_toolkit/application/
fetch.rs

1use thiserror::Error;
2
3use super::calendar::{CalendarDate, edition_days};
4use crate::{ClientError, InputSource, InputStore};
5
6#[derive(Debug, Default, Clone, Copy)]
7pub struct FetchOptions {
8    pub year: Option<usize>,
9    pub day: Option<usize>,
10    pub all: bool,
11}
12
13#[derive(Debug)]
14pub struct FetchReport {
15    pub year: usize,
16    pub day: usize,
17    pub outcome: Result<InputSource, ClientError>,
18}
19
20#[derive(Debug, Error)]
21pub enum FetchError {
22    #[error("Invalid Advent of Code year: {0}")]
23    InvalidYear(usize),
24
25    #[error("Invalid Advent of Code day: {0}")]
26    InvalidDay(usize),
27
28    #[error("--all cannot be combined with --day")]
29    AllConflict,
30
31    #[error("No puzzles are unlocked for {0}")]
32    NoUnlockedPuzzles(usize),
33
34    #[error("Puzzle {year} / Day {day:02} is not unlocked")]
35    PuzzleLocked { year: usize, day: usize },
36
37    #[error(transparent)]
38    Client(#[from] ClientError),
39}
40
41impl FetchOptions {
42    fn resolve(&self, today: CalendarDate) -> Result<Vec<(usize, usize)>, FetchError> {
43        if self.all && self.day.is_some() {
44            return Err(FetchError::AllConflict);
45        }
46
47        let year = self.year.unwrap_or(today.year);
48
49        if !(2015..=today.year).contains(&year) {
50            return Err(FetchError::InvalidYear(year));
51        }
52
53        if let Some(day) = self.day
54            && !(1..=edition_days(year)).contains(&day)
55        {
56            return Err(FetchError::InvalidDay(day));
57        }
58
59        let unlocked = today.unlocked_days(year);
60
61        if unlocked == 0 {
62            return Err(FetchError::NoUnlockedPuzzles(year));
63        }
64
65        if self.all {
66            return Ok((1..=unlocked).map(|day| (year, day)).collect());
67        }
68
69        let day = self.day.unwrap_or(unlocked);
70
71        if day > unlocked {
72            return Err(FetchError::PuzzleLocked { year, day });
73        }
74
75        Ok(vec![(year, day)])
76    }
77}
78
79/// Fetches the requested puzzle inputs.
80///
81/// Stops after the first acquisition error and returns
82/// reports for all attempted puzzles.
83///
84/// # Errors
85///
86/// Returns an error if the requested puzzle selection
87/// is invalid or the input store cannot be initialized.
88pub fn execute(options: &FetchOptions) -> Result<Vec<FetchReport>, FetchError> {
89    let days = options.resolve(CalendarDate::today())?;
90
91    let mut store = InputStore::global()?;
92    let mut reports = Vec::with_capacity(days.len());
93
94    for (year, day) in days {
95        let outcome = store.get_with_source(year, day).map(|(_, source)| source);
96
97        let failed = outcome.is_err();
98
99        reports.push(FetchReport { year, day, outcome });
100
101        if failed {
102            break;
103        }
104    }
105
106    Ok(reports)
107}
108
109#[cfg(test)]
110mod tests;