summaryrefslogtreecommitdiff
path: root/support/texlab/src/tex/compile.rs
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2021-05-23 03:00:39 +0000
committerNorbert Preining <norbert@preining.info>2021-05-23 03:00:39 +0000
commitf1261b349e875b842745b63258c3e338cb1fe3bf (patch)
treeb5d402b3e80818cde2c079a42249f3dcb9732247 /support/texlab/src/tex/compile.rs
parent58aa1ac09b1d9e4769d0a0661cf12e2b2db41b14 (diff)
CTAN sync 202105230300
Diffstat (limited to 'support/texlab/src/tex/compile.rs')
-rw-r--r--support/texlab/src/tex/compile.rs97
1 files changed, 0 insertions, 97 deletions
diff --git a/support/texlab/src/tex/compile.rs b/support/texlab/src/tex/compile.rs
deleted file mode 100644
index a2b0d2f37d..0000000000
--- a/support/texlab/src/tex/compile.rs
+++ /dev/null
@@ -1,97 +0,0 @@
-use std::{io, process::Stdio, time::Duration};
-use tempfile::{tempdir, TempDir};
-use thiserror::Error;
-use tokio::{
- fs,
- process::Command,
- time::{timeout, Elapsed},
-};
-
-#[derive(Debug, PartialEq, Eq, Clone, Copy)]
-pub enum Format {
- Latex,
- Pdflatex,
- Xelatex,
- Lualatex,
-}
-
-impl Format {
- pub fn executable(self) -> &'static str {
- match self {
- Self::Latex => "latex",
- Self::Pdflatex => "pdflatex",
- Self::Xelatex => "xelatex",
- Self::Lualatex => "lualatex",
- }
- }
-}
-
-#[derive(Debug)]
-pub struct Artifacts {
- pub dir: TempDir,
- pub log: String,
-}
-
-#[derive(Debug, Error)]
-pub enum CompileError {
- #[error("an I/O error occurred: `{0}`")]
- IO(#[from] io::Error),
- #[error("TeX engine is not installed")]
- NotInstalled,
- #[error("build timeout: `{0}`")]
- Timeout(#[from] Elapsed),
-}
-
-#[derive(Debug, PartialEq, Eq, Clone, Copy)]
-pub struct CompileParams<'a> {
- pub format: Format,
- pub file_name: &'a str,
- pub code: &'a str,
- pub timeout: Duration,
-}
-
-impl<'a> Default for CompileParams<'a> {
- fn default() -> Self {
- Self {
- format: Format::Lualatex,
- file_name: "code.tex",
- code: "",
- timeout: Duration::from_secs(15),
- }
- }
-}
-
-#[derive(Debug, Clone, Copy)]
-pub struct Compiler<'a> {
- pub executable: &'a str,
- pub args: &'a [&'a str],
- pub file_name: &'a str,
- pub timeout: Duration,
-}
-
-impl<'a> Compiler<'a> {
- pub async fn compile<'b>(&'a self, code: &'b str) -> Result<Artifacts, CompileError> {
- let directory = tempdir()?;
- let tex_file = directory.path().join(self.file_name);
- fs::write(&tex_file, code).await?;
-
- let child = Command::new(self.executable)
- .args(self.args)
- .current_dir(&directory)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .status();
-
- timeout(self.timeout, child)
- .await?
- .map_err(|_| CompileError::NotInstalled)?;
-
- let log_file = tex_file.with_extension("log");
- let log_bytes = fs::read(log_file).await?;
- let log = String::from_utf8_lossy(&log_bytes).into_owned();
- Ok(Artifacts {
- dir: directory,
- log,
- })
- }
-}