hardcopy 1.0.1

Generate hard copies of keys, certs and secrets.
Documentation
use clap::{Arg, ArgMatches};
use log::LevelFilter;

fn build_output_toggles<'a>() -> Vec<Arg<'a, 'a>> {
    let mut toggles = Vec::new();

    if cfg!(feature = "out-pgpfone") {
        toggles.push(
            Arg::with_name("no-pgpfone")
                .long("no-pgpfone")
                .help("Disable emission of PGPfone mnemonic words."),
        );
    }

    if cfg!(feature = "out-skeyotp") {
        toggles.push(
            Arg::with_name("no-skeyotp")
                .long("no-skeyotp")
                .help("Disable emission of S/KEY OTP mnemonic words."),
        );
    }

    if cfg!(feature = "out-qrcode") {
        toggles.push(
            Arg::with_name("no-qrcode")
                .long("no-qrcode")
                .help("Disable emission of QR codes."),
        );
    }

    if cfg!(feature = "out-bip39") {
        toggles.push(
            Arg::with_name("no-bip39")
                .long("no-bip39")
                .help("Disable emission of BIP-39 mnemonic words."),
        );
    }

    if cfg!(feature = "out-diceware") {
        toggles.push(
            Arg::with_name("no-diceware")
                .long("no-diceware")
                .help("Disable emission of EFF Large Diceware mnemonic words."),
        );
    }

    toggles
}

/// Generate the set of arguments that the HardCopy CLI can handle based on what
/// features are enabled.
pub fn configs<'a>() -> Vec<Arg<'a, 'a>> {
    let mut args = build_output_toggles();
    args.push(
        Arg::with_name("input")
            .short("i")
            .long("input")
            .takes_value(true)
            .value_name("FILE")
            .help("Input file to encode (default: stdin)."),
    );
    args.push(
        Arg::with_name("font-dir")
            .long("font-dir")
            .takes_value(true)
            .value_name("DIR")
            .help("Directory containing a monospace font family for PDF output."),
    );
    args.push(
        Arg::with_name("font-name")
            .long("font-name")
            .takes_value(true)
            .value_name("NAME")
            .help("Font family name to load for PDF output (e.g. ProggyCleanNerdFontMono)."),
    );
    args.push(
        Arg::with_name("template")
            .long("template")
            .takes_value(true)
            .value_name("FILE")
            .help("HTML template file to use for .html output (see doc/html-templates.md)."),
    );
    args.push(
        Arg::with_name("debug")
            .long("debug")
            .help("Enable debug mode to emit more details."),
    );
    args.push(
        Arg::with_name("FILE_NAME")
            .required(true)
            .index(1)
            .help("The output path/file; extension determines format (.txt or .pdf)."),
    );
    args
}

/// Internal configuration for HardCopy loaded from command-line options.
pub struct HardCopyConfig {
    debug: bool,
    /// Output file path; extension determines format (`.txt` or `.pdf`).
    pub out_file: String,
    /// Optional input file path; reads from stdin when absent.
    pub input_file: Option<String>,
    /// Emit PGPfone mnemonic words section when true.
    pub pgpfone_enabled: bool,
    /// Emit S/KEY OTP mnemonic words section when true.
    pub skeyotp_enabled: bool,
    /// Embed QR code section when true.
    pub qrcode_enabled: bool,
    /// Emit BIP-39 mnemonic words section when true.
    pub bip39_enabled: bool,
    /// Emit EFF Large Diceware mnemonic words section when true.
    pub diceware_enabled: bool,
    /// Directory to probe for a monospace font family (PDF output).
    pub font_dir: Option<String>,
    /// Font family name to load for PDF output.
    pub font_name: Option<String>,
    /// HTML template file path (HTML output only).
    pub html_template: Option<String>,
}

impl HardCopyConfig {
    /// Check what the log level looks like.
    pub fn get_log_level(&self) -> LevelFilter {
        // This is valid in 1.50, not now.
        //self.debug.then_some(LevelFilter::Debug).unwrap_or(LevelFilter::Warn)
        if self.debug {
            LevelFilter::Debug
        } else {
            LevelFilter::Warn
        }
    }
}

impl<'a> From<ArgMatches<'a>> for HardCopyConfig {
    fn from(matches: ArgMatches<'a>) -> Self {
        let debug = matches.is_present("debug");
        let out_file = matches
            .value_of("FILE_NAME")
            .expect("Output file name is required, clap should enforce this.")
            .to_string();
        let input_file = matches.value_of("input").map(str::to_string);
        let font_dir = matches.value_of("font-dir").map(str::to_string);
        let font_name = matches.value_of("font-name").map(str::to_string);
        let pgpfone_enabled = !matches.is_present("no-pgpfone");
        let skeyotp_enabled = !matches.is_present("no-skeyotp");
        let qrcode_enabled = !matches.is_present("no-qrcode");
        let bip39_enabled = !matches.is_present("no-bip39");
        let diceware_enabled = !matches.is_present("no-diceware");
        let html_template = matches.value_of("template").map(str::to_string);
        HardCopyConfig {
            debug,
            out_file,
            input_file,
            pgpfone_enabled,
            skeyotp_enabled,
            qrcode_enabled,
            bip39_enabled,
            diceware_enabled,
            font_dir,
            font_name,
            html_template,
        }
    }
}