Skip to content

Commit

Permalink
Move handling of integrations into askama_shared
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kijewski authored and djc committed Mar 23, 2022
1 parent c984945 commit 944d591
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 87 deletions.
20 changes: 10 additions & 10 deletions askama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
13 changes: 0 additions & 13 deletions askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 2 additions & 14 deletions askama_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"),
};
8 changes: 8 additions & 0 deletions askama_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
66 changes: 27 additions & 39 deletions askama_shared/src/generator.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()])
}

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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)
}

Expand Down Expand Up @@ -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;")?;
Expand All @@ -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]")?;
Expand All @@ -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]")?;
Expand All @@ -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();

Expand All @@ -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(),
},
};
Expand Down Expand Up @@ -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,
Expand All @@ -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");

Expand All @@ -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]")?;
Expand Down
11 changes: 0 additions & 11 deletions askama_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)] = &[
Expand Down

0 comments on commit 944d591

Please sign in to comment.