inertia_rust/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
use crate::{
inertia::{ReflashSession, TemplateResolver},
InertiaVersion, SsrClient,
};
use serde_json::{Map, Value};
/// A configuration struct for initializing Inertia. You can directly fill the struct or use
/// the builder fluent syntax by calling `InertiaConfig::builder()`, and finally `InertiaConfig::build()`.
///
/// Note that, even with builder, most of fields are mandatory and trying to build without filling them
/// will cause your application to `panic!`
///
/// * `url` - A valid [href](https://developer.mozilla.org/en-US/docs/Web/API/Location)
/// of the currentapplication
/// * `version` - The current asset version of the application.
/// See [Asset versioning](https://inertiajs.com/asset-versioning) for more
/// details.
/// * `template_path` - The path for the root html template.
/// * `template_resolver` - A function that renders the given root template html. Check
/// more details at [`Inertia::template_resolver`] document string.
/// * `template_resolver_data` - The third parameter of your template resolver. Inertia will
/// pass it by reference when calling the resolver function.
/// If you don't plan to use it, just pass an empty tuple (both here
/// and in your template resolver).
/// * `with_ssr` - Whether Server-side Rendering should be enabled.
/// * `custom_ssr_client` - An [`Option<SsrClient>`] with the Inertia Server address.
/// If `None` is given, `SsrClient::default` will
/// be used.
/// * `view_data` - Optional view data to be passed to the root template. It must be
/// handled by the provided `template_resolver`.
///
/// [`Inertia::template_resolver`]: crate::inertia::Inertia
pub struct InertiaConfig<T, V>
where
T: 'static,
V: ToString,
{
pub url: &'static str,
pub version: InertiaVersion<V>,
pub template_path: &'static str,
pub template_resolver: TemplateResolver<T>,
pub template_resolver_data: &'static T,
pub with_ssr: bool,
pub custom_ssr_client: Option<SsrClient>,
pub view_data: Option<Map<String, Value>>,
pub reflash_inertia_session: ReflashSession,
}
impl<T, V> InertiaConfig<T, V>
where
T: 'static,
V: ToString,
{
/// Instatiates a new InertiaConfigBuilder instance. It must be configured using a fluent syntax.
///
/// # Examples
/// ```rust
/// use inertia_rust::{InertiaVersion, InertiaConfig};
/// # use inertia_rust::{TemplateResolverOutput, ViewData, InertiaError};
/// # async fn _your_template_resolver(_template_path: &str, _view_data: ViewData) -> Result<String, InertiaError> {
/// # return Ok("".to_string());
/// # }
/// #
/// # pub fn your_template_resolver(template_path: &'static str, view_data: ViewData, _data: &()) -> TemplateResolverOutput {
/// # Box::pin(_your_template_resolver(template_path, view_data))
/// # }
/// #
/// let inertia_config = InertiaConfig::builder()
/// .set_url("http://localhost:8080")
/// .set_version(InertiaVersion::Literal("v1"))
/// .set_template_path("path/to/template.html")
/// .set_template_resolver(&your_template_resolver)
/// .set_template_resolver_data(&())
/// .build();
/// ```
pub fn builder() -> InertiaConfigBuilder<T, V> {
InertiaConfigBuilder::new()
}
}
pub struct InertiaConfigBuilder<T, V>
where
T: 'static,
V: ToString,
{
pub url: Option<&'static str>,
pub version: Option<InertiaVersion<V>>,
pub template_path: Option<&'static str>,
pub template_resolver: Option<TemplateResolver<T>>,
pub template_resolver_data: Option<&'static T>,
pub with_ssr: bool,
pub custom_ssr_client: Option<SsrClient>,
pub view_data: Option<Map<String, Value>>,
pub reflash_inertia_session: Option<ReflashSession>,
}
impl<T, V> Default for InertiaConfigBuilder<T, V>
where
T: 'static,
V: ToString,
{
fn default() -> Self {
Self::new()
}
}
impl<T, V> InertiaConfigBuilder<T, V>
where
T: 'static,
V: ToString,
{
/// Instatiates a new InertiaConfigBuilder instance. It must be configured using a fluent syntax.
///
/// # Examples
/// ```rust
/// use inertia_rust::{InertiaConfigBuilder, InertiaVersion};
///
/// # use inertia_rust::{TemplateResolverOutput, ViewData, InertiaError};
/// # async fn _your_template_resolver(_template_path: &str, _view_data: ViewData) -> Result<String, InertiaError> {
/// # return Ok("".to_string());
/// # }
/// # pub fn your_template_resolver(template_path: &'static str, view_data: ViewData, _data: &()) -> TemplateResolverOutput {
/// # Box::pin(_your_template_resolver(template_path, view_data))
/// # }
/// #
/// let inertia_config = InertiaConfigBuilder::new()
/// .set_url("http://localhost:8080")
/// .set_version(InertiaVersion::Literal("v1"))
/// .set_template_path("path/to/template.html")
/// .set_template_resolver(&your_template_resolver)
/// .set_template_resolver_data(&())
/// .build();
/// ```
pub fn new() -> Self {
Self {
url: None,
version: None,
template_path: None,
template_resolver: None,
template_resolver_data: None,
view_data: None,
with_ssr: false,
custom_ssr_client: None,
reflash_inertia_session: None,
}
}
pub fn set_ssr_client(mut self, ssr_client: SsrClient) -> Self {
self.custom_ssr_client = Some(ssr_client);
self
}
pub fn set_url(mut self, url: &'static str) -> Self {
self.url = Some(url);
self
}
pub fn set_version(mut self, version: InertiaVersion<V>) -> Self {
self.version = Some(version);
self
}
pub fn set_template_path(mut self, template_path: &'static str) -> Self {
self.template_path = Some(template_path);
self
}
pub fn set_template_resolver(mut self, template_resolver: TemplateResolver<T>) -> Self {
self.template_resolver = Some(template_resolver);
self
}
pub fn set_template_resolver_data(mut self, data: &'static T) -> Self {
self.template_resolver_data = Some(data);
self
}
pub fn set_view_data(mut self, view_data: Map<String, Value>) -> Self {
self.view_data = Some(view_data);
self
}
pub fn set_reflash_fn(mut self, reflash_inertia_session_fn: ReflashSession) -> Self {
self.reflash_inertia_session = Some(reflash_inertia_session_fn);
self
}
pub fn enable_ssr(mut self) -> Self {
self.with_ssr = true;
self
}
/// Compile the current `InertiaConfigBuilder` into a valid `InertiaConfig` struct.
///
/// # Panics
/// Panics if any of the following fields equal [`None`]:
/// * `url`
/// * `template_path`
/// * `template_resolver`
/// * `template_resolver_data`
/// * `version`
pub fn build(self) -> InertiaConfig<T, V> {
if self.url.is_none() {
panic!(
"[InertiaConfigBuilder] 'url' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
if self.template_path.is_none() {
panic!(
"[InertiaConfigBuilder] 'template_path' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
if self.template_resolver.is_none() {
panic!(
"[InertiaConfigBuilder] 'template_resolver' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
if self.template_resolver_data.is_none() {
panic!(
"[InertiaConfigBuilder] 'template_resolver_data' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
if self.version.is_none() {
panic!(
"[InertiaConfigBuilder] 'version' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
InertiaConfig {
url: self.url.unwrap(),
template_path: self.template_path.unwrap(),
template_resolver: self.template_resolver.unwrap(),
template_resolver_data: self.template_resolver_data.unwrap(),
version: self.version.unwrap(),
view_data: self.view_data,
with_ssr: self.with_ssr,
custom_ssr_client: self.custom_ssr_client,
reflash_inertia_session: self.reflash_inertia_session.unwrap_or(Box::new(|_| Ok(()))),
}
}
}
#[cfg(test)]
mod test {
use crate::{InertiaError, InertiaVersion, TemplateResolverOutput, ViewData};
use std::panic;
use super::{InertiaConfig, InertiaConfigBuilder};
// region: --- Mocks
async fn _mocked_resolver(
_template_path: &str,
_view_data: ViewData,
) -> Result<String, InertiaError> {
Ok("".to_string())
}
pub fn mocked_resolver(
template_path: &'static str,
view_data: ViewData,
_data: &(),
) -> TemplateResolverOutput {
Box::pin(_mocked_resolver(template_path, view_data))
}
// endregion: --- Mocks
// region: --- Tests
#[test]
fn builder_panics_if_critical_fields_are_unset() {
// region: --- builders
let build_totally_empty = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new().build();
});
let build_without_url = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_template_resolver(&mocked_resolver)
.set_template_path("path")
.set_template_resolver_data(&())
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_template_resolver = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_path("path")
.set_template_resolver_data(&())
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_template_path = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_resolver(&mocked_resolver)
.set_template_resolver_data(&())
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_template_data = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_resolver(&mocked_resolver)
.set_template_path("path")
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_version = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_resolver(&mocked_resolver)
.set_template_path("path")
.set_template_resolver_data(&())
.build()
});
let build_with_critical_fields_filled = panic::catch_unwind(move || {
InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_resolver(&mocked_resolver)
.set_template_path("path")
.set_template_resolver_data(&())
.set_version(InertiaVersion::Literal("v1"))
.build()
});
// endregion: --- builders
assert!(build_totally_empty.is_err());
assert!(build_without_url.is_err());
assert!(build_without_template_resolver.is_err());
assert!(build_without_template_path.is_err());
assert!(build_without_template_data.is_err());
assert!(build_without_version.is_err());
assert!(build_with_critical_fields_filled.is_ok());
}
#[test]
fn builder_builds_correctly() {
let with_builder = InertiaConfigBuilder::<(), &str>::new()
.set_url("foo")
.set_template_resolver(&mocked_resolver)
.set_template_path("path")
.set_template_resolver_data(&())
.set_version(InertiaVersion::Literal("v1"))
.build();
let directly_initialized = InertiaConfig {
url: "foo",
template_resolver: &mocked_resolver,
template_path: "path",
template_resolver_data: &(),
version: InertiaVersion::Literal("v1"),
view_data: None,
with_ssr: false,
custom_ssr_client: None,
reflash_inertia_session: Box::new(|_| Ok(())),
};
assert_eq!(&with_builder.url, &directly_initialized.url);
assert_eq!(
&with_builder.template_path,
&directly_initialized.template_path
);
assert_eq!(
&with_builder.template_resolver_data,
&directly_initialized.template_resolver_data
);
assert_eq!(
&with_builder.version.resolve(),
&directly_initialized.version.resolve()
);
assert_eq!(&with_builder.view_data, &directly_initialized.view_data);
assert_eq!(&with_builder.with_ssr, &directly_initialized.with_ssr);
assert_eq!(
&with_builder.custom_ssr_client,
&directly_initialized.custom_ssr_client
);
}
// endregion: --- Tests
}