1#![allow(non_snake_case)]
2
3#[cfg(not(target_arch = "wasm32"))]
4use crate::fs_cache::PathMapFn;
5
6use crate::memory_cache::InMemoryCache;
7use crate::IncrementalRenderer;
8
9use std::{
10 path::{Path, PathBuf},
11 time::Duration,
12};
13
14#[derive(Clone)]
16pub struct IncrementalRendererConfig {
17 static_dir: PathBuf,
18 memory_cache_limit: usize,
19 invalidate_after: Option<Duration>,
20 clear_cache: bool,
21 pre_render: bool,
22
23 #[cfg(not(target_arch = "wasm32"))]
24 map_path: Option<PathMapFn>,
25}
26
27impl Default for IncrementalRendererConfig {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl IncrementalRendererConfig {
34 pub fn new() -> Self {
36 Self {
37 static_dir: PathBuf::from("./static"),
38 memory_cache_limit: 10000,
39 invalidate_after: None,
40 clear_cache: true,
41 pre_render: false,
42 #[cfg(not(target_arch = "wasm32"))]
43 map_path: None,
44 }
45 }
46
47 pub fn clear_cache(mut self, clear_cache: bool) -> Self {
49 self.clear_cache = clear_cache;
50 self
51 }
52
53 #[cfg(not(target_arch = "wasm32"))]
56 pub fn map_path<F: Fn(&str) -> PathBuf + Send + Sync + 'static>(mut self, map_path: F) -> Self {
57 self.map_path = Some(std::sync::Arc::new(map_path));
58 self
59 }
60
61 pub fn static_dir<P: AsRef<Path>>(mut self, static_dir: P) -> Self {
63 self.static_dir = static_dir.as_ref().to_path_buf();
64 self
65 }
66
67 pub const fn memory_cache_limit(mut self, memory_cache_limit: usize) -> Self {
69 self.memory_cache_limit = memory_cache_limit;
70 self
71 }
72
73 pub fn invalidate_after(mut self, invalidate_after: Duration) -> Self {
75 self.invalidate_after = Some(invalidate_after);
76 self
77 }
78
79 pub fn pre_render(mut self, pre_render: bool) -> Self {
81 self.pre_render = pre_render;
82 self
83 }
84
85 pub fn build(self) -> IncrementalRenderer {
87 let mut renderer = IncrementalRenderer {
88 #[cfg(not(target_arch = "wasm32"))]
89 file_system_cache: crate::fs_cache::FileSystemCache::new(
90 self.static_dir.clone(),
91 self.map_path,
92 self.invalidate_after,
93 ),
94 memory_cache: InMemoryCache::new(self.memory_cache_limit, self.invalidate_after),
95 invalidate_after: self.invalidate_after,
96 };
97
98 if self.clear_cache {
99 renderer.invalidate_all();
100 }
101
102 renderer
103 }
104}