aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/generator.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-03-10 08:36:46 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-03-23 19:37:10 +0100
commit944d591121865c7c6856c91bfe9d2e91b2d5ff44 (patch)
treeb8fba2f7b11ca5b302e16a3eede4fd66cdda48d3 /askama_shared/src/generator.rs
parentc984945cd5d5b81c89510996988f31cdfdb4bd2b (diff)
downloadaskama-944d591121865c7c6856c91bfe9d2e91b2d5ff44.tar.gz
askama-944d591121865c7c6856c91bfe9d2e91b2d5ff44.tar.bz2
askama-944d591121865c7c6856c91bfe9d2e91b2d5ff44.zip
Move handling of integrations into askama_shared
Before this PR the handling of integrations was done both by askama_shared and askama_derive. This diff lets askama_shared do the work. This will prevent problems like #629, when both packages might come out of sync.
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r--askama_shared/src/generator.rs66
1 files changed, 27 insertions, 39 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index 9997139..def8c08 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -1,11 +1,9 @@
-use super::{get_template_source, CompileError, Integrations};
+use super::{get_template_source, CompileError};
use crate::filters;
use crate::heritage::{Context, Heritage};
use crate::input::{Source, TemplateInput};
use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Ws};
-use proc_macro2::Span;
-
use quote::{quote, ToTokens};
use std::collections::HashMap;
@@ -16,9 +14,8 @@ pub fn generate<S: std::hash::BuildHasher>(
input: &TemplateInput<'_>,
contexts: &HashMap<&Path, Context<'_>, S>,
heritage: Option<&Heritage<'_>>,
- integrations: Integrations,
) -> Result<String, CompileError> {
- Generator::new(input, contexts, heritage, integrations, MapChain::new())
+ Generator::new(input, contexts, heritage, MapChain::new())
.build(&contexts[input.path.as_path()])
}
@@ -29,8 +26,6 @@ struct Generator<'a, S: std::hash::BuildHasher> {
contexts: &'a HashMap<&'a Path, Context<'a>, S>,
// The heritage contains references to blocks and their ancestry
heritage: Option<&'a Heritage<'a>>,
- // What integrations need to be generated
- integrations: Integrations,
// Variables accessible directly from the current scope (not redirected to context)
locals: MapChain<'a, &'a str, LocalMeta>,
// Suffix whitespace from the previous literal. Will be flushed to the
@@ -53,14 +48,12 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
input: &'n TemplateInput<'_>,
contexts: &'n HashMap<&'n Path, Context<'n>, S>,
heritage: Option<&'n Heritage<'_>>,
- integrations: Integrations,
locals: MapChain<'n, &'n str, LocalMeta>,
) -> Generator<'n, S> {
Generator {
input,
contexts,
heritage,
- integrations,
locals,
next_ws: None,
skip_ws: false,
@@ -72,13 +65,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
fn child(&mut self) -> Generator<'_, S> {
let locals = MapChain::with_parent(&self.locals);
- Self::new(
- self.input,
- self.contexts,
- self.heritage,
- self.integrations,
- locals,
- )
+ Self::new(self.input, self.contexts, self.heritage, locals)
}
// Takes a Context and generates the relevant implementations.
@@ -93,27 +80,21 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
self.impl_template(ctx, &mut buf)?;
self.impl_display(&mut buf)?;
- if self.integrations.actix {
- self.impl_actix_web_responder(&mut buf)?;
- }
- if self.integrations.axum {
- self.impl_axum_into_response(&mut buf)?;
- }
- if self.integrations.gotham {
- self.impl_gotham_into_response(&mut buf)?;
- }
- if self.integrations.mendes {
- self.impl_mendes_responder(&mut buf)?;
- }
- if self.integrations.rocket {
- self.impl_rocket_responder(&mut buf)?;
- }
- if self.integrations.tide {
- self.impl_tide_integrations(&mut buf)?;
- }
- if self.integrations.warp {
- self.impl_warp_reply(&mut buf)?;
- }
+ #[cfg(feature = "actix-web")]
+ self.impl_actix_web_responder(&mut buf)?;
+ #[cfg(feature = "axum")]
+ self.impl_axum_into_response(&mut buf)?;
+ #[cfg(feature = "gotham")]
+ self.impl_gotham_into_response(&mut buf)?;
+ #[cfg(feature = "mendes")]
+ self.impl_mendes_responder(&mut buf)?;
+ #[cfg(feature = "rocket")]
+ self.impl_rocket_responder(&mut buf)?;
+ #[cfg(feature = "tide")]
+ self.impl_tide_integrations(&mut buf)?;
+ #[cfg(feature = "warp")]
+ self.impl_warp_reply(&mut buf)?;
+
Ok(buf.buf)
}
@@ -202,6 +183,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Actix-web's `Responder`.
+ #[cfg(feature = "actix-web")]
fn impl_actix_web_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_actix::actix_web::Responder", None)?;
buf.writeln("type Body = ::askama_actix::actix_web::body::BoxBody;")?;
@@ -216,6 +198,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Axum's `IntoResponse`.
+ #[cfg(feature = "axum")]
fn impl_axum_into_response(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_axum::IntoResponse", None)?;
buf.writeln("#[inline]")?;
@@ -230,6 +213,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement gotham's `IntoResponse`.
+ #[cfg(feature = "gotham")]
fn impl_gotham_into_response(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_gotham::IntoResponse", None)?;
buf.writeln("#[inline]")?;
@@ -244,6 +228,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement mendes' `Responder`.
+ #[cfg(feature = "mendes")]
fn impl_mendes_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
let param = syn::parse_str("A: ::mendes::Application").unwrap();
@@ -255,7 +240,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
let mut where_clause = match where_clause {
Some(clause) => clause.clone(),
None => syn::WhereClause {
- where_token: syn::Token![where](Span::call_site()),
+ where_token: syn::Token![where](proc_macro2::Span::call_site()),
predicates: syn::punctuated::Punctuated::new(),
},
};
@@ -293,8 +278,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Rocket's `Responder`.
+ #[cfg(feature = "rocket")]
fn impl_rocket_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
- let lifetime = syn::Lifetime::new("'askama", Span::call_site());
+ let lifetime = syn::Lifetime::new("'askama", proc_macro2::Span::call_site());
let param = syn::GenericParam::Lifetime(syn::LifetimeDef::new(lifetime));
self.write_header(
buf,
@@ -315,6 +301,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
Ok(())
}
+ #[cfg(feature = "tide")]
fn impl_tide_integrations(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
let ext = self.input.extension().unwrap_or("txt");
@@ -340,6 +327,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
buf.writeln("}\n}")
}
+ #[cfg(feature = "warp")]
fn impl_warp_reply(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_warp::warp::reply::Reply", None)?;
buf.writeln("#[inline]")?;