use std::fs;
use std::io::{self, Read, Write};
use std::path::PathBuf;
use aozora_core::zip::{is_zip_file, read_first_txt_from_zip};
use clap::Args as ClapArgs;
use aozora2::strip;
#[derive(ClapArgs, Debug)]
pub struct Args {
pub input: Option<PathBuf>,
#[arg(short, long)]
pub output: Option<PathBuf>,
#[arg(short, long)]
pub zip: bool,
}
pub fn run(args: Args) -> io::Result<()> {
let bytes = if args.zip {
let path = args.input.as_ref().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"ZIP mode requires an input file",
)
})?;
read_first_txt_from_zip(path)?
} else {
match &args.input {
Some(path) => {
let bytes = fs::read(path)?;
if is_zip_file(&bytes) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"input appears to be a ZIP file; use --zip option",
));
}
bytes
}
None => {
let mut buf = Vec::new();
io::stdin().read_to_end(&mut buf)?;
buf
}
}
};
let output = strip::convert(&bytes);
match &args.output {
Some(path) => fs::write(path, &output)?,
None => io::stdout().write_all(output.as_bytes())?,
}
Ok(())
}