rocket_include_handlebars/debug/
manager.rs1use std::sync::{Mutex, PoisonError};
2
3use serde::Serialize;
4
5use super::{HandlebarsResponse, ReloadableHandlebars};
6use crate::{functions::compute_data_etag, EtagIfNoneMatch};
7
8#[derive(Educe)]
10#[educe(Debug)]
11pub struct HandlebarsContextManager {
12 pub handlebars: Mutex<ReloadableHandlebars>,
13}
14
15impl HandlebarsContextManager {
16 #[inline]
17 pub(crate) fn new(
18 handlebars: Mutex<ReloadableHandlebars>,
19 _cache_capacity: usize,
20 ) -> HandlebarsContextManager {
21 HandlebarsContextManager {
22 handlebars,
23 }
24 }
25
26 #[inline]
28 pub fn build<S: AsRef<str>, V: Serialize>(
29 &self,
30 etag_if_none_match: &EtagIfNoneMatch<'_>,
31 minify: bool,
32 name: S,
33 context: V,
34 ) -> HandlebarsResponse {
35 self.handlebars
36 .lock()
37 .unwrap_or_else(PoisonError::into_inner)
38 .render(name.as_ref(), &context)
39 .map(|html| {
40 let etag = compute_data_etag(html.as_bytes());
41
42 if etag_if_none_match.weak_eq(&etag) {
43 HandlebarsResponse::not_modified()
44 } else {
45 let html = if minify { html_minifier::minify(html).unwrap() } else { html };
46
47 HandlebarsResponse::build_not_cache(html, &etag)
48 }
49 })
50 .unwrap()
51 }
52
53 #[inline]
55 pub fn render<S: AsRef<str>, V: Serialize>(&self, name: S, context: V) -> String {
56 self.handlebars
57 .lock()
58 .unwrap_or_else(PoisonError::into_inner)
59 .render(name.as_ref(), &context)
60 .unwrap()
61 }
62}