use crate::{template_resolver::TemplateResolver, InertiaVersion, SsrClient};
pub struct InertiaConfig<V>
where
V: ToString,
{
pub url: &'static str,
pub version: InertiaVersion<V>,
pub template_resolver: Box<dyn TemplateResolver + Send + Sync>,
pub with_ssr: bool,
pub custom_ssr_client: Option<SsrClient>,
pub encrypt_history: bool,
}
impl<V> InertiaConfig<V>
where
V: ToString,
{
pub fn builder() -> InertiaConfigBuilder<V> {
InertiaConfigBuilder::new()
}
}
pub struct InertiaConfigBuilder<V>
where
V: ToString,
{
pub url: Option<&'static str>,
pub version: Option<InertiaVersion<V>>,
pub template_resolver: Option<Box<dyn TemplateResolver + Send + Sync>>,
pub with_ssr: bool,
pub custom_ssr_client: Option<SsrClient>,
pub encrypt_history: bool,
}
impl<V> Default for InertiaConfigBuilder<V>
where
V: ToString,
{
fn default() -> Self {
Self::new()
}
}
impl<V> InertiaConfigBuilder<V>
where
V: ToString,
{
pub fn new() -> Self {
Self {
url: None,
version: None,
template_resolver: None,
with_ssr: false,
custom_ssr_client: None,
encrypt_history: false,
}
}
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_resolver(
mut self,
template_resolver: Box<dyn TemplateResolver + Send + Sync>,
) -> Self {
self.template_resolver = Some(template_resolver);
self
}
pub fn enable_ssr(mut self) -> Self {
self.with_ssr = true;
self
}
pub fn encrypt_history(mut self) -> Self {
self.encrypt_history = true;
self
}
pub fn build(self) -> InertiaConfig<V> {
if self.url.is_none() {
panic!(
"[InertiaConfigBuilder] 'url' 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.version.is_none() {
panic!(
"[InertiaConfigBuilder] 'version' is a mandatory field and InertiaConfigBuilder cannot build without it.");
}
InertiaConfig {
url: self.url.unwrap(),
template_resolver: self.template_resolver.unwrap(),
version: self.version.unwrap(),
with_ssr: self.with_ssr,
custom_ssr_client: self.custom_ssr_client,
encrypt_history: self.encrypt_history,
}
}
}
#[cfg(test)]
mod test {
use crate::{template_resolver::TemplateResolver, InertiaError, InertiaVersion, ViewData};
use std::panic;
use super::{InertiaConfig, InertiaConfigBuilder};
#[derive(PartialEq, Eq)]
struct MyTemplateResolver;
#[async_trait::async_trait(?Send)]
impl TemplateResolver for MyTemplateResolver {
async fn resolve_template(&self, _view_data: ViewData<'_>) -> Result<String, InertiaError> {
Ok("".to_string())
}
}
#[test]
fn builder_panics_if_critical_fields_are_unset() {
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(Box::new(MyTemplateResolver))
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_template_resolver = panic::catch_unwind(move || {
InertiaConfigBuilder::<&str>::new()
.set_url("foo")
.set_version(InertiaVersion::Literal("v1"))
.build()
});
let build_without_version = panic::catch_unwind(move || {
InertiaConfigBuilder::<&str>::new()
.set_url("foo")
.set_template_resolver(Box::new(MyTemplateResolver))
.build()
});
let build_with_critical_fields_filled = panic::catch_unwind(move || {
InertiaConfigBuilder::<&str>::new()
.set_url("foo")
.set_template_resolver(Box::new(MyTemplateResolver))
.set_version(InertiaVersion::Literal("v1"))
.build()
});
assert!(build_totally_empty.is_err());
assert!(build_without_url.is_err());
assert!(build_without_template_resolver.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(Box::new(MyTemplateResolver))
.set_version(InertiaVersion::Literal("v1"))
.build();
let directly_initialized = InertiaConfig {
url: "foo",
template_resolver: Box::new(MyTemplateResolver),
version: InertiaVersion::Literal("v1"),
with_ssr: false,
custom_ssr_client: None,
encrypt_history: false,
};
assert_eq!(&with_builder.url, &directly_initialized.url);
assert_eq!(
&with_builder.version.resolve(),
&directly_initialized.version.resolve()
);
assert_eq!(&with_builder.with_ssr, &directly_initialized.with_ssr);
assert_eq!(
&with_builder.custom_ssr_client,
&directly_initialized.custom_ssr_client
);
}
}