From 737858c5166e0d34d95084b349698971c019844f Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 28 Jul 2020 11:54:47 +0200 Subject: Add mendes integration --- askama_shared/src/generator.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ askama_shared/src/lib.rs | 1 + 2 files changed, 52 insertions(+) (limited to 'askama_shared/src') diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 99b8d85..655cdd9 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -104,6 +104,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { if self.integrations.iron { self.impl_iron_modifier_response(&mut buf); } + if self.integrations.mendes { + self.impl_mendes_responder(&mut buf); + } if self.integrations.rocket { self.impl_rocket_responder(&mut buf); } @@ -264,6 +267,54 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln("}"); } + // Implement mendes' `Responder`. + fn impl_mendes_responder(&mut self, buf: &mut Buffer) { + let param = syn::parse_str("A: ::mendes::Application").unwrap(); + + let mut generics = self.input.ast.generics.clone(); + generics.params.push(param); + let (_, orig_ty_generics, _) = self.input.ast.generics.split_for_impl(); + let (impl_generics, _, where_clause) = generics.split_for_impl(); + + let mut where_clause = match where_clause { + Some(clause) => clause.clone(), + None => syn::WhereClause { + where_token: syn::Token![where](Span::call_site()), + predicates: syn::punctuated::Punctuated::new(), + }, + }; + + where_clause + .predicates + .push(syn::parse_str("A::ResponseBody: From").unwrap()); + where_clause + .predicates + .push(syn::parse_str("A::Error: From<::mendes::askama::Error>").unwrap()); + + buf.writeln( + format!( + "{} {} for {} {} {{", + quote!(impl#impl_generics), + "::mendes::application::Responder", + self.input.ast.ident, + quote!(#orig_ty_generics #where_clause), + ) + .as_ref(), + ); + + buf.writeln( + "fn into_response(self, app: &A) \ + -> ::mendes::http::Response {", + ); + + buf.writeln(&format!( + "::mendes::askama::into_response(app, &self, {:?})", + self.input.path.extension() + )); + buf.writeln("}"); + buf.writeln("}"); + } + // Implement Rocket's `Responder`. fn impl_rocket_responder(&mut self, buf: &mut Buffer) { let lifetime = syn::Lifetime::new("'askama", Span::call_site()); diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 2e3d502..9aa93c3 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -265,6 +265,7 @@ pub struct Integrations { pub actix: bool, pub gotham: bool, pub iron: bool, + pub mendes: bool, pub rocket: bool, pub tide: bool, pub warp: bool, -- cgit