aoc-toolkit 0.1.0-alpha.2

A toolkit for solving Advent of Code problems
Documentation
use clap::{Args, Subcommand};

use crate::adapters::browser::{
    RegistrationStatus, install_native_host, native_host_status, uninstall_native_host,
};
use crate::cli::error::CliError;

#[derive(Debug, Args)]
pub struct BrowserArgs {
    #[command(subcommand)]
    command: BrowserCommand,
}

#[derive(Debug, Subcommand)]
enum BrowserCommand {
    /// Register the AOC Toolkit native messaging host
    Install {
        /// Browser extension identifier
        #[arg(long)]
        extension_id: String,
    },

    /// Check the native messaging host registration
    Status,

    /// Remove the native messaging host registration
    Uninstall,
}

pub(crate) fn execute(args: &BrowserArgs) -> Result<(), CliError> {
    match &args.command {
        BrowserCommand::Install { extension_id } => {
            let path = install_native_host(extension_id).map_err(CliError::BrowserRegistration)?;

            println!("✓ Native messaging host registered.");
            println!("Manifest: {}", path.display());
        }
        BrowserCommand::Status => {
            match native_host_status().map_err(CliError::BrowserRegistration)? {
                RegistrationStatus::Missing => {
                    println!("Native messaging host is not registered.");
                }
                RegistrationStatus::Registered => {
                    println!("✓ Native messaging host is registered.");
                }
                RegistrationStatus::Stale => {
                    println!("✗ Native messaging registration is stale.");
                }
            }
        }
        BrowserCommand::Uninstall => {
            let removed = uninstall_native_host().map_err(CliError::BrowserRegistration)?;

            if removed {
                println!("✓ Native messaging host unregistered.");
            } else {
                println!("Native messaging host is not registered.");
            }
        }
    }

    Ok(())
}