libhardcopy 1.0.1

Generalized library for hardcopy/softcopy command line utilities.
Documentation
/// PGPfone defines a "radio alphabet" for conveying bytes verbally
/// over a noisy phone line. The words were chosen to reduce
/// "confusibility". This is advantageous in this case (despite not
/// using it over a phone line), as we want the words to be distinct
/// enough that even with media degradation (paper is torn, etc), the
/// word choices are straight-forward.
///
/// For Hard-Copy generation, there are two modes; simple and verbose.
/// In 'verbose' mode, the wordlists themselves and documentation on
/// how to reconstruct the data from the wordlists is included in the
/// output.
///
///
/// References
/// * Whole-Word Phonetic Distances and the PGPfone Alphabet
///     http://www.asel.udel.edu/icslp/cdrom/vol1/005/a005.pdf
///
mod algo;

use crate::plugins::pgpfone::algo::{bytes_to_wordlist, max_word_len, wordlist_to_bytes};
use crate::plugins::{GenerationError, HardCopyGenerator, LibPlugin, SoftCopyGenerator};
use crate::{
    HardCopy, HardCopyInput, HardCopySection, HardCopySectionContent, SoftCopy, SoftCopyInput,
};

const WORDS_PER_LINE: usize = 4;

pub struct PGPfonePlugin;

const PLUGIN_NAME: &'static str = "PGPfone";
const PLUGIN_DESCRIPTION: &'static str = "Wordlist generation using PGP Words.";

// Basic instructions on the wordlist usage.
pub const PGPFONE_INSTRUCTIONS: &'static str =
    include_str!("../../../static/wordlists/pgpfone/instructions.txt");

impl LibPlugin for PGPfonePlugin {
    fn name(&self) -> String {
        PLUGIN_NAME.to_string()
    }
    fn description(&self) -> String {
        PLUGIN_DESCRIPTION.to_string()
    }
    fn instructions(&self) -> String {
        PGPFONE_INSTRUCTIONS.to_string()
    }
}

impl HardCopyGenerator for PGPfonePlugin {
    fn generate_hard_copy(&self, softinput: &SoftCopyInput) -> Result<HardCopy, GenerationError> {
        let words = bytes_to_wordlist(softinput.to_bytes());
        let col_width = max_word_len();
        let lines: Vec<String> = words
            .chunks(WORDS_PER_LINE)
            .enumerate()
            .map(|(i, chunk)| {
                let padded: Vec<String> = chunk
                    .iter()
                    .map(|w| format!("{:width$}", w, width = col_width))
                    .collect();
                format!("{:04}: {}", i + 1, padded.join(" "))
            })
            .collect();
        let mut hardcopy = HardCopy::new();
        hardcopy.push(HardCopySection {
            name: PLUGIN_NAME.to_string(),
            content: HardCopySectionContent::Words(lines),
        });
        Ok(hardcopy)
    }
}

impl SoftCopyGenerator for PGPfonePlugin {
    fn generate_soft_copy(&self, input: &mut HardCopyInput) -> Result<SoftCopy, GenerationError> {
        let word_refs: Vec<&str> = input.words().iter().map(String::as_str).collect();
        let bytes = wordlist_to_bytes(&word_refs);
        Ok(SoftCopy { bytes })
    }
}

#[cfg(test)]
mod test {
    use super::PGPFONE_INSTRUCTIONS;

    #[test]
    fn validate_instructions_non_empty() {
        assert!(
            !PGPFONE_INSTRUCTIONS.is_empty(),
            "Verify the instructions are not empty."
        );
    }
}