novel-cli 0.17.0

A set of tools for downloading novels from the web, manipulating text, and generating EPUB
Documentation
use std::io;

use clap::{Args, CommandFactory, ValueEnum};
use clap_complete::Generator;
use color_eyre::eyre::Result;
use fluent_templates::Loader;

use crate::config::Config;
use crate::{LANG_ID, LOCALES};

#[must_use]
#[derive(Clone, ValueEnum)]
pub enum Shell {
    Bash,
    Elvish,
    Fish,
    PowerShell,
    Zsh,
    Nushell,
}

impl Shell {
    fn to_clap_type(&self) -> Box<dyn Generator> {
        match self {
            Self::Bash => Box::new(clap_complete::Shell::Bash),
            Self::Elvish => Box::new(clap_complete::Shell::Elvish),
            Self::Fish => Box::new(clap_complete::Shell::Fish),
            Self::PowerShell => Box::new(clap_complete::Shell::PowerShell),
            Self::Zsh => Box::new(clap_complete::Shell::Zsh),
            Self::Nushell => Box::new(clap_complete_nushell::Nushell),
        }
    }
}

#[must_use]
#[derive(Args)]
#[command(arg_required_else_help = true,
    about = LOCALES.lookup(&LANG_ID, "completions_command"))]
pub struct Completions {
    #[arg(value_enum,
        help = LOCALES.lookup(&LANG_ID, "shell"))]
    pub shell: Shell,
}

pub fn execute(config: Completions) -> Result<()> {
    let mut cmd = Config::command();
    let bin_name = cmd.get_name().to_string();

    cmd.set_bin_name(bin_name);
    cmd.build();

    config
        .shell
        .to_clap_type()
        .generate(&cmd, &mut io::stdout());

    Ok(())
}