Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e619345
Showing
8 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[workspace] | ||
members = ["askama", "askama_codegen", "askama_test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "askama" | ||
version = "0.1.0" | ||
authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] | ||
workspace = ".." | ||
|
||
[dependencies] | ||
askama_codegen = { path = "../askama_codegen" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |