From 064077a9001e121a12fc234fee44514840380a0e Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 24 May 2022 14:20:09 +0200 Subject: Move code generation into askama_derive --- askama_derive/src/lib.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) (limited to 'askama_derive/src/lib.rs') 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: 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 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", +]; -- cgit