aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-04 11:19:47 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-04 11:19:47 +0100
commit3478395ca91413e007b06da600f4dba3370d197c (patch)
tree64f6f27fc83457f41c4907c8229bc9b991794b41
parent69238e55d3fe8ff6b8eff58d3cd96b99eb886617 (diff)
downloadaskama-3478395ca91413e007b06da600f4dba3370d197c.tar.gz
askama-3478395ca91413e007b06da600f4dba3370d197c.tar.bz2
askama-3478395ca91413e007b06da600f4dba3370d197c.zip
Pass entire AST to code generator
-rw-r--r--Cargo.lock1
-rw-r--r--askama/Cargo.toml1
-rw-r--r--askama/src/generator.rs5
-rw-r--r--askama/src/lib.rs1
-rw-r--r--askama_derive/src/lib.rs3
5 files changed, 7 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 444f796..4296551 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12,6 +12,7 @@ version = "0.1.0"
dependencies = [
"htmlescape 0.3.1 (git+https://github.com/veddan/rust-htmlescape)",
"nom 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index 97fde9f..0025c75 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -10,3 +10,4 @@ workspace = ".."
[dependencies]
htmlescape = { git = "https://github.com/veddan/rust-htmlescape" }
nom = "2.0"
+syn = "0.11"
diff --git a/askama/src/generator.rs b/askama/src/generator.rs
index c70e507..0fe3ba4 100644
--- a/askama/src/generator.rs
+++ b/askama/src/generator.rs
@@ -1,6 +1,7 @@
use parser::{Conds, Expr, Node, Nodes, Target};
use std::str;
use std::collections::HashSet;
+use syn;
struct Generator {
buf: String,
@@ -170,9 +171,9 @@ impl Generator {
}
-pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
+pub fn generate(ast: &syn::DeriveInput, tokens: &Vec<Node>) -> String {
let mut gen = Generator::new();
- gen.init(ctx_name);
+ gen.init(ast.ident.as_ref());
gen.handle(tokens);
gen.finalize();
gen.result()
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index 2931098..48aa4e3 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -1,5 +1,6 @@
#[macro_use]
extern crate nom;
+extern crate syn;
pub trait Template {
fn render(&self) -> String;
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs
index 37704b3..cdb0dd9 100644
--- a/askama_derive/src/lib.rs
+++ b/askama_derive/src/lib.rs
@@ -56,9 +56,8 @@ pub fn derive_template(input: TokenStream) -> TokenStream {
_ => panic!("#[derive(Template)] can only be used with structs"),
};
- let name = &ast.ident;
let path = get_path_from_attrs(&ast.attrs);
let src = get_template_source(&path);
let tokens = askama::parser::parse(&src);
- askama::generator::generate(name.as_ref(), &tokens).parse().unwrap()
+ askama::generator::generate(&ast, &tokens).parse().unwrap()
}