-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
754 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "futures-boxed" | ||
version = "0.1.0" | ||
authors = [ | ||
"Eric Förster <efoerster@users.noreply.github.com>", | ||
"Patrick Förster <pfoerster@users.noreply.github.com>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "1.0" | ||
quote = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#![recursion_limit = "128"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream, TokenTree}; | ||
use quote::{quote, ToTokens}; | ||
use std::iter::FromIterator; | ||
use syn::{export::TokenStream2, *}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn boxed(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
match parse::<ItemFn>(item.clone()) { | ||
Ok(fn_) => boxed_fn(fn_), | ||
Err(_) => { | ||
let item = TokenStream::from_iter(item.into_iter().filter(|x| match x { | ||
TokenTree::Ident(x) if x.to_string() == "async" => false, | ||
_ => true, | ||
})); | ||
|
||
let method: TraitItemMethod = parse(item).unwrap(); | ||
boxed_trait_method(method) | ||
} | ||
} | ||
} | ||
|
||
fn boxed_fn(fn_: ItemFn) -> TokenStream { | ||
let attrs = &fn_.attrs; | ||
let vis = &fn_.vis; | ||
let sig = boxed_fn_sig(&fn_.sig); | ||
let block = &fn_.block; | ||
let tokens = quote! { | ||
#(#attrs)* | ||
#vis #sig { | ||
use futures::future::FutureExt; | ||
let task = async move #block; | ||
task.boxed() | ||
} | ||
}; | ||
|
||
tokens.into() | ||
} | ||
|
||
fn boxed_trait_method(method: TraitItemMethod) -> TokenStream { | ||
let attrs = &method.attrs; | ||
let sig = boxed_fn_sig(&method.sig); | ||
let tokens = quote! { | ||
#(#attrs)* | ||
#sig; | ||
}; | ||
|
||
tokens.into() | ||
} | ||
|
||
fn boxed_fn_sig(sig: &Signature) -> TokenStream2 { | ||
let constness = &sig.constness; | ||
let ident = &sig.ident; | ||
let generics = &sig.generics; | ||
let inputs = &sig.inputs; | ||
let return_ty = match &sig.output { | ||
ReturnType::Default => quote!(()), | ||
ReturnType::Type(_, ty) => ty.into_token_stream(), | ||
}; | ||
|
||
quote! { | ||
#constness fn #ident #generics(#inputs) -> futures::future::BoxFuture<'_, #return_ty> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod tex; | ||
|
||
pub use self::tex::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use std::{error, fmt, io, process::Stdio, time::Duration}; | ||
use tempfile::{tempdir, TempDir}; | ||
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 directory: TempDir, | ||
pub log: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum CompileError { | ||
IO(io::Error), | ||
NotInstalled, | ||
Timeout(Elapsed), | ||
} | ||
|
||
impl From<io::Error> for CompileError { | ||
fn from(why: io::Error) -> Self { | ||
Self::IO(why) | ||
} | ||
} | ||
|
||
impl From<Elapsed> for CompileError { | ||
fn from(why: Elapsed) -> Self { | ||
Self::Timeout(why) | ||
} | ||
} | ||
|
||
impl fmt::Display for CompileError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::IO(why) => write!(f, "{}", why), | ||
Self::NotInstalled => write!(f, "TeX compiler not installed"), | ||
Self::Timeout(why) => write!(f, "{}", why), | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for CompileError { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
match self { | ||
Self::IO(why) => why.source(), | ||
Self::NotInstalled => None, | ||
Self::Timeout(why) => why.source(), | ||
} | ||
} | ||
} | ||
|
||
#[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 { directory, log }) | ||
} | ||
} |
Oops, something went wrong.