#[derive(Debug, Clone)]
pub struct InertiaConfig {
pub app_name: String,
pub vite_dev_server: String,
pub entry_point: String,
pub version: String,
pub development: bool,
pub html_template: Option<String>,
pub manifest_path: String,
pub title: Option<String>,
pub head_extras: Option<String>,
pub mount_id: String,
}
impl InertiaConfig {
pub fn from_env() -> Self {
let vite_dev_server = std::env::var("VITE_DEV_SERVER")
.unwrap_or_else(|_| "http://localhost:5173".to_string());
let is_dev = !matches!(
std::env::var("APP_ENV").ok().as_deref(),
Some("production") | Some("staging")
);
let app_name = std::env::var("APP_NAME").unwrap_or_else(|_| "Ferro".to_string());
Self {
app_name,
vite_dev_server,
entry_point: std::env::var("VITE_ENTRY_POINT")
.unwrap_or_else(|_| "src/main.tsx".to_string()),
version: std::env::var("INERTIA_VERSION").unwrap_or_else(|_| "1.0".to_string()),
development: is_dev,
html_template: None,
manifest_path: "public/.vite/manifest.json".to_string(),
title: None,
head_extras: None,
mount_id: "app".to_string(),
}
}
pub fn new() -> Self {
Self::default()
}
pub fn vite_dev_server(mut self, url: impl Into<String>) -> Self {
self.vite_dev_server = url.into();
self
}
pub fn entry_point(mut self, entry: impl Into<String>) -> Self {
self.entry_point = entry.into();
self
}
pub fn version(mut self, version: impl Into<String>) -> Self {
self.version = version.into();
self
}
pub fn production(mut self) -> Self {
self.development = false;
self
}
pub fn development(mut self) -> Self {
self.development = true;
self
}
pub fn app_name(mut self, name: impl Into<String>) -> Self {
self.app_name = name.into();
self
}
pub fn manifest_path(mut self, path: impl Into<String>) -> Self {
self.manifest_path = path.into();
self
}
pub fn html_template(mut self, template: impl Into<String>) -> Self {
self.html_template = Some(template.into());
self
}
pub fn title(mut self, t: impl Into<String>) -> Self {
self.title = Some(t.into());
self
}
pub fn head_extras(mut self, h: impl Into<String>) -> Self {
self.head_extras = Some(h.into());
self
}
pub fn mount_id(mut self, id: impl Into<String>) -> Self {
self.mount_id = id.into();
self
}
}
impl Default for InertiaConfig {
fn default() -> Self {
Self::from_env()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_env_reads_defaults() {
let c = InertiaConfig::from_env();
assert_eq!(c.entry_point, "src/main.tsx");
assert_eq!(c.version, "1.0");
assert_eq!(c.mount_id, "app");
}
#[test]
fn new_fields_default_to_none() {
let c = InertiaConfig::from_env();
assert!(c.title.is_none());
assert!(c.head_extras.is_none());
}
#[test]
fn builders_set_new_fields() {
let c = InertiaConfig::new()
.title("My App")
.head_extras(r#"<link rel="icon" href="/favicon.ico">"#)
.mount_id("root");
assert_eq!(c.title.as_deref(), Some("My App"));
assert_eq!(
c.head_extras.as_deref(),
Some(r#"<link rel="icon" href="/favicon.ico">"#)
);
assert_eq!(c.mount_id, "root");
}
#[test]
fn default_equals_from_env_shape() {
let a = InertiaConfig::default();
let b = InertiaConfig::from_env();
assert_eq!(a.entry_point, b.entry_point);
assert_eq!(a.version, b.version);
assert_eq!(a.mount_id, b.mount_id);
}
}