mod path_arg;
use crate::path_arg::PathArg;
use clap::{Args, Parser};
use itertools::Itertools;
use std::io::Write;
use tohaya::{CitationFormat, tohaya};
#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[command(flatten)]
format: FormatArg,
#[clap(short, long)]
append: bool,
#[clap(short, long, default_value = "-")]
output_file: PathArg,
#[clap(required = true)]
input_files: Vec<PathArg>,
}
#[derive(Args, Copy, Clone)]
#[group(required = false, multiple = false)]
struct FormatArg {
#[clap(short, long)]
bibtex: bool,
#[clap(short, long)]
pubmed: bool,
}
impl FormatArg {
fn resolve(self) -> Option<CitationFormat> {
if self.bibtex {
Some(CitationFormat::Bibtex)
} else if self.pubmed {
Some(CitationFormat::Pubmed)
} else {
None
}
}
}
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let args = Cli::parse();
let mut out = args
.output_file
.open(args.append)
.map(std::io::BufWriter::new)?;
let inputs: Vec<_> = args
.input_files
.into_iter()
.map(|s| s.read())
.try_collect()?;
let output = tohaya(inputs, args.format.resolve())?;
out.write_all(output.as_ref())?;
Ok(())
}