aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive
diff options
context:
space:
mode:
authorLibravatar Rudi Floren <rudi@instant.ca>2023-02-10 13:39:24 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-02-13 11:31:23 +0100
commit32f0799ebb35677c57e18092e6f074cccc2e36ed (patch)
tree873912badd77af2250217b96f0c00179cbc9341f /askama_derive
parentc131154cb1eaec52110e27fcdfb570308cc8a77e (diff)
downloadaskama-32f0799ebb35677c57e18092e6f074cccc2e36ed.tar.gz
askama-32f0799ebb35677c57e18092e6f074cccc2e36ed.tar.bz2
askama-32f0799ebb35677c57e18092e6f074cccc2e36ed.zip
Add impl From<{Template}> for hyper::Body derive
with-hyper now derives an impl allowing Templates to be passed to functions with trait bounds for Into<hyper::Body>.
Diffstat (limited to 'askama_derive')
-rw-r--r--askama_derive/src/generator.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 173fab1..762f356 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -426,12 +426,13 @@ impl<'a> Generator<'a> {
buf.writeln("}")
}
- // Implement `From<Template> for hyper::Response<Body>`.
+ // Implement `From<Template> for hyper::Response<Body>` and `From<Template> for hyper::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;
+ // From<Template> for hyper::Response<Body>
buf.writeln(&format!(
"{} {{",
quote!(
@@ -447,6 +448,27 @@ impl<'a> Generator<'a> {
))?;
buf.writeln("::askama_hyper::respond(value)")?;
buf.writeln("}")?;
+ buf.writeln("}")?;
+
+ // From<Template> for hyper::Body
+ buf.writeln(&format!(
+ "{} {{",
+ quote!(
+ impl #impl_generics ::core::convert::From<&#ident #orig_ty_generics>
+ for ::askama_hyper::hyper::Body
+ #where_clause
+ )
+ ))?;
+ buf.writeln("#[inline]")?;
+ buf.writeln(&format!(
+ "{} {{",
+ quote!(fn from(value: &#ident #orig_ty_generics) -> Self)
+ ))?;
+ buf.writeln(
+ "::askama::Template::render(value).ok().map(Into::into)\
+ .unwrap_or_else(|| ::askama_hyper::hyper::Body::empty())",
+ )?;
+ buf.writeln("}")?;
buf.writeln("}")
}