impl Responder for Html<T: Template>

This commit is contained in:
iliana etaoin 2023-04-06 13:45:02 -07:00
parent 591fd66786
commit 65dd3252c9
1 changed files with 23 additions and 10 deletions

View File

@ -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<askama::Error>> {
#[derive(Debug)]
struct Html<T: Template>(T);
impl<T: Template> Responder<'_, 'static> for Html<T> {
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;
Ok((ContentType::HTML, Index.render()?))
#[get("/")]
fn index() -> Html<Index> {
Html(Index)
}
#[derive(FromForm)]