diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2016-12-22 13:49:53 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2016-12-22 14:16:05 +0100 |
commit | e619345766e98578929061e566178010c171df48 (patch) | |
tree | 844c71a6842608ca78a244bfd1caaf50cfc92c2e /askama_codegen | |
download | askama-e619345766e98578929061e566178010c171df48.tar.gz askama-e619345766e98578929061e566178010c171df48.tar.bz2 askama-e619345766e98578929061e566178010c171df48.zip |
Initial stab at macros 1.1
Diffstat (limited to 'askama_codegen')
-rw-r--r-- | askama_codegen/Cargo.toml | 11 | ||||
-rw-r--r-- | askama_codegen/src/lib.rs | 31 |
2 files changed, 42 insertions, 0 deletions
diff --git a/askama_codegen/Cargo.toml b/askama_codegen/Cargo.toml new file mode 100644 index 0000000..29043aa --- /dev/null +++ b/askama_codegen/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "askama_codegen" +version = "0.1.0" +authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] + +[dependencies] +syn = "0.10" +quote = "0.3" + +[lib] +proc-macro = true diff --git a/askama_codegen/src/lib.rs b/askama_codegen/src/lib.rs new file mode 100644 index 0000000..6d7170e --- /dev/null +++ b/askama_codegen/src/lib.rs @@ -0,0 +1,31 @@ +#![feature(proc_macro, proc_macro_lib)] + +extern crate proc_macro; +#[macro_use] +extern crate quote; +extern crate syn; + +use proc_macro::TokenStream; + +#[proc_macro_derive(Template, attributes(template))] +pub fn derive_template(input: TokenStream) -> TokenStream { + let source = input.to_string(); + + let ast = syn::parse_macro_input(&source).unwrap(); + let _ctx = match ast.body { + syn::Body::Struct(ref data) => data, + _ => panic!("#[derive(Template)] can only be used with structs"), + }; + + let name = &ast.ident; + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); + let expanded = quote! { + impl #impl_generics askama::Template for #name #ty_generics #where_clause { + fn render(&self) -> String { + "hello world, bar".to_string() + } + } + }; + + expanded.parse().unwrap() +} |