148 lines
3.9 KiB
Rust
148 lines
3.9 KiB
Rust
//! Make a pdf using pdflatex
|
|
|
|
// use std::fmt::Display;
|
|
use std::str;
|
|
use tokio::io::{AsyncBufReadExt, BufReader};
|
|
use tokio::process::Command;
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use clap::Args;
|
|
use std::process::Stdio;
|
|
|
|
use crate::Config;
|
|
|
|
#[derive(Args, Debug, Clone)]
|
|
/// The configuration struct for the PdfMaker
|
|
pub(crate) struct MakeConfig {
|
|
/// The filename to compile
|
|
pub(crate) filename: String,
|
|
/// The verbosity level
|
|
#[arg(short, long, default_value_t = false)]
|
|
pub(crate) verbosity: bool,
|
|
}
|
|
|
|
/// Make a pdf spawning a subprocess
|
|
pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> {
|
|
let _ = config;
|
|
launch_subprocess(args).unwrap();
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
/// The current status of a run, holding all final info
|
|
struct Status {
|
|
/// Number of encountered info (mainly o/ufull)
|
|
info: usize,
|
|
/// Number of warnings, mainly label/reference rerun
|
|
warnings: usize,
|
|
/// Only makes sense with non-interactie builds
|
|
errors: usize,
|
|
/// List of all recoverable
|
|
v_errors: Vec<String>,
|
|
}
|
|
|
|
impl Status {
|
|
/// Add a recoverable error to the list of errors
|
|
fn add_error(&mut self, error_msg: String) {
|
|
self.errors += 1;
|
|
self.v_errors.push(error_msg);
|
|
}
|
|
}
|
|
|
|
// impl Display for Status {
|
|
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
// todo!()
|
|
// }
|
|
// }
|
|
|
|
impl Status {
|
|
/// Initialize a new status
|
|
fn zero() -> Self {
|
|
Self {
|
|
info: 0,
|
|
warnings: 0,
|
|
errors: 0,
|
|
v_errors: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
/// Pdfreading and curating
|
|
async fn launch_subprocess(args: &MakeConfig) -> Result<Status> {
|
|
let mut cmd = Command::new("pdflatex");
|
|
let mut status = Status::zero();
|
|
let mut context = Vec::new();
|
|
cmd.arg(&args.filename);
|
|
cmd.env("max_print_line", "1000");
|
|
|
|
// Specify that we want the command's standard output piped back to us.
|
|
cmd.stdout(Stdio::piped());
|
|
|
|
let mut child = cmd.spawn().expect("Failed to spawn command");
|
|
|
|
let stdout = child
|
|
.stdout
|
|
.take()
|
|
.expect("Child did not have a handle to stdout");
|
|
|
|
let mut reader = BufReader::new(stdout).split(0xA);
|
|
|
|
// Ensure the child process is spawned in the runtime so it can
|
|
// make progress on its own while we await for any output.
|
|
tokio::spawn(async move {
|
|
let status = child
|
|
.wait()
|
|
.await
|
|
.expect("Child process encountered an error");
|
|
|
|
println!("Child status was: {}", status);
|
|
});
|
|
|
|
loop {
|
|
let res = reader.next_segment().await;
|
|
if let Err(e) = res {
|
|
return Err(anyhow!("[CORE] Runtime error : {}", e));
|
|
} else if let Ok(line) = res {
|
|
if let Some(line) = line {
|
|
let s = match str::from_utf8(&line) {
|
|
Ok(v) => v,
|
|
Err(e) => {
|
|
// status.errors += 1;
|
|
status.add_error(format!("[CORE] Invalid UTF-8 sequence: {}", e));
|
|
continue;
|
|
}
|
|
};
|
|
// println!("Line: {:?}", s);
|
|
checkline(s, &mut status, &mut context).expect("Error while checking line");
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// println!("{:?}", status);
|
|
Ok(status)
|
|
}
|
|
|
|
/// The line encountered
|
|
#[derive(Debug)]
|
|
enum LineType {
|
|
/// The tex building mode
|
|
Mode,
|
|
Info,
|
|
BoxFullness,
|
|
/// Unknown type
|
|
Unknown(String),
|
|
}
|
|
/// Check each line and changes the status and/or display according warnings
|
|
fn checkline(s: &str, status: &mut Status, context: &mut Vec<String>) -> Result<()> {
|
|
// Match the line to guess what is happening
|
|
let line = match s {
|
|
s if s.starts_with("entering_extended_mode") => LineType::Mode,
|
|
_ => LineType::Unknown(s.to_string()),
|
|
};
|
|
// status.warnings += 1;
|
|
println!("{:?}", line);
|
|
Ok(())
|
|
}
|