actix_web_lab/
html.rs

1use actix_web::{
2    http::{
3        header::{self, ContentType, TryIntoHeaderValue},
4        StatusCode,
5    },
6    HttpRequest, HttpResponse, Responder,
7};
8
9/// An HTML responder.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Html(pub String);
12
13impl Html {
14    /// Constructs a new `Html` responder.
15    pub fn new(html: impl Into<String>) -> Self {
16        Self(html.into())
17    }
18}
19
20impl Responder for Html {
21    type Body = String;
22
23    fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
24        let mut res = HttpResponse::with_body(StatusCode::OK, self.0);
25        res.headers_mut().insert(
26            header::CONTENT_TYPE,
27            ContentType::html().try_into_value().unwrap(),
28        );
29        res
30    }
31}