-
Notifications
You must be signed in to change notification settings - Fork 132
/
lib.rs
48 lines (40 loc) · 1.26 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
#![feature(proc_macro)]
#![doc(html_root_url = "https://docs.rs/maud_macros/0.17.1")]
extern crate literalext;
extern crate maud_htmlescape;
extern crate proc_macro;
mod parse;
mod build;
use proc_macro::{Literal, Span, Term, TokenNode, TokenStream, TokenTree};
use proc_macro::quote;
type ParseResult<T> = Result<T, String>;
#[proc_macro]
pub fn html(input: TokenStream) -> TokenStream {
expand(input)
}
#[proc_macro]
pub fn html_debug(input: TokenStream) -> TokenStream {
let expr = expand(input);
println!("expansion:\n{}", expr);
expr
}
fn expand(input: TokenStream) -> TokenStream {
let output_ident = TokenTree {
kind: TokenNode::Term(Term::intern("__maud_output")),
span: Span::default(),
};
// Heuristic: the size of the resulting markup tends to correlate with the
// code size of the template itself
let size_hint = input.to_string().len();
let size_hint = TokenNode::Literal(Literal::u64(size_hint as u64));
let stmts = match parse::parse(input, output_ident.clone()) {
Ok(stmts) => stmts,
Err(e) => panic!(e),
};
quote!({
extern crate maud;
let mut $output_ident = String::with_capacity($size_hint as usize);
$stmts
maud::PreEscaped($output_ident)
})
}