aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/generator.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-07-20 21:18:18 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-07-25 11:39:58 +0200
commitbd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918 (patch)
treefdca7dca4591f62fca30d798b37779f6fadae00a /askama_derive/src/generator.rs
parentc5fbd2ebfb0ce84a49db47f09caa12a048ea61d0 (diff)
downloadaskama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.gz
askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.bz2
askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.zip
Implement basic hyper integration
The integration is based on askama_gotham. There is no specific trait to convert an arbitrary T to `hyper::Response`, so I used `From<Template> for hyper::Response`.
Diffstat (limited to 'askama_derive/src/generator.rs')
-rw-r--r--askama_derive/src/generator.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 142ff7f..5b8c19e 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -308,6 +308,8 @@ impl<'a> Generator<'a> {
self.impl_axum_into_response(&mut buf)?;
#[cfg(feature = "with-gotham")]
self.impl_gotham_into_response(&mut buf)?;
+ #[cfg(feature = "with-hyper")]
+ self.impl_hyper_into_response(&mut buf)?;
#[cfg(feature = "with-mendes")]
self.impl_mendes_responder(&mut buf)?;
#[cfg(feature = "with-rocket")]
@@ -449,6 +451,31 @@ impl<'a> Generator<'a> {
buf.writeln("}")
}
+ // Implement `From<Template> for hyper::Response<Body>`.
+ #[cfg(feature = "with-hyper")]
+ fn impl_hyper_into_response(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
+ let (impl_generics, orig_ty_generics, where_clause) =
+ self.input.ast.generics.split_for_impl();
+ let ident = &self.input.ast.ident;
+ buf.writeln(&format!(
+ "{} {{",
+ quote!(
+ impl #impl_generics ::core::convert::From<&#ident #orig_ty_generics>
+ for ::askama_hyper::hyper::Response<::askama_hyper::hyper::Body>
+ #where_clause
+ )
+ ))?;
+ buf.writeln("#[inline]")?;
+ buf.writeln(&format!(
+ "{} {{",
+ quote!(fn from(value: &#ident #orig_ty_generics) -> Self)
+ ))?;
+ let ext = self.input.extension().unwrap_or("txt");
+ buf.writeln(&format!("::askama_hyper::respond(value, {:?})", ext))?;
+ buf.writeln("}")?;
+ buf.writeln("}")
+ }
+
// Implement mendes' `Responder`.
#[cfg(feature = "with-mendes")]
fn impl_mendes_responder(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {