Skip to main content

aoc_toolkit/application/
run.rs

1use crate::project::{Language, Project};
2use crate::{ClientError, InputStore, adapters};
3use std::process::ExitStatus;
4
5#[derive(Debug, Default, Clone, Copy)]
6pub struct RunOptions {
7    pub year: Option<usize>,
8    pub day: Option<u8>,
9    pub part: Option<u8>,
10    pub all: bool,
11}
12
13/// Executes a project's configured runner.
14///
15/// # Errors
16///
17/// Returns an error if the options are invalid
18/// or the runner process cannot be executed.
19pub fn execute(project: &Project, options: &RunOptions) -> Result<ExitStatus, RunError> {
20    options.validate()?;
21
22    let output = match project.config.project.language {
23        Language::Rust => adapters::rust::plan(project, options)?,
24    };
25
26    if !output.status.success() {
27        return Ok(output.status);
28    }
29
30    let days: Vec<(usize, usize)> =
31        serde_json::from_slice(&output.stdout).map_err(RunError::InvalidPlan)?;
32
33    if days.is_empty()
34        || days
35            .iter()
36            .any(|(year, day)| !(2015..=9999).contains(year) || !(1..=25).contains(day))
37    {
38        return Err(RunError::InvalidPlanDays);
39    }
40
41    let mut inputs = InputStore::global()?;
42
43    for (year, day) in days {
44        inputs.get(year, day)?;
45    }
46
47    match project.config.project.language {
48        Language::Rust => Ok(adapters::rust::run(
49            project,
50            options,
51            inputs.account_root(),
52        )?),
53    }
54}
55
56#[derive(Debug, thiserror::Error)]
57pub enum RunOptionsError {
58    #[error("Day must be between 1 and 25")]
59    InvalidDay,
60
61    #[error("Part must be either 1 or 2")]
62    InvalidPart,
63
64    #[error("--all cannot be combined with --day or --part")]
65    AllConflict,
66
67    #[error("--day is required when using --year with --part")]
68    MissingDay,
69}
70
71impl RunOptions {
72    /// # Errors
73    ///
74    /// Returns an error if the options are invalid.
75    pub fn validate(&self) -> Result<(), RunOptionsError> {
76        if self.day.is_some_and(|day| !(1..=25).contains(&day)) {
77            return Err(RunOptionsError::InvalidDay);
78        }
79
80        if self.part.is_some_and(|part| !(1..=2).contains(&part)) {
81            return Err(RunOptionsError::InvalidPart);
82        }
83
84        if self.all && (self.day.is_some() || self.part.is_some()) {
85            return Err(RunOptionsError::AllConflict);
86        }
87
88        if self.year.is_some() && self.part.is_some() && self.day.is_none() {
89            return Err(RunOptionsError::MissingDay);
90        }
91
92        Ok(())
93    }
94}
95
96#[derive(Debug, thiserror::Error)]
97pub enum RunError {
98    #[error(transparent)]
99    InvalidOptions(#[from] RunOptionsError),
100
101    #[error("Failed to execute runner: {0}")]
102    Process(#[from] std::io::Error),
103
104    #[error("Cannot decode runner execution plan: {0}")]
105    InvalidPlan(#[source] serde_json::Error),
106
107    #[error("Runner returned an invalid execution plan")]
108    InvalidPlanDays,
109
110    #[error(transparent)]
111    Client(#[from] ClientError),
112}