askama_web/implementation/
trillium_0_2.rs

1pub use std::borrow::Cow;
2pub use std::boxed::Box;
3pub use std::future::Future;
4pub use std::marker::{Send, Sync};
5pub use std::pin::Pin;
6pub use std::primitive::str;
7pub use std::stringify;
8
9pub use askama::Template;
10use trillium_0_2::{Body, KnownHeaderName, Status};
11pub use trillium_0_2::{Conn, Handler};
12
13#[cfg(feature = "derive")]
14pub use crate::__askama_web_impl_trillium_0_2 as derive;
15
16#[cfg(feature = "derive")]
17#[macro_export]
18#[doc(hidden)]
19macro_rules! __askama_web_impl_trillium_0_2 {
20    (@ $ast:tt) => {
21        $crate::__askama_web_impl::askama_web_derive::impl_framework! {
22            $crate::__askama_web_impl::trillium_0_2::derive!(
23                $ast where Self:
24                    $crate::__askama_web_impl::trillium_0_2::Send +
25                    $crate::__askama_web_impl::trillium_0_2::Sync +
26                    'static
27            );
28        }
29    };
30    (
31        ident: [$ident:ident],
32        impl_generics: $_impl_generics:tt,
33        ty_generics: $_ty_generics:tt,
34        where_clause: $_where_clause:tt,
35        ex_impl_generics: [$($impl_generics:tt)*],
36        ex_ty_generics: [$($ty_generics:tt)*],
37        ex_where_clause: [$($where_clause:tt)*],
38    ) => {
39        const _: () = {
40            use $crate::__askama_web_impl::trillium_0_2 as __askama_web;
41
42            impl $($impl_generics)* __askama_web::Handler
43            for $ident $($ty_generics)* $($where_clause)* {
44                #[inline]
45                #[must_use]
46                #[track_caller]
47                #[allow(single_use_lifetimes)] // false-positive
48                fn run<'__askama_web_self, '__askama_web_future>(
49                    &'__askama_web_self self,
50                    conn: __askama_web::Conn,
51                ) -> __askama_web::Pin<Box<
52                    dyn __askama_web::Future<Output = __askama_web::Conn> +
53                    __askama_web::Send +
54                    '__askama_web_future
55                >>
56                where
57                    '__askama_web_self: '__askama_web_future,
58                    Self: '__askama_web_future,
59                {
60                    let result = <Self as __askama_web::Template>::render(&self);
61                    __askama_web::render(result, conn)
62                }
63
64                #[inline]
65                fn name(&self) -> __askama_web::Cow<'static, __askama_web::str> {
66                    __askama_web::Cow::Borrowed(__askama_web::stringify!($ident))
67                }
68            }
69        };
70    };
71}
72
73impl<T: Template + Send + Sync + 'static> Handler for crate::WebTemplate<T> {
74    #[inline]
75    #[must_use]
76    #[track_caller]
77    #[allow(single_use_lifetimes)] // false-positive
78    fn run<'a: 'b, 'b>(&'a self, conn: Conn) -> Pin<Box<dyn Future<Output = Conn> + Send + 'b>>
79    where
80        Self: 'b,
81    {
82        render(T::render(&self.0), conn)
83    }
84}
85
86#[must_use]
87#[track_caller]
88pub fn render(
89    result: askama::Result<String>,
90    conn: Conn,
91) -> Pin<Box<dyn Future<Output = Conn> + Send + 'static>> {
92    let result = match result {
93        Ok(body) => Some(body),
94        Err(err) => {
95            crate::render_error(&err);
96            None
97        }
98    };
99    Box::pin(run(result, conn))
100}
101
102#[must_use]
103async fn run(result: Option<String>, conn: Conn) -> Conn {
104    let (status, ct, body) = if let Some(body) = result {
105        (
106            Status::Ok,
107            "text/html; charset=utf-8",
108            Body::new_static(body.into_bytes()),
109        )
110    } else {
111        (
112            Status::InternalServerError,
113            "text/plain; charset=utf-8",
114            Body::new_static(b"INTERNAL SERVER ERROR"),
115        )
116    };
117    conn.with_status(status)
118        .with_response_header(KnownHeaderName::ContentType, ct)
119        .with_body(body)
120}