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/Cargo.toml | 1 + askama_derive/Cargo.toml | 1 + askama_derive/src/lib.rs | 1 + askama_shared/src/generator.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ askama_shared/src/lib.rs | 1 + 5 files changed, 55 insertions(+) diff --git a/askama/Cargo.toml b/askama/Cargo.toml index aa21dcb..4b804ad 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -27,6 +27,7 @@ num-traits = ["askama_shared/num-traits"] with-actix-web = ["askama_derive/actix-web"] with-gotham = ["askama_derive/gotham"] with-iron = ["askama_derive/iron"] +with-mendes = ["askama_derive/mendes"] with-rocket = ["askama_derive/rocket"] with-tide = ["askama_derive/tide"] with-warp = ["askama_derive/warp"] diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 8e97647..4394444 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -17,6 +17,7 @@ proc-macro = true actix-web = [] gotham = [] iron = [] +mendes = [] rocket = [] tide = [] warp = [] diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 2277942..cbffa07 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -88,6 +88,7 @@ const INTEGRATIONS: Integrations = Integrations { actix: cfg!(feature = "actix-web"), gotham: cfg!(feature = "gotham"), iron: cfg!(feature = "iron"), + mendes: cfg!(feature = "mendes"), rocket: cfg!(feature = "rocket"), tide: cfg!(feature = "tide"), warp: cfg!(feature = "warp"), 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