libhardcopy 1.0.1

Generalized library for hardcopy/softcopy command line utilities.
Documentation
mod input;
pub mod plugins;

/// A single named section of a hard-copy document: either a block of
/// mnemonic words or raw bytes (e.g. a QR code image).
pub enum HardCopySectionContent {
    /// Lines of mnemonic words produced by an encoding plugin.
    Words(Vec<String>),
    /// Raw bytes, e.g. a QR code PNG.
    Bytes(Vec<u8>),
}

/// A labeled section within a HardCopy document.
pub struct HardCopySection {
    pub name: String,
    pub content: HardCopySectionContent,
}

/// A hard-copy document ready to be formatted and written to disk.
pub struct HardCopy {
    pub sections: Vec<HardCopySection>,
}

impl HardCopy {
    pub fn new() -> Self {
        HardCopy {
            sections: Vec::new(),
        }
    }

    pub fn push(&mut self, section: HardCopySection) {
        self.sections.push(section);
    }
}

/// The result of soft-copy reconstruction: the recovered raw bytes.
pub struct SoftCopy {
    pub bytes: Vec<u8>,
}

/// A hard-copy being entered by the user, accumulating word input
/// before it can be decoded into a SoftCopy.
pub struct HardCopyInput {
    words: Vec<String>,
}

impl HardCopyInput {
    pub fn new() -> Self {
        HardCopyInput { words: Vec::new() }
    }

    /// Accept one line of user input, stripping an optional `NNNN: ` line-number
    /// prefix (as printed by the TXT/PDF formatter), then appending each
    /// whitespace-separated word to the internal word list.
    pub fn push_line(&mut self, line: &str) {
        let words_part = match line.find(": ") {
            Some(idx) => &line[idx + 2..],
            None => line,
        };
        for word in words_part.split_whitespace() {
            self.words.push(word.to_string());
        }
    }

    /// All words accumulated so far.
    pub fn words(&self) -> &[String] {
        &self.words
    }
}

/// An input file or set of files whose content needs to be encoded
/// into a hard-copy document.
pub struct SoftCopyInput {
    bytes: Vec<u8>,
}

impl SoftCopyInput {
    pub fn from_bytes(bytes: Vec<u8>) -> Self {
        SoftCopyInput { bytes }
    }

    pub fn to_bytes(&self) -> &Vec<u8> {
        &self.bytes
    }
}