aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/lib.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-05-24 14:20:09 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-05-24 16:41:32 +0200
commit064077a9001e121a12fc234fee44514840380a0e (patch)
tree9de78005b5049abfd313c3c5e652a5e37a980f17 /askama_derive/src/lib.rs
parente30cad33fd28c0d2546fbd70afa6834bea195f9e (diff)
downloadaskama-064077a9001e121a12fc234fee44514840380a0e.tar.gz
askama-064077a9001e121a12fc234fee44514840380a0e.tar.bz2
askama-064077a9001e121a12fc234fee44514840380a0e.zip
Move code generation into askama_derive
Diffstat (limited to 'askama_derive/src/lib.rs')
-rw-r--r--askama_derive/src/lib.rs92
1 files changed, 91 insertions, 1 deletions
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",
+];