Skip to main content

aoc_toolkit/application/
new.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5use super::calendar::CalendarDate;
6use crate::adapters::rust;
7use crate::project::{Language, Project};
8
9pub use crate::scaffold::error::ScaffoldError;
10
11#[derive(Debug)]
12pub struct NewOptions {
13    pub year: usize,
14    pub day: usize,
15}
16
17#[derive(Debug)]
18pub struct NewReport {
19    pub root: PathBuf,
20    pub year: usize,
21    pub day: usize,
22    pub created_files: Vec<PathBuf>,
23    pub updated_files: Vec<PathBuf>,
24}
25
26#[derive(Debug, Error)]
27pub enum NewError {
28    #[error("Invalid Advent of Code year: {0}")]
29    InvalidYear(usize),
30
31    #[error("Invalid Advent of Code day: {0}")]
32    InvalidDay(usize),
33
34    #[error(transparent)]
35    Scaffold(#[from] ScaffoldError),
36
37    #[error("Cannot infer puzzle day; specify --day")]
38    MissingDay,
39}
40
41impl NewOptions {
42    /// Validates the requested year and day.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if either value is outside
47    /// the supported structural range.
48    pub fn validate(&self) -> Result<(), NewError> {
49        if !(2015..=9999).contains(&self.year) {
50            return Err(NewError::InvalidYear(self.year));
51        }
52
53        if !(1..=25).contains(&self.day) {
54            return Err(NewError::InvalidDay(self.day));
55        }
56
57        Ok(())
58    }
59
60    /// Resolves missing year and day from the current `AoC` date.
61    ///
62    /// # Errors
63    ///
64    /// Returns an error if a day cannot be inferred
65    /// or the resolved options are invalid.
66    pub fn resolve(year: Option<usize>, day: Option<usize>) -> Result<Self, NewError> {
67        Self::resolve_at(year, day, CalendarDate::today())
68    }
69
70    fn resolve_at(
71        year: Option<usize>,
72        day: Option<usize>,
73        today: CalendarDate,
74    ) -> Result<Self, NewError> {
75        let year = year.unwrap_or(today.year);
76
77        let day = match day {
78            Some(day) => day,
79            None if year == today.year && today.month == 12 && (1..=25).contains(&today.day) => {
80                today.day
81            }
82            None => return Err(NewError::MissingDay),
83        };
84
85        let options = Self { year, day };
86
87        options.validate()?;
88
89        Ok(options)
90    }
91}
92
93/// Creates and registers a new puzzle solver.
94///
95/// # Errors
96///
97/// Returns an error if the options are invalid,
98/// the registry is modified or the scaffold cannot be applied.
99pub fn execute(project: &Project, options: &NewOptions) -> Result<NewReport, NewError> {
100    options.validate()?;
101
102    let plan = match project.config.project.language {
103        Language::Rust => rust::build_new_plan(project, options.year, options.day)?,
104    };
105
106    let report = plan.apply(&project.root)?;
107
108    Ok(NewReport {
109        root: project.root.clone(),
110        year: options.year,
111        day: options.day,
112        created_files: report.created,
113        updated_files: report.updated,
114    })
115}
116
117#[cfg(test)]
118mod tests;