softcopy 1.0.1

Sibling project to 'hardcopy'; used to convert back into keys/certs/etc.
Documentation
use clap::{Arg, ArgMatches};
use log::LevelFilter;

/// Generate the set of arguments that the SoftCopy CLI can handle.
pub fn configs<'a>() -> Vec<Arg<'a, 'a>> {
    vec![
        Arg::with_name("from")
            .long("from")
            .takes_value(true)
            .value_name("PLUGIN")
            .help("Plugin used to decode the hard-copy (e.g. plugins.pgpfone)."),
        Arg::with_name("output")
            .short("o")
            .long("output")
            .takes_value(true)
            .value_name("FILE")
            .help("Output file path for the reconstructed data (decode mode)."),
        Arg::with_name("verify")
            .long("verify")
            .help("Validate a reconstructed file against a checksum from the hard-copy."),
        Arg::with_name("debug")
            .long("debug")
            .help("Enable debug mode to emit more details."),
        Arg::with_name("FILE")
            .index(1)
            .value_name("FILE")
            .help("Output file (decode mode) or file to verify (--verify mode)."),
    ]
}

/// Internal configuration for SoftCopy loaded from command-line options.
pub struct SoftCopyConfig {
    /// Plugin used for decoding (e.g. `"plugins.pgpfone"`).
    pub plugin: Option<String>,
    /// Output file path supplied via `-o`.
    pub output: Option<String>,
    /// Positional file argument (decode output or verify target).
    pub file: Option<String>,
    /// When true, verify a reconstructed file against a checksum.
    pub verify: bool,
    /// Enable debug-level logging.
    pub debug: bool,
}

impl SoftCopyConfig {
    /// Returns the log level filter for this configuration.
    pub fn get_log_level(&self) -> LevelFilter {
        if self.debug {
            LevelFilter::Debug
        } else {
            LevelFilter::Warn
        }
    }
}

impl<'a> From<ArgMatches<'a>> for SoftCopyConfig {
    fn from(matches: ArgMatches<'a>) -> Self {
        SoftCopyConfig {
            plugin: matches.value_of("from").map(str::to_string),
            output: matches.value_of("output").map(str::to_string),
            file: matches.value_of("FILE").map(str::to_string),
            verify: matches.is_present("verify"),
            debug: matches.is_present("debug"),
        }
    }
}