quicklatex 0.1.0

A program to help me write LaTeX quickly
Documentation
#![warn(
    clippy::all,
    clippy::pedantic,
    clippy::nursery,
    //clippy::cargo_common_metadata
)]
// Anachronism
#![allow(clippy::non_ascii_literal)]
// More or less manual checked and documentation agrees with me that
// it's usually not needed.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_lossless
)]

use std::{env::args_os, ffi::OsString, fs::File, io::Write, path::PathBuf, process::Command, str::FromStr};

use anyhow::{bail, Context, Error};
use diskit::StdDiskit;
use tempdir::TempDir;

use quicklatex::{arguments::parse_args, Timers};

fn main() -> Result<(), Error>
{
    let mut timers = Timers::default();

    timers.start("main")?;

    let arguments = parse_args(args_os()).context("Usage: quicklatex [OPTIONS] FILE [COUNT]")?;

    let (s, noisy) =
        quicklatex::run(&arguments.path, &mut timers, StdDiskit).context("Couldn't transform the qlx to tex")?;

    noisy.print();

    let tmpdir = TempDir::new("quicklatex").context("Couldn't create temporary directory")?;
    let part_of_path = arguments
        .path
        .file_name()
        .map_or_else(|| OsString::from("file.tex"), ToOwned::to_owned);
    let mut output_path = arguments
        .path
        .parent()
        .map_or_else(|| PathBuf::from_str(".").unwrap(), ToOwned::to_owned);
    /*.join(path.file_name().map_or_else(
            || OsString::from("file.pdf"),
            |file_name| {
                let mut file_name = PathBuf::from(file_name)
                    .file_stem()
                    .map_or_else(|| OsString::from("file"), ToOwned::to_owned);
                file_name.extend([&*OsString::from(".pdf")]);
                file_name
            },
    ));*/
    let tmp_path = tmpdir.path().join(part_of_path);

    if output_path.as_os_str().is_empty()
    {
        output_path = PathBuf::from_str(".").unwrap();
    }

    File::create(&tmp_path)
        .context("Couldn't open temporary file")?
        .write_all(s.as_bytes())
        .context("Couldn't write to temporary file")?;

    eprintln!("{s}");

    timers.start("pdflatex")?;

    for _ in 0..arguments.count
    {
        if !Command::new("pdflatex")
            .arg("-halt-on-error")
            .arg(format!("-output-directory={}", output_path.to_string_lossy().clone()))
            .arg(&tmp_path)
            .status()
            .context("Couldn't run pdflatex")?
            .success()
        {
            bail!("Problem when running pdflatex");
        }
    }

    timers.stop("pdflatex")?;

    noisy.print();

    timers.stop("main")?;

    if arguments.should_profile
    {
        timers.print_all();
    }

    Ok(())
}