aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--askama_derive/src/config.rs (renamed from askama_shared/src/config.rs)0
-rw-r--r--askama_derive/src/generator.rs (renamed from askama_shared/src/generator.rs)43
-rw-r--r--askama_derive/src/heritage.rs (renamed from askama_shared/src/heritage.rs)0
-rw-r--r--askama_derive/src/input.rs (renamed from askama_shared/src/input.rs)3
-rw-r--r--askama_derive/src/lib.rs92
-rw-r--r--askama_derive/src/parser.rs (renamed from askama_shared/src/parser.rs)0
6 files changed, 113 insertions, 25 deletions
diff --git a/askama_shared/src/config.rs b/askama_derive/src/config.rs
index 01f81a2..01f81a2 100644
--- a/askama_shared/src/config.rs
+++ b/askama_derive/src/config.rs
diff --git a/askama_shared/src/generator.rs b/askama_derive/src/generator.rs
index ea95bd3..39a5380 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -2,9 +2,9 @@ use crate::config::{get_template_source, read_config_file, Config, WhitespaceHan
use crate::heritage::{Context, Heritage};
use crate::input::{Print, Source, TemplateInput};
use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws};
-use crate::{filters, CompileError};
+use crate::CompileError;
-use proc_macro2::TokenStream;
+use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use std::collections::HashMap;
@@ -12,9 +12,8 @@ use std::path::{Path, PathBuf};
use std::{cmp, hash, mem, str};
/// The actual implementation for askama_derive::Template
-#[doc(hidden)]
-pub fn derive_template(input: TokenStream) -> TokenStream {
- let ast: syn::DeriveInput = syn::parse2(input).unwrap();
+pub(crate) fn derive_template(input: TokenStream) -> TokenStream {
+ let ast: syn::DeriveInput = syn::parse(input).unwrap();
match build_template(&ast) {
Ok(source) => source.parse().unwrap(),
Err(e) => e.into_compile_error(),
@@ -302,19 +301,19 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
self.impl_template(ctx, &mut buf)?;
self.impl_display(&mut buf)?;
- #[cfg(feature = "actix-web")]
+ #[cfg(feature = "with-actix-web")]
self.impl_actix_web_responder(&mut buf)?;
- #[cfg(feature = "axum")]
+ #[cfg(feature = "with-axum")]
self.impl_axum_into_response(&mut buf)?;
- #[cfg(feature = "gotham")]
+ #[cfg(feature = "with-gotham")]
self.impl_gotham_into_response(&mut buf)?;
- #[cfg(feature = "mendes")]
+ #[cfg(feature = "with-mendes")]
self.impl_mendes_responder(&mut buf)?;
- #[cfg(feature = "rocket")]
+ #[cfg(feature = "with-rocket")]
self.impl_rocket_responder(&mut buf)?;
- #[cfg(feature = "tide")]
+ #[cfg(feature = "with-tide")]
self.impl_tide_integrations(&mut buf)?;
- #[cfg(feature = "warp")]
+ #[cfg(feature = "with-warp")]
self.impl_warp_reply(&mut buf)?;
Ok(buf.buf)
@@ -405,7 +404,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Actix-web's `Responder`.
- #[cfg(feature = "actix-web")]
+ #[cfg(feature = "with-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;")?;
@@ -420,7 +419,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Axum's `IntoResponse`.
- #[cfg(feature = "axum")]
+ #[cfg(feature = "with-axum")]
fn impl_axum_into_response(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_axum::IntoResponse", None)?;
buf.writeln("#[inline]")?;
@@ -435,7 +434,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement gotham's `IntoResponse`.
- #[cfg(feature = "gotham")]
+ #[cfg(feature = "with-gotham")]
fn impl_gotham_into_response(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::askama_gotham::IntoResponse", None)?;
buf.writeln("#[inline]")?;
@@ -450,7 +449,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement mendes' `Responder`.
- #[cfg(feature = "mendes")]
+ #[cfg(feature = "with-mendes")]
fn impl_mendes_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
let param = syn::parse_str("A: ::mendes::Application").unwrap();
@@ -500,7 +499,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
// Implement Rocket's `Responder`.
- #[cfg(feature = "rocket")]
+ #[cfg(feature = "with-rocket")]
fn impl_rocket_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
let lifetime = syn::Lifetime::new("'askama", proc_macro2::Span::call_site());
let param = syn::GenericParam::Lifetime(syn::LifetimeDef::new(lifetime));
@@ -523,7 +522,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
Ok(())
}
- #[cfg(feature = "tide")]
+ #[cfg(feature = "with-tide")]
fn impl_tide_integrations(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
let ext = self.input.extension().unwrap_or("txt");
@@ -549,7 +548,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
buf.writeln("}\n}")
}
- #[cfg(feature = "warp")]
+ #[cfg(feature = "with-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]")?;
@@ -1381,11 +1380,11 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
name = "json";
}
- #[cfg(not(feature = "json"))]
+ #[cfg(not(feature = "serde-json"))]
if name == "json" {
return Err("the `json` filter requires the `serde-json` feature to be enabled".into());
}
- #[cfg(not(feature = "yaml"))]
+ #[cfg(not(feature = "serde-yaml"))]
if name == "yaml" {
return Err("the `yaml` filter requires the `serde-yaml` feature to be enabled".into());
}
@@ -1396,7 +1395,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
"::askama::filters::{}({}, ",
name, self.input.escaper
));
- } else if filters::BUILT_IN_FILTERS.contains(&name) {
+ } else if crate::BUILT_IN_FILTERS.contains(&name) {
buf.write(&format!("::askama::filters::{}(", name));
} else {
buf.write(&format!("filters::{}(", name));
diff --git a/askama_shared/src/heritage.rs b/askama_derive/src/heritage.rs
index 52c14a2..52c14a2 100644
--- a/askama_shared/src/heritage.rs
+++ b/askama_derive/src/heritage.rs
diff --git a/askama_shared/src/input.rs b/askama_derive/src/input.rs
index 1f367fd..c09f3d0 100644
--- a/askama_shared/src/input.rs
+++ b/askama_derive/src/input.rs
@@ -181,8 +181,7 @@ impl Default for Print {
}
}
-#[doc(hidden)]
-pub fn extension_to_mime_type(ext: &str) -> Mime {
+pub(crate) fn extension_to_mime_type(ext: &str) -> Mime {
let basic_type = mime_guess::from_ext(ext).first_or_octet_stream();
for (simple, utf_8) in &TEXT_TYPES {
if &basic_type == simple {
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs
index b41fa6a..2acf583 100644
--- a/askama_derive/src/lib.rs
+++ b/askama_derive/src/lib.rs
@@ -2,9 +2,99 @@
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]
+use std::borrow::Cow;
+use std::fmt;
+
use proc_macro::TokenStream;
+use proc_macro2::Span;
+
+mod config;
+mod generator;
+mod heritage;
+mod input;
+mod parser;
#[proc_macro_derive(Template, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
- askama_shared::derive_template(input.into()).into()
+ generator::derive_template(input)
+}
+
+#[derive(Debug, Clone)]
+struct CompileError {
+ msg: Cow<'static, str>,
+ span: Span,
}
+
+impl CompileError {
+ fn new<S: Into<Cow<'static, str>>>(s: S, span: Span) -> Self {
+ Self {
+ msg: s.into(),
+ span,
+ }
+ }
+
+ fn into_compile_error(self) -> TokenStream {
+ syn::Error::new(self.span, self.msg)
+ .to_compile_error()
+ .into()
+ }
+}
+
+impl std::error::Error for CompileError {}
+
+impl fmt::Display for CompileError {
+ #[inline]
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.write_str(&self.msg)
+ }
+}
+
+impl From<&'static str> for CompileError {
+ #[inline]
+ fn from(s: &'static str) -> Self {
+ Self::new(s, Span::call_site())
+ }
+}
+
+impl From<String> for CompileError {
+ #[inline]
+ fn from(s: String) -> Self {
+ Self::new(s, Span::call_site())
+ }
+}
+
+// This is used by the code generator to decide whether a named filter is part of
+// Askama or should refer to a local `filters` module. It should contain all the
+// filters shipped with Askama, even the optional ones (since optional inclusion
+// in the const vector based on features seems impossible right now).
+const BUILT_IN_FILTERS: &[&str] = &[
+ "abs",
+ "capitalize",
+ "center",
+ "e",
+ "escape",
+ "filesizeformat",
+ "fmt",
+ "format",
+ "indent",
+ "into_f64",
+ "into_isize",
+ "join",
+ "linebreaks",
+ "linebreaksbr",
+ "paragraphbreaks",
+ "lower",
+ "lowercase",
+ "safe",
+ "trim",
+ "truncate",
+ "upper",
+ "uppercase",
+ "urlencode",
+ "urlencode_strict",
+ "wordcount",
+ // optional features, reserve the names anyway:
+ "json",
+ "markdown",
+ "yaml",
+];
diff --git a/askama_shared/src/parser.rs b/askama_derive/src/parser.rs
index efcad73..efcad73 100644
--- a/askama_shared/src/parser.rs
+++ b/askama_derive/src/parser.rs