Initial commit
This commit is contained in:
12
src/bin.rs
Normal file
12
src/bin.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use qroc::perl;
|
||||
|
||||
fn main() {
|
||||
println!("{}", add(1, 2));
|
||||
}
|
||||
|
||||
#[perl {
|
||||
$_ =~ s/populate/a + b/g;
|
||||
}]
|
||||
fn add(a: isize, b: isize) -> isize {
|
||||
populate
|
||||
}
|
48
src/lib.rs
Normal file
48
src/lib.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use proc_macro::TokenStream;
|
||||
use std::env;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use std::process::Command;
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn perl(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let out_path = format!("/tmp/{}/outputs", env!("CARGO_PKG_NAME"));
|
||||
if !Path::new(&out_path).exists() {
|
||||
create_dir_all(Path::new(&out_path)).expect("Couldn't create temp directory");
|
||||
}
|
||||
|
||||
let pl = attr.to_string();
|
||||
let body = item.to_string();
|
||||
|
||||
let filestub = format!("{:x}", md5::compute(format!("{}+{}", pl, body)));
|
||||
|
||||
let header_pl = format!(
|
||||
"$_ = @ARGV[0]; {}; open (FH, '>', '{}/{}'); print FH $_;",
|
||||
pl, out_path, filestub
|
||||
);
|
||||
|
||||
let output = Command::new("perl")
|
||||
.args(["-e", &header_pl, &body])
|
||||
.output()
|
||||
.expect("Failed to invoke perl");
|
||||
|
||||
if !output.stderr.is_empty() {
|
||||
let err = String::from_utf8(output.stderr).expect("Couldn't decode UTF-8");
|
||||
panic!("stderr(perl): {}", err)
|
||||
} else {
|
||||
if !output.stdout.is_empty() {
|
||||
let stdout = String::from_utf8(output.stdout).expect("Couldn't decode UTF-8");
|
||||
println!("stdout(perl): {}", stdout);
|
||||
}
|
||||
|
||||
let mut outfile = File::open(Path::new(&format!("{}/{}", out_path, filestub)))
|
||||
.expect("Couldn't open output file");
|
||||
let mut tokens = String::new();
|
||||
outfile
|
||||
.read_to_string(&mut tokens)
|
||||
.expect("Couldn't read output file");
|
||||
tokens.parse().expect("Couldn't form token stream")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user