Skip to main content

aoc_toolkit/application/
run.rs

1use crate::adapters;
2use crate::project::{Language, Project};
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    match project.config.project.language {
23        Language::Rust => Ok(adapters::rust::run(project, options)?),
24    }
25}
26
27#[derive(Debug, thiserror::Error)]
28pub enum RunOptionsError {
29    #[error("Day must be between 1 and 25")]
30    InvalidDay,
31
32    #[error("Part must be either 1 or 2")]
33    InvalidPart,
34
35    #[error("--all cannot be combined with --day or --part")]
36    AllConflict,
37
38    #[error("--day is required when using --year with --part")]
39    MissingDay,
40}
41
42impl RunOptions {
43    /// # Errors
44    ///
45    /// Returns an error if the options are invalid.
46    pub fn validate(&self) -> Result<(), RunOptionsError> {
47        if self.day.is_some_and(|day| !(1..=25).contains(&day)) {
48            return Err(RunOptionsError::InvalidDay);
49        }
50
51        if self.part.is_some_and(|part| !(1..=2).contains(&part)) {
52            return Err(RunOptionsError::InvalidPart);
53        }
54
55        if self.all && (self.day.is_some() || self.part.is_some()) {
56            return Err(RunOptionsError::AllConflict);
57        }
58
59        if self.year.is_some() && self.part.is_some() && self.day.is_none() {
60            return Err(RunOptionsError::MissingDay);
61        }
62
63        Ok(())
64    }
65}
66
67#[derive(Debug, thiserror::Error)]
68pub enum RunError {
69    #[error(transparent)]
70    InvalidOptions(#[from] RunOptionsError),
71
72    #[error("Failed to execute runner: {0}")]
73    Process(#[from] std::io::Error),
74}