qwreey_rocket/
responder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::sync::Arc;

use qwreey_utility_rs::ElementReadHandle;
use rocket::{
    http::ContentType,
    response::{Responder, Result as RocketResult},
    Request, Response,
};

pub struct ElementResponder<'a> {
    content_type: ContentType,
    body: ElementReadHandle<'a, Arc<str>>,
}
impl<'a> ElementResponder<'a> {
    pub fn new(content_type: ContentType, body: ElementReadHandle<'a, Arc<str>>) -> Self {
        Self { content_type, body }
    }
}
impl<'req, 'res: 'req> Responder<'req, 'res> for ElementResponder<'res> {
    fn respond_to(self, request: &'req Request) -> RocketResult<'res> {
        Ok(Response::build_from(self.body.clone().respond_to(request)?)
            .header(self.content_type)
            .finalize())
    }
}
pub trait ToElementResponder<'a> {
    fn to_element_responder(self, content_type: ContentType) -> ElementResponder<'a>;
}
impl<'a> ToElementResponder<'a> for ElementReadHandle<'a, Arc<str>> {
    fn to_element_responder(self, content_type: ContentType) -> ElementResponder<'a> {
        ElementResponder::new(content_type, self)
    }
}