#![allow(unused)]
use clap::Parser;
use marksonnet;
use pulldown_cmark_to_cmark::cmark;
use std::error::Error;
use std::fs;
use std::io;
use std::io::BufWriter;
use std::path::PathBuf;
use std::process;
use std::result;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct CLI {
source: Option<String>,
#[arg(short, long)]
output: Option<String>,
#[arg(short = 'J', long, action = clap::ArgAction::Append)]
jpaths: Vec<PathBuf>,
}
impl CLI {
fn source(&self) -> io::Result<String> {
match &self.source {
Some(path) => Ok(fs::read_to_string(path)?),
None => Ok(io::read_to_string(io::stdin())?),
}
}
fn output(&self) -> io::Result<Box<dyn io::Write>> {
match &self.output {
Some(path) => Ok(Box::new(fs::File::create(path)?)),
None => Ok(Box::new(io::stdout())),
}
}
}
fn main() {
let mut cli = CLI::parse();
if let Err(e) = run(&mut cli) {
eprintln!("Application error: {e}");
process::exit(1);
}
}
fn run(cli: &mut CLI) -> Result<(), Box<dyn Error>> {
let source = cli.source()?;
let mut output = cli.output()?;
let parser = marksonnet::Parser::new(&source).with_library_paths(&cli.jpaths);
let mut result = String::new();
cmark(parser, &mut result)?;
output.write(&result.into_bytes())?;
Ok(())
}