forked from lambda-fairy/maud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
37 lines (32 loc) · 1.17 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
#![doc(html_root_url = "https://docs.rs/maud_macros/0.26.0")]
// TokenStream values are reference counted, and the mental overhead of tracking
// lifetimes outweighs the marginal gains from explicit borrowing
#![allow(clippy::needless_pass_by_value)]
extern crate proc_macro;
mod ast;
mod escape;
mod generate;
mod parse;
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use proc_macro_error::proc_macro_error;
use quote::quote;
#[proc_macro]
#[proc_macro_error]
pub fn html(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
expand(input.into()).into()
}
fn expand(input: TokenStream) -> TokenStream {
let output_ident = TokenTree::Ident(Ident::new("__maud_output", Span::mixed_site()));
// 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 markups = parse::parse(input);
let stmts = generate::generate(markups, output_ident.clone());
quote!({
extern crate alloc;
extern crate maud;
let mut #output_ident = alloc::string::String::with_capacity(#size_hint);
#stmts
maud::PreEscaped(#output_ident)
})
}