Merge pull request 'feature/builder' (#5) from feature/builder into main
Reviewed-on: #5
This commit is contained in:
commit
3a5583ea99
|
|
@ -1,2 +1,2 @@
|
|||
[build]
|
||||
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Ctarget-cpu=native"]
|
||||
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Ctarget-cpu=native", "-Zthreads=24"]
|
||||
|
|
|
|||
22
Cargo.toml
22
Cargo.toml
|
|
@ -7,18 +7,21 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.82"
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
anyhow = "1.0.86"
|
||||
clap = { version = "4.5.7", features = ["derive"] }
|
||||
csv = "1.3.0"
|
||||
debug_print = "1.0.0"
|
||||
dynfmt = { version = "0.1.5", features = ["curly"] }
|
||||
enum_dispatch = "0.3.13"
|
||||
futures = "0.3.30"
|
||||
glob = "0.3.1"
|
||||
logos = "0.14.0"
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
regex = "1.10.4"
|
||||
regex = "1.10.5"
|
||||
regex_static = "0.1.1"
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
toml = "0.8.12"
|
||||
serde = { version = "1.0.203", features = ["derive"] }
|
||||
tokio = { version = "1.38.0", features = ["full"] }
|
||||
toml = "0.8.14"
|
||||
|
||||
[profile.dev]
|
||||
debug = 0
|
||||
|
|
@ -39,3 +42,12 @@ lto = true
|
|||
codegen-units = 1
|
||||
incremental = false
|
||||
panic = "abort"
|
||||
|
||||
[profile.base]
|
||||
inherits = "dev"
|
||||
opt-level = 0
|
||||
debug = 0
|
||||
strip = "none"
|
||||
lto = false
|
||||
codegen-units = 256
|
||||
incremental = false
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
// #![allow(dead_code)]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
mod makepdf;
|
||||
mod stat_algo;
|
||||
mod stat_io;
|
||||
mod stat_structs;
|
||||
|
|
@ -21,6 +22,7 @@ use std::{
|
|||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use makepdf::{make_pdf, MakeConfig};
|
||||
use serde::Deserialize;
|
||||
use stats::{stat_solver, StatConfig};
|
||||
use table_structs::Craft;
|
||||
|
|
@ -45,6 +47,8 @@ enum Commands {
|
|||
Config(ConfigArgs),
|
||||
/// Handle stat repartition
|
||||
Stats(StatConfig),
|
||||
/// Make a pdf via pdflatex
|
||||
Make(MakeConfig),
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
|
|
@ -90,6 +94,7 @@ fn main() -> Result<()> {
|
|||
Commands::Tablegen(args) => table_gen(&args, &config),
|
||||
Commands::Config(args) => write_config(&args, &config, config_str),
|
||||
Commands::Stats(args) => stat_solver(&args, &config),
|
||||
Commands::Make(args) => make_pdf(&args, &config),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,429 @@
|
|||
//! Make a pdf using pdflatex
|
||||
|
||||
// use std::fmt::Display;
|
||||
use logos::{Lexer, Logos};
|
||||
use phf::phf_map;
|
||||
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 = "warning")]
|
||||
pub(crate) verbosity: VerboseLevel,
|
||||
// /// The list of whitelisted verbosity levels
|
||||
// pub(crate) whitelist: Vec<VerboseLevel>,
|
||||
// /// The list of blacklisted verbosity levels
|
||||
// pub(crate) blacklist: Vec<VerboseLevel>,
|
||||
}
|
||||
|
||||
/// The verbosity level
|
||||
#[derive(Clone, Debug, PartialEq, PartialOrd)]
|
||||
pub enum VerboseLevel {
|
||||
/// Shouldn't be printed no matter what
|
||||
None,
|
||||
/// Garbage
|
||||
Misc,
|
||||
/// Boxness, barely above Misc
|
||||
Box,
|
||||
/// Context info (Debug info)
|
||||
Context,
|
||||
/// Usefull info
|
||||
Info,
|
||||
/// Important warnings
|
||||
Warning,
|
||||
/// Errors encountered requiring user input
|
||||
Error,
|
||||
/// Uncatachable errors
|
||||
Critical,
|
||||
/// Literrally uncatchable things
|
||||
Uncatchable,
|
||||
}
|
||||
|
||||
static STR_TO_TARGET: phf::Map<&'static str, VerboseLevel> = phf_map! {
|
||||
"misc" =>VerboseLevel::Misc,
|
||||
"box" => VerboseLevel::Box,
|
||||
"context" => VerboseLevel::Context,
|
||||
"info" => VerboseLevel::Info,
|
||||
"warning" => VerboseLevel::Warning,
|
||||
"error" => VerboseLevel::Error,
|
||||
"critical" => VerboseLevel::Critical,
|
||||
};
|
||||
|
||||
impl From<&str> for VerboseLevel {
|
||||
fn from(value: &str) -> Self {
|
||||
if let Some(target) = STR_TO_TARGET.get(value).cloned() {
|
||||
target
|
||||
} else {
|
||||
panic!(
|
||||
"Invalid debug level {}. The debug level available are: {:?}",
|
||||
value,
|
||||
STR_TO_TARGET.keys()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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", "3000");
|
||||
|
||||
// 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(args, s, &mut status, &mut context).expect("Error while checking line");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// println!("{:?}", status);
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
/// The line encountered
|
||||
// #[allow(unused)]
|
||||
#[derive(Debug, Logos, PartialEq)]
|
||||
#[logos(skip r"[) ]+")]
|
||||
enum LineType {
|
||||
/// The tex building mode : entering extended mode
|
||||
#[regex("entering[_ ]extended[_ ]mode")]
|
||||
Mode,
|
||||
/// Info level
|
||||
#[token("L3 programming layer", |lex| lex.slice().to_string())]
|
||||
#[token("For additional information", |lex| lex.slice().to_string())]
|
||||
#[token("Pictures in Paragraphs", |lex| lex.slice().to_string())]
|
||||
#[token("2021/01/20 v 2.00 tkz-graph.sty", |lex| lex.slice().to_string())]
|
||||
Info(String),
|
||||
|
||||
/// Strange starting sequence
|
||||
#[token("? ) [")]
|
||||
#[token("? [1{")]
|
||||
StartingSequence,
|
||||
|
||||
/// Strange end sequence
|
||||
#[token(" ){/")]
|
||||
EndSequence,
|
||||
|
||||
/// Converter [Loading MPS to PDF converter (version 2006.09.02).]
|
||||
#[token("[Loading MPS to PDF converter")]
|
||||
MPSConverter,
|
||||
|
||||
/// A chapter was processed
|
||||
#[regex(r"Chapitre [0-9]+", |lex| lex.slice()[9..].to_string())]
|
||||
Chapter(String),
|
||||
|
||||
/// Missing reference
|
||||
#[token(r"LaTeX Warning: Reference")]
|
||||
MissingReferencePrs,
|
||||
|
||||
/// Missed reference, completed with all info
|
||||
MissingReference(String),
|
||||
|
||||
/// Generic Latex Warning
|
||||
#[regex(r"(LaTex |Warning:){2}[^\n]+", |lex| lex.slice()[14..].to_string())]
|
||||
Warning(String),
|
||||
|
||||
// /// Runaway argument
|
||||
// #[token("Runaway argument?")]
|
||||
// RunAway,
|
||||
/// PdfTex VersionThis is pdfTeX, Version 3.141592653-2.6-1.40.22
|
||||
#[token("This is pdfTeX")]
|
||||
PdfTexVersion,
|
||||
|
||||
/// Context, mosty for boxes
|
||||
// TODO: False positives / Reparsing Context("10") )) [107] (./Ecoles/Protecteur.tex
|
||||
#[regex(r"\[\d[^\]]+", |lex| lex.slice()[1..lex.slice().len()-1].to_string())]
|
||||
Context(String),
|
||||
|
||||
/// Overfull and underfull boxes
|
||||
#[regex("(Over|Under)full")]
|
||||
BoxFullness,
|
||||
/// The Latex Version
|
||||
#[regex("LaTeX2e")]
|
||||
LaTexVersion,
|
||||
/// The write18 mode, whatever that is
|
||||
#[token(" restricted \\write18 enabled.")]
|
||||
WriteMode,
|
||||
|
||||
/// The class of document (book, article, etc)
|
||||
// s if s.starts_with("Document Class") => LineType::DocumentClass(s.to_string()),
|
||||
#[token("Document Class:")]
|
||||
DocumentClass,
|
||||
|
||||
/// Signals that the parser entered a document
|
||||
//TODO: Fix name collection
|
||||
// #[regex(r"(\)+ )?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())]
|
||||
// #[regex(r"(\)+ )?(\[\w+\])?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())]
|
||||
|
||||
// #[regex(r"[\) ]*\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())]
|
||||
// #[regex(r"[\) ]*(\[[0-9]+\] )?\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())]
|
||||
// #[regex(r"[\) 0-9\[\]]*\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())]
|
||||
#[regex(r"\(\./[A-z/_-]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())]
|
||||
EnteringDocument(String),
|
||||
|
||||
/// Info on the loading of a library
|
||||
#[regex(r"Library \(\w+\)[^\n]+", |lex| split_by(":",&lex.slice()[8..]))]
|
||||
Library((String, String)),
|
||||
|
||||
/// The warning generated by a package
|
||||
#[regex(r"Package [A-z_-]+ Warning: [^\n]+", |lex| split_by(":",lex.slice()))]
|
||||
PackageWarning((String, String)),
|
||||
|
||||
/// The warning context generated by a package
|
||||
#[regex(r"\([A-z_-]+\) [^\n]+", |lex| split_by(")",&lex.slice()[1..]))]
|
||||
PackageContextWarning((String, String)),
|
||||
|
||||
/// Loading (Load something)
|
||||
#[regex(r"(\)+ )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string())]
|
||||
Loading(String),
|
||||
|
||||
/// Unknown type
|
||||
Unknown(String),
|
||||
|
||||
/// Building blocks, signals pages being rendered ?
|
||||
#[token(" []")]
|
||||
#[token("[]")]
|
||||
PageGen,
|
||||
|
||||
/// Output
|
||||
// #[regex(r"Output written on", |lex| lex.slice()[18..].to_string())]
|
||||
#[token(r"Output written on")]
|
||||
Output,
|
||||
|
||||
/// Log
|
||||
// #[regex("Transcript written on [^\n]+", |lex| lex.slice()[18..].to_string())]
|
||||
#[token("Transcript written on")]
|
||||
Log,
|
||||
|
||||
// /// Barely parsable data
|
||||
// #[regex(r"\)+")]
|
||||
// Garbage,
|
||||
/// Empty line, to discard
|
||||
Empty,
|
||||
|
||||
/// Geometry package
|
||||
#[token("*geometry* driver: auto-detecting")]
|
||||
GeometryResolution,
|
||||
|
||||
/// Geometry found
|
||||
#[token("*geometry* detected driver: pdftex")]
|
||||
GeometryFound,
|
||||
|
||||
/// Transcript
|
||||
#[token("(see the transcript file for additional information)")]
|
||||
Transcript,
|
||||
}
|
||||
|
||||
impl LineType {
|
||||
/// Decides all verbose levels, for output
|
||||
fn vlvl(&self) -> VerboseLevel {
|
||||
match &self {
|
||||
LineType::Mode => VerboseLevel::Misc,
|
||||
LineType::Info(_) => VerboseLevel::Misc,
|
||||
LineType::StartingSequence => VerboseLevel::Misc,
|
||||
LineType::EndSequence => VerboseLevel::Misc,
|
||||
LineType::MPSConverter => VerboseLevel::Misc,
|
||||
LineType::Chapter(_) => VerboseLevel::Context,
|
||||
LineType::MissingReferencePrs => VerboseLevel::Warning,
|
||||
LineType::Warning(_) => VerboseLevel::Warning,
|
||||
LineType::MissingReference(_) => VerboseLevel::Warning,
|
||||
LineType::PdfTexVersion => VerboseLevel::Misc,
|
||||
LineType::Context(_) => VerboseLevel::Misc,
|
||||
LineType::BoxFullness => VerboseLevel::Box,
|
||||
LineType::LaTexVersion => VerboseLevel::Misc,
|
||||
LineType::WriteMode => VerboseLevel::Misc,
|
||||
LineType::DocumentClass => VerboseLevel::Misc,
|
||||
LineType::EnteringDocument(_) => VerboseLevel::Info,
|
||||
LineType::Library(_) => VerboseLevel::Context,
|
||||
LineType::PackageWarning(_) => VerboseLevel::Box,
|
||||
LineType::PackageContextWarning(_) => VerboseLevel::Warning,
|
||||
LineType::Loading(_) => VerboseLevel::Context,
|
||||
LineType::Unknown(_) => VerboseLevel::Uncatchable,
|
||||
LineType::PageGen => VerboseLevel::None,
|
||||
LineType::Output => VerboseLevel::Misc,
|
||||
LineType::Log => VerboseLevel::Misc,
|
||||
LineType::Empty => VerboseLevel::None,
|
||||
LineType::GeometryResolution => VerboseLevel::Misc,
|
||||
LineType::GeometryFound => VerboseLevel::Misc,
|
||||
LineType::Transcript => VerboseLevel::Misc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to split the token and return two string
|
||||
fn split_by(delim: &str, text: &str) -> Option<(String, String)> {
|
||||
let mut slice = text.split(delim);
|
||||
// println!("{:?}", text);
|
||||
if let Some(one) = slice.next() {
|
||||
// println!("{:?}", one);
|
||||
if let Some(two) = slice.next() {
|
||||
// println!("{} {:?}", one, slice.next());
|
||||
return Some((one.to_string(), two.trim_start().to_string()));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check each line and changes the status and/or display according warnings
|
||||
fn checkline(
|
||||
args: &MakeConfig,
|
||||
orginal_line: &str,
|
||||
status: &mut Status,
|
||||
context: &mut [String],
|
||||
) -> Result<()> {
|
||||
// Match the line to guess what is happening
|
||||
let mut lex = LineType::lexer(orginal_line);
|
||||
let parsed_line = match lex.next() {
|
||||
Some(Ok(m)) => m,
|
||||
None => LineType::Empty,
|
||||
_ => LineType::Unknown(orginal_line.to_string()),
|
||||
};
|
||||
|
||||
// Early exit if the verbosity level doenst match
|
||||
if parsed_line.vlvl() < args.verbosity {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Post processing for some lines that can't be properly handlend by the lexer
|
||||
let final_line = match parsed_line {
|
||||
// LineType::Mode => todo!(),
|
||||
// LineType::Info(_) => todo!(),
|
||||
// LineType::StartingSequence => todo!(),
|
||||
// LineType::EndSequence => todo!(),
|
||||
// LineType::MPSConverter => todo!(),
|
||||
// LineType::Chapter(_) => todo!(),
|
||||
LineType::MissingReferencePrs => &LineType::MissingReference(orginal_line.to_string()),
|
||||
// LineType::Warning(_) => todo!(),
|
||||
// LineType::PdfTexVersion => todo!(),
|
||||
// LineType::Context(_) => todo!(),
|
||||
// TODO: Add
|
||||
// LineType::BoxFullness => todo!(),
|
||||
// LineType::LaTexVersion => todo!(),
|
||||
// LineType::WriteMode => todo!(),
|
||||
// LineType::DocumentClass => todo!(),
|
||||
// LineType::EnteringDocument(_) => todo!(),
|
||||
// LineType::Library(_) => todo!(),
|
||||
// LineType::PackageWarning(_) => todo!(),
|
||||
// LineType::PackageContextWarning(_) => todo!(),
|
||||
// LineType::Loading(_) => todo!(),
|
||||
// LineType::Unknown(_) => todo!(),
|
||||
// LineType::PageGen => todo!(),
|
||||
// LineType::Output => todo!(),
|
||||
// LineType::Log => todo!(),
|
||||
// LineType::Empty => todo!(),
|
||||
// LineType::GeometryResolution => todo!(),
|
||||
// LineType::GeometryFound => todo!(),
|
||||
// LineType::Transcript => todo!(),
|
||||
_ => &parsed_line,
|
||||
};
|
||||
|
||||
match &parsed_line.vlvl() {
|
||||
VerboseLevel::None => (),
|
||||
VerboseLevel::Misc => println!("[MISC] {:?}", final_line),
|
||||
VerboseLevel::Box => println!("[BOXX] {:?}", final_line),
|
||||
VerboseLevel::Context => println!("[CNTX] {:?}", final_line), // Context tends to be shit ?
|
||||
VerboseLevel::Info => println!("[INFO] {:?}", final_line),
|
||||
VerboseLevel::Warning => println!("[WARN] {:?}", final_line),
|
||||
VerboseLevel::Error => println!("[CRIT] {:?}", final_line),
|
||||
VerboseLevel::Critical => println!("[CRIT] {:?}", final_line),
|
||||
VerboseLevel::Uncatchable => println!("[UKNW] {}", orginal_line),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Reference in New Issue