use crate::*;
use anyhow::Context;
use std::io::{stdin, Read};
pub struct HardCopyInput {
raw: Vec<u8>,
}
impl HardCopyInput {
pub fn from_config(config: &HardCopyConfig) -> Result<HardCopyInput> {
match config.input_file.as_deref() {
Some(path) => HardCopyInput::from_file(path),
None => HardCopyInput::from_stdin(),
}
}
fn from_stdin() -> Result<HardCopyInput> {
let mut raw = Vec::new();
stdin()
.lock()
.read_to_end(&mut raw)
.context("Failed to read from stdin.")?;
Ok(HardCopyInput { raw })
}
fn from_file(path: &str) -> Result<HardCopyInput> {
let raw = std::fs::read(path)
.with_context(|| format!("Failed to read input file '{}'.", path))?;
Ok(HardCopyInput { raw })
}
pub fn as_bytes(&self) -> &[u8] {
&self.raw
}
}