1use std::collections::BTreeMap;
5use std::fmt;
6
7use std::time::Duration;
8
9use laravel::time::ExactDuration;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
17pub struct LivewireConfig {
18 pub component_locations: Vec<String>,
20 pub component_namespaces: BTreeMap<String, String>,
23 pub component_layout: Option<String>,
25 pub component_placeholder: Option<String>,
27 pub make_command: MakeCommand,
29 pub component_path: String,
31 pub view_path: String,
33 pub temporary_file_upload: TemporaryFileUpload,
35 pub render_on_redirect: bool,
37 pub legacy_model_binding: bool,
39 pub inject_assets: bool,
41 pub navigate: Navigate,
43 pub inject_morph_markers: bool,
45 pub smart_wire_keys: bool,
47 pub pagination_theme: PaginationTheme,
49 pub release_token: String,
51 pub csp_safe: bool,
53 pub asset_url: Option<String>,
55 pub update_uri: Option<String>,
57 pub payload: PayloadLimits,
59}
60
61impl Default for LivewireConfig {
62 fn default() -> Self {
63 Self {
64 component_locations: vec![
65 "resources/views/components".to_owned(),
66 "resources/views/livewire".to_owned(),
67 ],
68 component_namespaces: BTreeMap::from([
69 ("layouts".to_owned(), "resources/views/layouts".to_owned()),
70 ("pages".to_owned(), "resources/views/pages".to_owned()),
71 ]),
72 component_layout: Some("layouts::app".to_owned()),
73 component_placeholder: None,
74 make_command: MakeCommand::default(),
75 component_path: "src/livewire".to_owned(),
76 view_path: "resources/views/livewire".to_owned(),
77 temporary_file_upload: TemporaryFileUpload::default(),
78 render_on_redirect: false,
79 legacy_model_binding: false,
80 inject_assets: true,
81 navigate: Navigate::default(),
82 inject_morph_markers: true,
83 smart_wire_keys: true,
84 pagination_theme: PaginationTheme::Tailwind,
85 release_token: "a".to_owned(),
86 csp_safe: false,
87 asset_url: None,
88 update_uri: None,
89 payload: PayloadLimits::default(),
90 }
91 }
92}
93
94impl LivewireConfig {
95 #[must_use]
98 pub fn of(config: &laravel::config::Config) -> &Self {
99 static DEFAULTS: std::sync::LazyLock<LivewireConfig> = std::sync::LazyLock::new(LivewireConfig::default);
100
101 config.get::<LivewireConfig>().unwrap_or(&DEFAULTS)
102 }
103
104 #[must_use]
106 pub fn first_component_location(&self) -> Option<&str> {
107 self.component_locations.first().map(String::as_str)
108 }
109
110 #[must_use]
113 pub fn upload_throttle(&self) -> Option<laravel::http::middleware::Throttle> {
114 let middleware = &self.temporary_file_upload.middleware;
115 Some(laravel::http::middleware::Throttle::per(
116 middleware.max_attempts,
117 ExactDuration::from(Duration::from_secs(middleware.decay_seconds)),
118 ))
119 }
120}
121
122#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
124pub enum ComponentKind {
125 #[default]
127 Sfc,
128 Mfc,
130 Class,
133}
134
135impl ComponentKind {
136 #[must_use]
138 pub const fn as_str(self) -> &'static str {
139 match self {
140 Self::Sfc => "sfc",
141 Self::Mfc => "mfc",
142 Self::Class => "class",
143 }
144 }
145}
146
147impl fmt::Display for ComponentKind {
148 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
149 formatter.write_str(self.as_str())
150 }
151}
152
153#[derive(Clone, Copy, Debug, PartialEq, Eq)]
155pub struct MakeCommand {
156 pub kind: ComponentKind,
158 pub emoji: bool,
160 pub js: bool,
162 pub css: bool,
164 pub test: bool,
166}
167
168impl Default for MakeCommand {
169 fn default() -> Self {
170 Self {
171 kind: ComponentKind::Sfc,
172 emoji: true,
173 js: false,
174 css: false,
175 test: false,
176 }
177 }
178}
179
180#[derive(Clone, Debug, PartialEq, Eq)]
182pub struct TemporaryFileUpload {
183 pub disk: Option<String>,
185 pub rules: Vec<String>,
187 pub directory: Option<String>,
189 pub middleware: UploadMiddleware,
191 pub preview_mimes: Vec<String>,
193 pub max_upload_time: u64,
195 pub cleanup: bool,
197}
198
199impl Default for TemporaryFileUpload {
200 fn default() -> Self {
201 Self {
202 disk: None,
203 rules: vec!["required".to_owned(), "file".to_owned(), "max:12288".to_owned()],
204 directory: None,
205 middleware: UploadMiddleware::default(),
206 preview_mimes: [
207 "png", "gif", "bmp", "svg", "wav", "mp4", "mov", "avi", "wmv", "mp3", "m4a", "jpg", "jpeg", "mpga",
208 "webp", "wma",
209 ]
210 .into_iter()
211 .map(str::to_owned)
212 .collect(),
213 max_upload_time: 5,
214 cleanup: true,
215 }
216 }
217}
218
219impl TemporaryFileUpload {
220 #[must_use]
222 pub fn directory(&self) -> &str {
223 self.directory.as_deref().unwrap_or("livewire-tmp")
224 }
225
226 #[must_use]
228 pub fn max_upload_time(&self) -> ExactDuration {
229 ExactDuration::from(Duration::from_secs(self.max_upload_time.saturating_mul(60)))
230 }
231}
232
233#[derive(Clone, Copy, Debug, PartialEq, Eq)]
235pub struct UploadMiddleware {
236 pub max_attempts: u32,
238 pub decay_seconds: u64,
240}
241
242impl Default for UploadMiddleware {
243 fn default() -> Self {
244 Self {
245 max_attempts: 60,
246 decay_seconds: 60,
247 }
248 }
249}
250
251#[derive(Clone, Debug, PartialEq, Eq)]
253pub struct Navigate {
254 pub show_progress_bar: bool,
256 pub progress_bar_color: String,
258}
259
260impl Default for Navigate {
261 fn default() -> Self {
262 Self {
263 show_progress_bar: true,
264 progress_bar_color: "#2299dd".to_owned(),
265 }
266 }
267}
268
269#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
271pub enum PaginationTheme {
272 #[default]
274 Tailwind,
275 Bootstrap,
277}
278
279impl PaginationTheme {
280 #[must_use]
282 pub const fn as_str(self) -> &'static str {
283 match self {
284 Self::Tailwind => "tailwind",
285 Self::Bootstrap => "bootstrap",
286 }
287 }
288}
289
290impl fmt::Display for PaginationTheme {
291 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
292 formatter.write_str(self.as_str())
293 }
294}
295
296#[derive(Clone, Copy, Debug, PartialEq, Eq)]
298pub struct PayloadLimits {
299 pub max_size: Option<usize>,
301 pub max_nesting_depth: Option<usize>,
303 pub max_calls: Option<usize>,
305 pub max_components: Option<usize>,
307}
308
309impl Default for PayloadLimits {
310 fn default() -> Self {
311 Self {
312 max_size: Some(1_048_576),
313 max_nesting_depth: Some(10),
314 max_calls: Some(50),
315 max_components: Some(200),
316 }
317 }
318}
319
320#[cfg(test)]
321mod tests {
322 use super::*;
323
324 #[test]
325 fn matches_upstream_config_defaults() {
326 let config = LivewireConfig::default();
327
328 assert_eq!(
329 config.component_locations,
330 ["resources/views/components", "resources/views/livewire"]
331 );
332 assert_eq!(config.component_layout.as_deref(), Some("layouts::app"));
333 assert_eq!(config.component_namespaces["pages"], "resources/views/pages");
334 assert_eq!(config.make_command.kind, ComponentKind::Sfc);
335 assert!(config.make_command.emoji);
336 assert_eq!(config.temporary_file_upload.rules, ["required", "file", "max:12288"]);
337 assert_eq!(config.temporary_file_upload.directory(), "livewire-tmp");
338 assert_eq!(config.temporary_file_upload.preview_mimes.len(), 16);
339 assert_eq!(
340 config.temporary_file_upload.max_upload_time(),
341 ExactDuration::from(Duration::from_secs(300))
342 );
343 assert!(config.temporary_file_upload.cleanup);
344 assert_eq!(config.navigate.progress_bar_color, "#2299dd");
345 assert_eq!(config.pagination_theme, PaginationTheme::Tailwind);
346 assert_eq!(config.release_token, "a");
347 assert_eq!(config.payload.max_size, Some(1_048_576));
348 assert_eq!(config.payload.max_nesting_depth, Some(10));
349 assert_eq!(config.payload.max_calls, Some(50));
350 assert_eq!(config.payload.max_components, Some(200));
351 }
352
353 #[test]
354 fn the_upload_throttle_is_a_sixty_per_minute_limit() {
355 let throttle = LivewireConfig::default().upload_throttle().expect("a throttle");
356
357 assert_eq!(
358 throttle,
359 laravel::http::middleware::Throttle::per(60, ExactDuration::from(Duration::from_secs(60)))
360 );
361 }
362}