Skip to main content

ferro_inertia/
config.rs

1//! Configuration for Inertia.js integration.
2
3/// Configuration for Inertia.js responses.
4///
5/// # Example
6///
7/// ```rust
8/// use ferro_inertia::InertiaConfig;
9///
10/// // Development configuration (default)
11/// let config = InertiaConfig::default();
12///
13/// // Production configuration
14/// let config = InertiaConfig::new()
15///     .version("1.0.0")
16///     .production();
17///
18/// // Custom Vite dev server
19/// let config = InertiaConfig::new()
20///     .vite_dev_server("http://localhost:3000")
21///     .entry_point("src/app.tsx");
22/// ```
23#[derive(Debug, Clone)]
24pub struct InertiaConfig {
25    /// Vite dev server URL (e.g., "http://localhost:5173")
26    pub vite_dev_server: String,
27    /// Entry point for the frontend (e.g., "src/main.tsx")
28    pub entry_point: String,
29    /// Asset version for cache busting
30    pub version: String,
31    /// Whether we're in development mode (use Vite dev server)
32    pub development: bool,
33    /// Custom HTML template (if None, uses default)
34    pub html_template: Option<String>,
35}
36
37impl Default for InertiaConfig {
38    fn default() -> Self {
39        let vite_dev_server = std::env::var("VITE_DEV_SERVER")
40            .unwrap_or_else(|_| "http://localhost:5173".to_string());
41
42        let is_dev = !matches!(
43            std::env::var("APP_ENV").ok().as_deref(),
44            Some("production") | Some("staging")
45        );
46
47        Self {
48            vite_dev_server,
49            entry_point: "src/main.tsx".to_string(),
50            version: "1.0".to_string(),
51            development: is_dev,
52            html_template: None,
53        }
54    }
55}
56
57impl InertiaConfig {
58    /// Create a new configuration with default values.
59    pub fn new() -> Self {
60        Self::default()
61    }
62
63    /// Set the Vite dev server URL.
64    pub fn vite_dev_server(mut self, url: impl Into<String>) -> Self {
65        self.vite_dev_server = url.into();
66        self
67    }
68
69    /// Set the frontend entry point.
70    pub fn entry_point(mut self, entry: impl Into<String>) -> Self {
71        self.entry_point = entry.into();
72        self
73    }
74
75    /// Set the asset version for cache busting.
76    pub fn version(mut self, version: impl Into<String>) -> Self {
77        self.version = version.into();
78        self
79    }
80
81    /// Enable production mode (disables Vite dev server integration).
82    pub fn production(mut self) -> Self {
83        self.development = false;
84        self
85    }
86
87    /// Enable development mode (enables Vite dev server integration).
88    pub fn development(mut self) -> Self {
89        self.development = true;
90        self
91    }
92
93    /// Set a custom HTML template.
94    ///
95    /// The template should contain the following placeholders:
96    /// - `{page}` - The escaped JSON page data
97    /// - `{csrf}` - The CSRF token (optional)
98    ///
99    /// # Example
100    ///
101    /// ```rust
102    /// use ferro_inertia::InertiaConfig;
103    ///
104    /// let template = r#"
105    /// <!DOCTYPE html>
106    /// <html>
107    /// <head><title>My App</title></head>
108    /// <body>
109    ///     <div id="app" data-page="{page}"></div>
110    ///     <script src="/app.js"></script>
111    /// </body>
112    /// </html>
113    /// "#;
114    ///
115    /// let config = InertiaConfig::new()
116    ///     .html_template(template);
117    /// ```
118    pub fn html_template(mut self, template: impl Into<String>) -> Self {
119        self.html_template = Some(template.into());
120        self
121    }
122}