Skip to content

Commit

Permalink
Initial stab at macros 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 22, 2016
0 parents commit e619345
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
target
Cargo.lock
2 changes: 2 additions & 0 deletions Cargo.toml
@@ -0,0 +1,2 @@
[workspace]
members = ["askama", "askama_codegen", "askama_test"]
8 changes: 8 additions & 0 deletions askama/Cargo.toml
@@ -0,0 +1,8 @@
[package]
name = "askama"
version = "0.1.0"
authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"]
workspace = ".."

[dependencies]
askama_codegen = { path = "../askama_codegen" }
26 changes: 26 additions & 0 deletions askama/src/lib.rs
@@ -0,0 +1,26 @@
#![feature(proc_macro)]

#[macro_use]
extern crate askama_codegen;

pub trait Template {
fn render(&self) -> String;
}

#[cfg(test)]
mod tests {

extern crate askama;

#[derive(Template)]
struct TestTemplate {
var: String,
}

#[test]
fn it_works() {
let s = TestTemplate { var: "foo".to_string() }.render();
assert_eq!(s, "hello world, foo");
}

}
11 changes: 11 additions & 0 deletions 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
31 changes: 31 additions & 0 deletions 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()
}
9 changes: 9 additions & 0 deletions askama_test/Cargo.toml
@@ -0,0 +1,9 @@
[package]
name = "askama_test"
version = "0.1.0"
authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"]
workspace = ".."

[dependencies]
askama_codegen = { path = "../askama_codegen" }
askama = { path = "../askama" }
18 changes: 18 additions & 0 deletions askama_test/tests/simple.rs
@@ -0,0 +1,18 @@
#![feature(proc_macro)]

#[macro_use]
extern crate askama_codegen;
extern crate askama;

use askama::Template;

#[derive(Template)]
struct TestTemplate {
var: String,
}

#[test]
fn it_works() {
let s = TestTemplate { var: "foo".to_string() }.render();
assert_eq!(s, "hello world, foo");
}

0 comments on commit e619345

Please sign in to comment.