diff options
author | Rudi Floren <rudi@instant.ca> | 2023-02-13 13:43:38 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-03-03 10:31:34 +0100 |
commit | 4a7068d33317b69cc6e9d060136594997af53e46 (patch) | |
tree | fa1b796059d10f4f3290655d8cc592205370855f | |
parent | c9613ff6029ee58dc3f94c3dd955b905ed7fc9ef (diff) | |
download | askama-4a7068d33317b69cc6e9d060136594997af53e46.tar.gz askama-4a7068d33317b69cc6e9d060136594997af53e46.tar.bz2 askama-4a7068d33317b69cc6e9d060136594997af53e46.zip |
exchange hyper features From<Template> for hyper::Body with TryFrom
The previous implementation hid the error returned by render,
which made it impossible for callers to react to rendering errors.
While having a simple way to call into would be nice, there is not
way to have both implementations without adding any manual
specialization. Thus, the From impl is replaced by the TryFrom impl.
-rw-r--r-- | askama_derive/src/generator.rs | 12 | ||||
-rw-r--r-- | askama_hyper/tests/basic.rs | 33 |
2 files changed, 16 insertions, 29 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 204098f..fc74f34 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -450,24 +450,22 @@ impl<'a> Generator<'a> { buf.writeln("}")?; buf.writeln("}")?; - // From<Template> for hyper::Body + // TryFrom<Template> for hyper::Body buf.writeln(&format!( "{} {{", quote!( - impl #impl_generics ::core::convert::From<&#ident #orig_ty_generics> + impl #impl_generics ::core::convert::TryFrom<&#ident #orig_ty_generics> for ::askama_hyper::hyper::Body #where_clause ) ))?; + buf.writeln("type Error = ::askama::Error;")?; buf.writeln("#[inline]")?; buf.writeln(&format!( "{} {{", - quote!(fn from(value: &#ident #orig_ty_generics) -> Self) + quote!(fn try_from(value: &#ident #orig_ty_generics) -> Result<Self, Self::Error>) ))?; - buf.writeln( - "::askama::Template::render(value).ok().map(Into::into)\ - .unwrap_or_else(|| ::askama_hyper::hyper::Body::empty())", - )?; + buf.writeln("::askama::Template::render(value).map(Into::into)")?; buf.writeln("}")?; buf.writeln("}") } diff --git a/askama_hyper/tests/basic.rs b/askama_hyper/tests/basic.rs index 831bcbb..5081076 100644 --- a/askama_hyper/tests/basic.rs +++ b/askama_hyper/tests/basic.rs @@ -22,16 +22,16 @@ async fn hello_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> Ok(template.into()) } -async fn body_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> { +async fn try_body_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> { let name = req.param("name").unwrap(); let template = &HelloTemplate { name: &name }; - Ok(Builder::new().body(template.into()).unwrap()) + Ok(Builder::new().body(template.try_into().unwrap()).unwrap()) } fn router() -> Router<Body, Infallible> { Router::builder() .get("/hello/:name", hello_handler) - .get("/body/:name", body_handler) + .get("/try_body/:name", try_body_handler) .build() .unwrap() } @@ -43,15 +43,12 @@ async fn test_hyper() { let server = Server::bind(&addr).serve(service); let local_addr = server.local_addr(); - let (tx1, rx1) = tokio::sync::oneshot::channel::<()>(); - let (tx2, rx2) = tokio::sync::oneshot::channel::<()>(); + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>(); let serve = async move { - let server = server.with_graceful_shutdown(async { - rx1.await.expect("Could not await signal to stop"); - rx2.await.expect("Could not await signal to stop"); - }); + let server = server.with_graceful_shutdown(async { while rx.recv().await.is_some() {} }); server.await.expect("Could not serve"); }; + let hello_tx = tx.clone(); let hello_query = async move { let uri = format!("http://{local_addr}/hello/world") .parse() @@ -73,11 +70,11 @@ async fn test_hyper() { let body = std::str::from_utf8(&body).expect("Body was not UTF-8"); assert_eq!(body, "Hello, world!"); - tx1.send(()).unwrap(); + hello_tx.send(()).unwrap(); }; - let body_query = async move { - let uri = format!("http://{local_addr}/hello/world") + let try_body_query = async move { + let uri = format!("http://{local_addr}/try_body/world") .parse() .expect("Could not format URI"); let client = Client::new(); @@ -85,20 +82,12 @@ async fn test_hyper() { let res = client.get(uri).await.expect("Could not query client"); assert_eq!(res.status(), hyper::StatusCode::OK); - let content_type = res - .headers() - .get("content-type") - .expect("Response did not contain content-type header") - .to_str() - .expect("Content-type was not a UTF-8 string"); - assert_eq!(content_type, mime::TEXT_HTML_UTF_8.to_string()); - let body = to_bytes(res).await.expect("No body returned"); let body = std::str::from_utf8(&body).expect("Body was not UTF-8"); assert_eq!(body, "Hello, world!"); - tx2.send(()).unwrap(); + tx.send(()).unwrap(); }; - tokio::join!(serve, body_query, hello_query); + tokio::join!(serve, try_body_query, hello_query); } |