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
use crate::{template_resolver::TemplateResolver, InertiaVersion, SsrClient};

/// 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 to the root html template.
/// * `template_resolver`       -   A valid template resolver. Check [Template Resolvers] chapter for more details.
/// * `with_ssr`                -   Whether Server-side Rendering should be enabled or not.
/// * `custom_ssr_client`       -   An [`Option<SsrClient>`] with the Inertia Server address.
///                                 If `None` is given, `SsrClient::default` will
///                                 be used.
/// * `encrypt_history`         -   Whether to encrypt or not the session. Refer to [History encryption]
///                                 for more details.
///
/// [Template Resolvers]: https://kaiofelps.github.io/inertia-rust/advanced/template_resolvers.html
/// [Flash Messages and Validation Errors]: https://kaiofelps.github.io/inertia-rust/advanced/flash-messages.html
/// [History encryption]: https://inertiajs.com/history-encryption
pub struct InertiaConfig<V>
where
    V: ToString,
{
    pub url: &'static str,
    pub version: InertiaVersion<V>,
    pub template_path: &'static str,
    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,
{
    /// Instatiates a new InertiaConfigBuilder instance. It must be configured using a fluent syntax.
    ///
    /// # Examples
    /// ```rust
    /// use inertia_rust::{InertiaVersion, InertiaConfig};
    /// # use inertia_rust::{ViewData, InertiaError, template_resolvers::TemplateResolver};
    /// #
    /// #   struct YourTemplateResolver;
    /// #
    /// #   #[async_trait::async_trait(?Send)]
    /// #   impl TemplateResolver for YourTemplateResolver {
    /// #       async fn resolve_template(
    /// #           &self,
    /// #           template_path: &str,
    /// #           view_data: ViewData<'_>,
    /// #       ) -> Result<String, InertiaError> {
    /// #           // import the layout root and render it using your template engine
    /// #           // lets pretend we rendered it, so it ended up being the html output below!
    /// #           Ok("<h1>my rendered page!</h1>".to_string())
    /// #       }
    /// #   }
    /// #
    /// 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(Box::new(YourTemplateResolver))
    ///     .build();
    /// ```
    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_path: Option<&'static str>,
    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,
{
    /// Instatiates a new InertiaConfigBuilder instance. It must be configured using a fluent syntax.
    ///
    /// # Examples
    /// ```rust
    /// use inertia_rust::{InertiaConfigBuilder, InertiaVersion};
    ///
    /// # use inertia_rust::{template_resolvers::TemplateResolver, ViewData, InertiaError};
    /// #   struct YourTemplateResolver;
    /// #
    /// #   #[async_trait::async_trait(?Send)]
    /// #   impl TemplateResolver for YourTemplateResolver {
    /// #       async fn resolve_template(
    /// #           &self,
    /// #           template_path: &str,
    /// #           view_data: ViewData<'_>,
    /// #       ) -> Result<String, InertiaError> {
    /// #           // import the layout root and render it using your template engine
    /// #           // lets pretend we rendered it, so it ended up being the html output below!
    /// #           Ok("<h1>my rendered page!</h1>".to_string())
    /// #       }
    /// #   }
    /// #
    /// 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(Box::new(YourTemplateResolver))
    ///     .build();
    /// ```
    pub fn new() -> Self {
        Self {
            url: None,
            version: None,
            template_path: 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_path(mut self, template_path: &'static str) -> Self {
        self.template_path = Some(template_path);
        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
    }

    /// Compile the current `InertiaConfigBuilder` into a valid `InertiaConfig` struct.
    ///
    /// # Panics
    /// Panics if any of the following fields equal [`None`]:
    /// * `url`
    /// * `template_path`
    /// * `template_resolver`
    /// * `version`
    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_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.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(),
            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};

    // region: --- Mocks

    #[derive(PartialEq, Eq)]
    struct MyTemplateResolver;

    #[async_trait::async_trait(?Send)]
    impl TemplateResolver for MyTemplateResolver {
        async fn resolve_template(
            &self,
            _path: &str,
            _view_data: ViewData<'_>,
        ) -> Result<String, InertiaError> {
            Ok("".to_string())
        }
    }

    // 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(Box::new(MyTemplateResolver))
                .set_template_path("path")
                .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_version(InertiaVersion::Literal("v1"))
                .build()
        });

        let build_without_template_path = panic::catch_unwind(move || {
            InertiaConfigBuilder::<&str>::new()
                .set_url("foo")
                .set_template_resolver(Box::new(MyTemplateResolver))
                .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))
                .set_template_path("path")
                .build()
        });

        let build_with_critical_fields_filled = panic::catch_unwind(move || {
            InertiaConfigBuilder::<&str>::new()
                .set_url("foo")
                .set_template_resolver(Box::new(MyTemplateResolver))
                .set_template_path("path")
                .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_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_template_path("path")
            .set_version(InertiaVersion::Literal("v1"))
            .build();

        let directly_initialized = InertiaConfig {
            url: "foo",
            template_resolver: Box::new(MyTemplateResolver),
            template_path: "path",
            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.template_path,
            &directly_initialized.template_path
        );
        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
        );
    }

    // endregion: --- Tests
}