-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.rs
78 lines (74 loc) · 2.37 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use proc_macro::TokenStream;
use std::process::{Command, Output};
use syn::{parse_macro_input, Lit, Meta, MetaList, NestedMeta};
fn lit_as_str(lit: &Lit) -> String {
match lit {
Lit::Str(s) => s.value(),
_ => unreachable!(),
}
}
fn name_value_meta(meta: &Meta) -> (String, String) {
match meta {
Meta::NameValue(nv) => (
nv.path.get_ident().unwrap().to_string(),
lit_as_str(&nv.lit),
),
_ => unreachable!(),
}
}
fn check_version() {
let ver_output = Command::new("witc-exe")
.args(["--version"])
.output()
.unwrap();
let ver = String::from_utf8(ver_output.stdout).unwrap();
if ver != "0.2.0\n" {
panic!("witc-exe version mismatch: expected 0.2.0, got {}", ver);
}
}
#[proc_macro]
pub fn wit_instance(input: TokenStream) -> TokenStream {
check_version();
let MetaList { path, nested, .. } = parse_macro_input!(input);
let mode = path.get_ident().unwrap().to_string();
let r: Output = match &nested[0] {
NestedMeta::Lit(lit) => {
let wit_file = lit_as_str(lit);
Command::new("witc-exe")
.args(["instance", &mode, &wit_file])
.output()
.unwrap()
}
NestedMeta::Meta(meta) => {
let (import_name, wit_file) = name_value_meta(meta);
Command::new("witc-exe")
.args(["instance", &mode, &wit_file, &import_name])
.output()
.unwrap()
}
};
String::from_utf8_lossy(&r.stdout).parse().unwrap()
}
#[proc_macro]
pub fn wit_runtime(input: TokenStream) -> TokenStream {
check_version();
let MetaList { path, nested, .. } = parse_macro_input!(input);
let mode = path.get_ident().unwrap().to_string();
let r: Output = match &nested[0] {
NestedMeta::Lit(lit) => {
let wit_file = lit_as_str(lit);
Command::new("witc-exe")
.args(["runtime", &mode, &wit_file])
.output()
.unwrap()
}
NestedMeta::Meta(meta) => {
let (import_name, wit_file) = name_value_meta(meta);
Command::new("witc-exe")
.args(["runtime", &mode, &wit_file, &import_name])
.output()
.unwrap()
}
};
String::from_utf8_lossy(&r.stdout).parse().unwrap()
}