From 65dd3252c903d07313252f0cfc5d166a52526cdc Mon Sep 17 00:00:00 2001 From: iliana etaoin Date: Thu, 6 Apr 2023 13:45:02 -0700 Subject: [PATCH] impl Responder for Html --- src/main.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index e0056c9..17b4d62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,11 +6,11 @@ use askama::Template; use reqwest::{Client, StatusCode, Url}; use rocket::form::{Form, FromForm}; -use rocket::http::{ContentType, Header}; -use rocket::response::status::NoContent; -use rocket::response::{Debug, Redirect, Responder}; -use rocket::{get, post, routes, uri, State}; +use rocket::http::{ContentType, Header, Status}; +use rocket::response::{self, status::NoContent, Debug, Redirect, Responder}; +use rocket::{get, post, routes, uri, Request, Response, State}; use serde::Deserialize; +use std::io::Cursor; use std::str::FromStr; #[rocket::launch] @@ -38,13 +38,26 @@ fn rocket() -> _ { ) } -#[get("/")] -fn index() -> Result<(ContentType, String), Debug> { - #[derive(Template)] - #[template(path = "index.html")] - struct Index; +#[derive(Debug)] +struct Html(T); - Ok((ContentType::HTML, Index.render()?)) +impl Responder<'_, 'static> for Html { + fn respond_to(self, _request: &Request<'_>) -> response::Result<'static> { + let data = self.0.render().map_err(|_| Status::InternalServerError)?; + Response::build() + .header(Header::new("content-type", T::MIME_TYPE)) + .sized_body(data.len(), Cursor::new(data)) + .ok() + } +} + +#[derive(Template)] +#[template(path = "index.html")] +struct Index; + +#[get("/")] +fn index() -> Html { + Html(Index) } #[derive(FromForm)]