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
#![feature(proc_macro_span, proc_macro_raw_ident, slice_patterns)]
#![recursion_limit = "128"]
#![feature(drain_filter)]
use std::{
cell::RefCell,
collections::HashMap,
};
extern crate proc_macro;
mod parsers;
mod types;
type ProcMacroMap = HashMap<String, proc_macro::TokenStream>;
thread_local! {
static SMD_CACHE: RefCell<ProcMacroMap> = RefCell::new(HashMap::new());
static SMD_BORROWED_CACHE: RefCell<ProcMacroMap> = RefCell::new(HashMap::new());
}
#[proc_macro]
pub fn smd(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
smd_inner(input, true)
}
#[proc_macro]
pub fn smd_borrowed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
smd_inner(input, false)
}
fn with_cache<T>(should_move: bool, callback: impl FnOnce(&mut ProcMacroMap) -> T) -> T {
let cache_local_key = if should_move {
&SMD_CACHE
} else {
&SMD_BORROWED_CACHE
};
cache_local_key.with(|cache_cell| {
let mut cache = cache_cell.borrow_mut();
callback(&mut cache)
})
}
fn smd_inner(input: proc_macro::TokenStream, should_move: bool) -> proc_macro::TokenStream {
with_cache(should_move, |cache| {
let as_str = input.to_string();
if let Some(proc_macro_result) = cache.get(&as_str) {
proc_macro_result.clone()
} else {
let input: proc_macro2::TokenStream = input.into();
let vec_of_trees: Vec<proc_macro2::TokenTree> = input.into_iter().collect();
let parsed = parsers::match_html_component(&vec_of_trees, should_move);
let unwrapped = parsed.unwrap();
#[cfg(feature = "smd-logs")]
println!("\nlet mut app = {};\n", unwrapped.1);
let proc_macro_result: proc_macro::TokenStream = unwrapped.1.into();
cache.insert(as_str, proc_macro_result.clone());
proc_macro_result
}
})
}