aoc-toolkit 0.1.0-alpha.4

A toolkit for solving Advent of Code problems
Documentation
use crate::runner::commands::result::CommandOutput;
use crate::runner::commands::run::Run;
use crate::runner::commands::{Command, CommandResult};
use crate::runner::context::CommandContext;
use clap::{CommandFactory, Parser, Subcommand};

#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

impl Cli {
    pub fn parse_validated() -> Self {
        let cli = Self::parse();

        let result = match &cli.command {
            Commands::Run(command) => command.validate(),
            Commands::Registry => Ok(()),
        };

        if let Err(error) = result {
            error.format(&mut Self::command()).exit();
        }

        cli
    }
}

#[derive(Subcommand)]
pub enum Commands {
    /// Runs the specified Advent of Code solution
    Run(Run),

    #[command(hide = true)]
    Registry,
}

impl Command for Commands {
    fn execute(self, ctx: &CommandContext<'_>) -> CommandResult {
        match self {
            Self::Run(command) => command.execute(ctx),
            Self::Registry => {
                let mut days: Vec<_> = ctx.registry.iter().map(|day| (day.year, day.day)).collect();
                days.sort_unstable();
                days.dedup();

                Ok(CommandOutput::Plan(days))
            }
        }
    }
}