adblock-list-compiler 0.0.8

CLI tool to compile multiple adblock list sources into a single file
Documentation
use std::path::PathBuf;

use clap::{Parser, Subcommand};

use crate::{
    cli_run::{compile::Compile, config_check::ConfigCheck, CliRun},
    config::ConfigUrl,
};

#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
    /// Outputs the current config
    Check {
        /// Sets a custom config file
        #[arg(
            short,
            long,
            value_name = "CONFIG",
            default_value = "https://raw.githubusercontent.com/ragibkl/adblock-dns-server/master/data/configuration.yaml"
        )]
        config_url: ConfigUrl,
    },
}

#[derive(Debug, Subcommand)]
pub enum Command {
    #[command(subcommand)]
    Config(ConfigCommand),

    // Compiles an adblock list and output to file
    Compile {
        /// Sets a custom config file
        #[arg(
            short,
            long,
            value_name = "CONFIG",
            default_value = "https://raw.githubusercontent.com/ragibkl/adblock-dns-server/master/data/configuration.yaml"
        )]
        config_url: ConfigUrl,

        /// output file location
        #[arg(short, long, value_name = "OUTPUT", default_value = "./blacklist.zone")]
        output: PathBuf,

        /// output format
        #[arg(short, long, value_name = "FORMAT", default_value = "zone")]
        format: String,
    },
}

#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

impl Cli {
    pub fn from_args() -> Self {
        Self::parse()
    }

    pub fn into_cli_run(self) -> Box<dyn CliRun> {
        match &self.command {
            Command::Config(config_cmd) => match config_cmd {
                ConfigCommand::Check { config_url } => Box::new(ConfigCheck::new(config_url)),
            },
            Command::Compile {
                config_url,
                output,
                format,
            } => Box::new(Compile::new(config_url, output, format)),
        }
    }
}