Skip to main content

max / multithreaded

1.6 KB · 65 lines History Blame Raw
1 //! Askama template definitions for all HTML pages and fragments.
2 //!
3 //! Split by domain:
4 //! - `public`: forum directory, project, category, thread pages
5 //! - `partials`: reusable post item fragment
6
7 mod public;
8 mod partials;
9
10 pub use public::*;
11
12 use askama::Template;
13 use axum::{
14 http::StatusCode,
15 response::{Html, IntoResponse, Response},
16 };
17
18 /// CSRF token passed to templates; None when no session store is configured.
19 pub type CsrfTokenOption = Option<String>;
20
21 /// Helper to convert any Askama template into an Axum response.
22 fn render_template<T: Template>(template: T) -> Response {
23 match template.render() {
24 Ok(html) => Html(html).into_response(),
25 Err(err) => {
26 tracing::error!(error = ?err, "template rendering error");
27 (StatusCode::INTERNAL_SERVER_ERROR, "Template error").into_response()
28 }
29 }
30 }
31
32 /// Implement `IntoResponse` for one or more Askama template structs.
33 macro_rules! impl_into_response {
34 ($($T:ty),+ $(,)?) => {
35 $(
36 impl IntoResponse for $T {
37 fn into_response(self) -> Response {
38 render_template(self)
39 }
40 }
41 )+
42 };
43 }
44
45 impl_into_response!(
46 ForumDirectoryTemplate,
47 CommunityTemplate,
48 CategoryTemplate,
49 ThreadTemplate,
50 NewThreadTemplate,
51 EditThreadTemplate,
52 CommunitySettingsTemplate,
53 EditCategoryTemplate,
54 MembersTemplate,
55 UserProfileTemplate,
56 ModerationTemplate,
57 ModLogTemplate,
58 AdminDashboardTemplate,
59 TrackedThreadsTemplate,
60 TrackingInfoTemplate,
61 SearchResultsFragment,
62 Error404Template,
63 Error500Template,
64 );
65