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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*!
# `weft-derive`.
This module provides compiler support for creating `weft` templates. See the `weft` module for usage.
*/

extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;
extern crate env_logger;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate html5ever;
extern crate regex;

mod derive_renderable;
mod inline_parse;
use derive_renderable::*;

use failure::{Error, ResultExt};
use html5ever::parse_fragment;
use html5ever::rcdom::{Handle, NodeData, RcDom};
use html5ever::tendril::TendrilSink;
use html5ever::QualName;
use proc_macro::TokenStream;
use quote::ToTokens;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Eq, PartialEq)]
enum TemplateSource {
    Path(PathBuf),
    Source(String),
}

#[derive(Debug, Clone)]
struct TemplateDerivation {
    template_source: TemplateSource,
}

/// The main entrypoint for the derivation crate.
#[proc_macro_derive(WeftRenderable, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
    // Theoretically `rustc` provides it's own logging, but we
    // don't know for sure that we're using the same `log` crate. So, just in case?
    env_logger::try_init().unwrap_or_default();
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    match make_template(ast) {
        Ok(toks) => toks.into(),
        Err(err) => panic!("Error: {:?}", err),
    }
}

fn make_template(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Error> {
    info!("Deriving for {}", item.ident);
    trace!("{:#?}", item);
    let config = TemplateDerivation::from_derive(&item).context("find template")?;
    let dom = config.load_relative_to(&root_dir())?;

    let impl_body = derive_impl(&dom, item)?;

    Ok(impl_body.into_token_stream())
}

fn parse_path(path: &Path) -> Result<Vec<Handle>, Error> {
    info!("Using template from {:?}", path);
    let root_name = QualName::new(None, ns!(html), local_name!("html"));
    let parser =
        parse_fragment(RcDom::default(), Default::default(), root_name, Vec::new()).from_utf8();

    let dom = parser
        .from_file(&path)
        .with_context(|_| format!("Parsing template from path {:?}", &path))?;

    let content = find_root_from(dom.document)
        .ok_or_else(|| failure::err_msg("Could not locate root of parsed document?"))?;

    Ok(content)
}

fn parse_source(source: &str) -> Result<Vec<Handle>, Error> {
    info!("Using inline template");
    let root_name = QualName::new(None, ns!(html), local_name!("html"));
    let parser = parse_fragment(RcDom::default(), Default::default(), root_name, Vec::new());

    let dom = parser.one(source);
    let content = find_root_from(dom.document)
        .ok_or_else(|| failure::err_msg("Could not locate root of parsed document?"))?;

    Ok(content)
}

fn find_root_from(node: Handle) -> Option<Vec<Handle>> {
    let root_name = QualName::new(None, ns!(html), local_name!("html"));
    match node.data {
        NodeData::Element { ref name, .. } => {
            if name == &root_name {
                return Some(node.children.borrow().clone());
            }
        }
        _ => {}
    }
    let children = node.children.borrow();
    for child in children.iter() {
        if let Some(it) = find_root_from(child.clone()) {
            return Some(it);
        }
    }

    None
}

impl TemplateDerivation {
    fn from_derive(item: &syn::DeriveInput) -> Result<TemplateDerivation, Error> {
        let attr = item
            .attrs
            .iter()
            .filter_map(|a| a.interpret_meta())
            .inspect(|a| info!("Attribute: {:#?}", a))
            .find(|a| a.name() == "template")
            .ok_or_else(|| failure::err_msg("Could not find template attribute"))?;

        let meta_list = match attr {
            syn::Meta::List(inner) => inner,
            _ => return Err(failure::err_msg("template attribute incorrectly formatted")),
        };

        let mut path = None;
        let mut source = None;
        for meta in meta_list.nested {
            if let syn::NestedMeta::Meta(ref item) = meta {
                if let syn::Meta::NameValue(ref pair) = item {
                    match pair.ident.to_string().as_ref() {
                        "path" => if let syn::Lit::Str(ref s) = pair.lit {
                            path = Some(PathBuf::from(s.value()));
                        } else {
                            return Err(failure::err_msg(
                                "template path attribute should be a string",
                            ));
                        },
                        "source" => if let syn::Lit::Str(ref s) = pair.lit {
                            source = Some(s.value())
                        } else {
                            return Err(failure::err_msg(
                                "template path attribute should be a string",
                            ));
                        },

                        _ => warn!("Unrecognised attribute {:#?}", pair),
                    }
                }
            }
        }
        let template_source = match (path, source) {
            (Some(path), None) => {
                TemplateSource::Path(path)
            },
            (None, Some(source)) => {
                TemplateSource::Source(source)
            },
            _ => bail!("Exactly one of `source` or `path` attributes must be specfied in `#[template(...)]")
        };
        // .ok_or_else(|| failure::err_msg("Missing path attribute"))?);

        let res = TemplateDerivation { template_source };

        Ok(res)
    }

    fn load_relative_to<P: AsRef<Path>>(&self, root_dir: P) -> Result<Vec<Handle>, Error> {
        match &self.template_source {
            TemplateSource::Path(ref path) => {
                let path = PathBuf::from(root_dir.as_ref()).join(path);
                Ok(parse_path(&path)?)
            }
            TemplateSource::Source(ref source) => Ok(parse_source(source)?),
        }
    }
}

fn root_dir() -> PathBuf {
    std::env::var("CARGO_MANIFEST_DIR")
        .unwrap_or_else(|_| {
            warn!("Environment variable $CARGO_MANIFEST_DIR not set, assuming .");
            ".".into()
        }).into()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn can_parse_with_path() {
        let deriv = parse_quote!(#[template(path = "hello.html")]
        struct X;);

        let conf = TemplateDerivation::from_derive(&deriv).expect("parse derive");

        assert_eq!(
            conf.template_source,
            TemplateSource::Path(PathBuf::from("hello.html"))
        );
    }

    #[test]
    fn can_parse_with_source() {
        let source = "<p>Stuff</p>";
        let deriv = parse_quote!(#[template(source = #source)]
        struct X;);

        let conf = TemplateDerivation::from_derive(&deriv).expect("parse derive");

        assert_eq!(conf.template_source, TemplateSource::Source(source.into()));
    }

    #[test]
    fn cannot_parse_with_neither_source_or_path() {
        let deriv = quote!(#[template()]
        struct X;);

        let parsed = syn::parse2(deriv.clone()).expect("parse");
        let res = TemplateDerivation::from_derive(&parsed);
        assert!(res.is_err(), "Template {} should not parse", deriv)
    }

    #[test]
    fn cannot_parse_with_both_source_or_path() {
        let deriv = quote!(#[template(source = "...", path = "...")]
        struct X;);

        let parsed = syn::parse2(deriv.clone()).expect("parse");
        let res = TemplateDerivation::from_derive(&parsed);
        assert!(res.is_err(), "Template {} should not parse", deriv)
    }
}