aboutsummaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--askama/Cargo.toml20
-rw-r--r--askama_derive/Cargo.toml13
-rw-r--r--askama_derive/src/lib.rs16
-rw-r--r--askama_shared/Cargo.toml8
-rw-r--r--askama_shared/src/generator.rs66
-rw-r--r--askama_shared/src/lib.rs11
6 files changed, 47 insertions, 87 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index 64468b2..cdf836b 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -17,20 +17,20 @@ maintenance = { status = "actively-developed" }
[features]
default = ["config", "humansize", "num-traits", "urlencode"]
-config = ["askama_derive/config", "askama_shared/config"]
+config = ["askama_shared/config"]
humansize = ["askama_shared/humansize"]
markdown = ["askama_shared/markdown"]
urlencode = ["askama_shared/percent-encoding"]
-serde-json = ["askama_derive/json", "askama_shared/json"]
-serde-yaml = ["askama_derive/yaml", "askama_shared/yaml"]
+serde-json = ["askama_shared/json"]
+serde-yaml = ["askama_shared/yaml"]
num-traits = ["askama_shared/num-traits"]
-with-actix-web = ["askama_derive/actix-web"]
-with-axum = ["askama_derive/axum"]
-with-gotham = ["askama_derive/gotham"]
-with-mendes = ["askama_derive/mendes"]
-with-rocket = ["askama_derive/rocket"]
-with-tide = ["askama_derive/tide"]
-with-warp = ["askama_derive/warp"]
+with-actix-web = ["askama_shared/actix-web"]
+with-axum = ["askama_shared/axum"]
+with-gotham = ["askama_shared/gotham"]
+with-mendes = ["askama_shared/mendes"]
+with-rocket = ["askama_shared/rocket"]
+with-tide = ["askama_shared/tide"]
+with-warp = ["askama_shared/warp"]
# deprecated
mime = []
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml
index 80301dd..0900ac7 100644
--- a/askama_derive/Cargo.toml
+++ b/askama_derive/Cargo.toml
@@ -12,19 +12,6 @@ edition = "2018"
[lib]
proc-macro = true
-[features]
-config = ["askama_shared/config"]
-json = ["askama_shared/json"]
-yaml = ["askama_shared/yaml"]
-
-actix-web = []
-axum = []
-gotham = []
-mendes = []
-rocket = []
-tide = []
-warp = []
-
[dependencies]
askama_shared = { version = "0.12.1", path = "../askama_shared", default-features = false }
proc-macro2 = "1"
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs
index 00936d0..d04ef72 100644
--- a/askama_derive/src/lib.rs
+++ b/askama_derive/src/lib.rs
@@ -5,9 +5,7 @@
use askama_shared::heritage::{Context, Heritage};
use askama_shared::input::{Print, Source, TemplateInput};
use askama_shared::parser::{parse, Expr, Node};
-use askama_shared::{
- generator, get_template_source, read_config_file, CompileError, Config, Integrations,
-};
+use askama_shared::{generator, get_template_source, read_config_file, CompileError, Config};
use proc_macro::TokenStream;
use std::collections::HashMap;
@@ -62,7 +60,7 @@ fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
eprintln!("{:?}", parsed[input.path.as_path()]);
}
- let code = generator::generate(&input, &contexts, heritage.as_ref(), INTEGRATIONS)?;
+ let code = generator::generate(&input, &contexts, heritage.as_ref())?;
if input.print == Print::Code || input.print == Print::All {
eprintln!("{}", code);
}
@@ -108,13 +106,3 @@ fn find_used_templates(
}
Ok(())
}
-
-const INTEGRATIONS: Integrations = Integrations {
- actix: cfg!(feature = "actix-web"),
- axum: cfg!(feature = "axum"),
- gotham: cfg!(feature = "gotham"),
- mendes: cfg!(feature = "mendes"),
- rocket: cfg!(feature = "rocket"),
- tide: cfg!(feature = "tide"),
- warp: cfg!(feature = "warp"),
-};
diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml
index 8e2be28..9d95b00 100644
--- a/askama_shared/Cargo.toml
+++ b/askama_shared/Cargo.toml
@@ -16,6 +16,14 @@ json = ["serde", "serde_json", "askama_escape/json"]
markdown = ["comrak"]
yaml = ["serde", "serde_yaml"]
+actix-web = []
+axum = []
+gotham = []
+mendes = []
+rocket = []
+tide = []
+warp = []
+
[dependencies]
askama_escape = { version = "0.10.3", path = "../askama_escape" }
comrak = { version = "0.12", optional = true, default-features = false }
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]")?;
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index f8d5729..8a336bb 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -274,17 +274,6 @@ pub fn get_template_source(tpl_path: &Path) -> std::result::Result<String, Compi
}
}
-#[derive(Clone, Copy, Debug)]
-pub struct Integrations {
- pub actix: bool,
- pub axum: bool,
- pub gotham: bool,
- pub mendes: bool,
- pub rocket: bool,
- pub tide: bool,
- pub warp: bool,
-}
-
static CONFIG_FILE_NAME: &str = "askama.toml";
static DEFAULT_SYNTAX_NAME: &str = "default";
static DEFAULT_ESCAPERS: &[(&[&str], &str)] = &[