From 3478395ca91413e007b06da600f4dba3370d197c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 4 Feb 2017 11:19:47 +0100 Subject: Pass entire AST to code generator --- Cargo.lock | 1 + askama/Cargo.toml | 1 + askama/src/generator.rs | 5 +++-- askama/src/lib.rs | 1 + askama_derive/src/lib.rs | 3 +-- 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) -> String { +pub fn generate(ast: &syn::DeriveInput, tokens: &Vec) -> 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() } -- cgit