diff options
-rw-r--r-- | askama_derive/Cargo.toml | 2 | ||||
-rw-r--r-- | askama_derive/src/generator.rs (renamed from askama_shared/src/generator.rs) | 3 | ||||
-rw-r--r-- | askama_derive/src/input.rs (renamed from askama_shared/src/input.rs) | 2 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 72 | ||||
-rw-r--r-- | askama_derive/src/parser.rs (renamed from askama_shared/src/parser.rs) | 0 | ||||
-rw-r--r-- | askama_shared/Cargo.toml | 3 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 72 |
7 files changed, 75 insertions, 79 deletions
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 3291cef..fed1800 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -18,4 +18,6 @@ rocket = ["askama_shared/rocket"] [dependencies] askama_shared = { version = "0.6.0", path = "../askama_shared" } +nom = "3" +quote = "0.5" syn = "0.13" diff --git a/askama_shared/src/generator.rs b/askama_derive/src/generator.rs index 8a112c8..357bd0f 100644 --- a/askama_shared/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1,7 +1,6 @@ -use filters; use input::TemplateInput; use parser::{self, Cond, Expr, Macro, MatchParameter, MatchVariant, Node, Target, When, WS}; -use path; +use shared::{filters, path}; use quote::{ToTokens, Tokens}; diff --git a/askama_shared/src/input.rs b/askama_derive/src/input.rs index 5869af0..b2b815b 100644 --- a/askama_shared/src/input.rs +++ b/askama_derive/src/input.rs @@ -1,4 +1,4 @@ -use path; +use shared::path; use std::path::{Path, PathBuf}; diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 7c92fdc..df76507 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -1,8 +1,24 @@ extern crate askama_shared as shared; +#[macro_use] +extern crate nom; extern crate proc_macro; +#[macro_use] +extern crate quote; +#[macro_use(parse_quote)] extern crate syn; +mod input; +mod generator; +mod parser; + +use input::Print; +use parser::{Macro, Node}; use proc_macro::TokenStream; +use shared::path; + +use std::borrow::Cow; +use std::collections::HashMap; +use std::path::Path; #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { @@ -11,5 +27,59 @@ pub fn derive_template(input: TokenStream) -> TokenStream { syn::Data::Struct(ref data) => data, _ => panic!("#[derive(Template)] can only be used with structs"), }; - shared::build_template(&ast).parse().unwrap() + build_template(&ast).parse().unwrap() +} + +/// Takes a `syn::DeriveInput` and generates source code for it +/// +/// Reads the metadata from the `template()` attribute to get the template +/// metadata, then fetches the source from the filesystem. The source is +/// parsed, and the parse tree is fed to the code generator. Will print +/// the parse tree and/or generated source according to the `print` key's +/// value as passed to the `template()` attribute. +fn build_template(ast: &syn::DeriveInput) -> String { + let data = input::TemplateInput::new(ast); + let nodes = parser::parse(data.source.as_ref()); + let imports = Imports::new(&nodes, &data.path); + if data.meta.print == Print::Ast || data.meta.print == Print::All { + println!("{:?}", nodes); + } + let code = generator::generate(&data, &nodes, &imports.macro_map()); + if data.meta.print == Print::Code || data.meta.print == Print::All { + println!("{}", code); + } + code +} + +struct Imports<'a> { + sources: HashMap<&'a str, Cow<'a, str>>, +} + +impl<'a> Imports<'a> { + fn new(parent_nodes: &'a [Node], parent_path: &'a Path) -> Imports<'a> { + let sources = parent_nodes.iter().filter_map(|n| { + match *n { + Node::Import(_, import_path, scope) => { + let path = path::find_template_from_path(import_path, Some(parent_path)); + let src = path::get_template_source(&path); + Some((scope, Cow::Owned(src))) + }, + _ => None, + } + }).collect(); + Imports { sources } + } + + fn macro_map(&'a self) -> HashMap<(&'a str, &'a str), Macro<'a>> { + let mut macro_map = HashMap::new(); + for (scope, s) in &self.sources { + for n in parser::parse(s.as_ref()) { + match n { + Node::Macro(name, m) => macro_map.insert((*scope, name), m), + _ => None, + }; + } + } + macro_map + } } diff --git a/askama_shared/src/parser.rs b/askama_derive/src/parser.rs index 402801f..402801f 100644 --- a/askama_shared/src/parser.rs +++ b/askama_derive/src/parser.rs diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml index 3f7c4b3..d1ef222 100644 --- a/askama_shared/Cargo.toml +++ b/askama_shared/Cargo.toml @@ -16,8 +16,5 @@ rocket = [] [dependencies] error-chain = "0.11" -nom = "3" -quote = "0.5" serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } -syn = "0.13" diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 1f145f2..3213778 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -2,12 +2,6 @@ #[macro_use] extern crate error_chain; -#[macro_use] -extern crate nom; -#[macro_use] -extern crate quote; -#[macro_use(parse_quote)] -extern crate syn; #[cfg(feature = "serde-json")] extern crate serde; @@ -20,72 +14,6 @@ pub mod filters; pub mod path; mod escaping; -mod generator; -mod input; -mod parser; - -use input::Print; -use parser::{Macro, Node}; - -use std::borrow::Cow; -use std::collections::HashMap; -use std::path::Path; - -/// Takes a `syn::DeriveInput` and generates source code for it -/// -/// Reads the metadata from the `template()` attribute to get the template -/// metadata, then fetches the source from the filesystem. The source is -/// parsed, and the parse tree is fed to the code generator. Will print -/// the parse tree and/or generated source according to the `print` key's -/// value as passed to the `template()` attribute. -pub fn build_template(ast: &syn::DeriveInput) -> String { - let data = input::TemplateInput::new(ast); - let nodes = parser::parse(data.source.as_ref()); - let imports = Imports::new(&nodes, &data.path); - if data.meta.print == Print::Ast || data.meta.print == Print::All { - println!("{:?}", nodes); - } - let code = generator::generate(&data, &nodes, &imports.macro_map()); - if data.meta.print == Print::Code || data.meta.print == Print::All { - println!("{}", code); - } - code -} - - -pub struct Imports<'a> { - pub sources: HashMap<&'a str, Cow<'a, str>>, -} - -impl<'a> Imports<'a> { - pub fn new(parent_nodes: &'a [Node], parent_path: &'a Path) -> Imports<'a> { - let sources = parent_nodes.iter().filter_map(|n| { - match *n { - Node::Import(_, import_path, scope) => { - let path = path::find_template_from_path(import_path, Some(parent_path)); - let src = path::get_template_source(&path); - Some((scope, Cow::Owned(src))) - }, - _ => None, - } - }).collect(); - Imports { sources } - } - - pub fn macro_map(&'a self) -> HashMap<(&'a str, &'a str), Macro<'a>> { - let mut macro_map = HashMap::new(); - for (scope, s) in &self.sources { - for n in parser::parse(s.as_ref()) { - match n { - Node::Macro(name, m) => macro_map.insert((*scope, name), m), - _ => None, - }; - } - } - macro_map - } -} - mod errors { error_chain! { |