inertia_rust/
page.rs

1use std::collections::HashMap;
2
3use crate::inertia::Component;
4use serde::{Deserialize, Serialize};
5use serde_json::{Map, Value};
6
7/// Inertia Full Page response to be rendered inside the root template
8/// on the first request or on a full visit request.
9#[derive(Deserialize)]
10pub struct InertiaSSRPage {
11    /// All html-string elements to be injected in inertia_head, at the root template.
12    pub(crate) head: Vec<String>,
13    /// All html-string elements to be injected in inertia_body div container, at the root template.
14    pub(crate) body: String,
15}
16
17impl InertiaSSRPage {
18    /// Instantiates a new InertiaSSRPage object. See [`InertiaSSRPage`] struct docs for more
19    /// details of its usage.
20    ///
21    /// [`InertiaSSRPage`]: InertiaSSRPage
22    ///
23    /// # Arguments
24    /// * `head` -  A stringified html of the content to be injected in the layout
25    ///             (given by [template_path]) head element (by innerHTML method).
26    /// * `body` -  A stringified html of the body to be injected in the Inertia's div container
27    ///             in the layout.
28    ///
29    /// [template_path]: crate::inertia::Inertia
30    ///
31    pub fn new(head: Vec<String>, body: String) -> Self {
32        InertiaSSRPage { head, body }
33    }
34
35    pub fn get_head(&self) -> String {
36        self.head.join("\n")
37    }
38
39    pub fn get_body(&self) -> String {
40        self.body.clone()
41    }
42}
43
44pub type DeferredProps<'a> = Option<HashMap<&'a str, Vec<&'a str>>>;
45
46/// Response containing a valid Inertia Payload that will be used
47/// by the Inertia client to render the components.
48#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
49pub struct InertiaPage<'a> {
50    // The name of the JavaScript page component.
51    pub(crate) component: Component,
52
53    // A merge of page props and shared props .
54    pub(crate) props: Map<String, Value>,
55
56    // this is not the same as Inertia::url, that represents the application url.
57    // this url represents the current request's url, i.e. the page url.
58    pub(crate) url: &'a str,
59
60    /// Current assets version.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub(crate) version: Option<&'a str>,
63
64    #[serde(rename = "clearHistory")]
65    pub(crate) clear_history: bool,
66
67    #[serde(rename = "encryptHistory")]
68    pub(crate) encrypt_history: bool,
69
70    #[serde(rename = "deferredProps")]
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub(crate) deferred_props: DeferredProps<'a>,
73
74    #[serde(rename = "mergeProps")]
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub(crate) merge_props: Option<Vec<&'a str>>,
77}
78
79#[allow(clippy::too_many_arguments)]
80impl<'a> InertiaPage<'a> {
81    /// Instantiates an Inertia Page object to sent as http response,
82    /// according to [Inertia Protocol].
83    ///
84    ///
85    /// # Arguments
86    /// * `component`       -   The name of the javascript page component (e.g. "/Me").
87    /// * `url`             -   The Inertia instance's url (the application URL). It can be a
88    ///                         whole href or an absolute hostless path ("/me").
89    /// * `version`         -   Current assets version. Used to assert assets are up-to-date. See
90    ///                         [Inertia's assets versioning] page for more details.
91    /// * `props`           -   A map of the page's props.
92    /// * `merge_props`     -   A list containing the keys of the properties that shall me merged by the
93    ///                         client-side adapter.
94    /// * `deferred_props`  -   A hashmap of which the keys are groups. It contains the keys of the props
95    ///                         that must be fetched by the client-side adapter after the first page load.
96    ///                         Refer to [Deferred Props] for more details.
97    /// * `clear_history`   -   Whether the history must be cleaned by the client-side once this response is
98    ///                         received. Refer to [Clearing history] section from History Encryption documentatin
99    ///                         for more details.
100    /// * `encrypt_history` -   Whether the client-side adapter must encrypt the history page data related to
101    ///                         this response. Refer to [History Encryption] for more details.
102    ///
103    /// [Inertia Protocol]: https://inertiajs.com/the-protocol
104    /// [Inertia's assets versioning]: https://inertiajs.com/the-protocol#asset-versioning
105    /// [Deferred Props]: https://inertiajs.com/deferred-props
106    /// [History Encryption]: https://inertiajs.com/history-encryption
107    /// [Clearing history]: https://inertiajs.com/history-encryption#clearing-history
108    pub fn new(
109        component: Component,
110        url: &'a str,
111        version: Option<&'a str>,
112        props: Map<String, Value>,
113        merge_props: Option<Vec<&'a str>>,
114        deferred_props: DeferredProps<'a>,
115        clear_history: bool,
116        encrypt_history: bool,
117    ) -> Self {
118        InertiaPage {
119            component,
120            url,
121            props,
122            version,
123            merge_props,
124            deferred_props,
125            clear_history,
126            encrypt_history,
127        }
128    }
129
130    pub fn get_props(&self) -> &Map<String, Value> {
131        &self.props
132    }
133
134    pub fn get_component(&self) -> &Component {
135        &self.component
136    }
137
138    pub fn get_url(&self) -> &str {
139        self.url
140    }
141
142    pub fn get_version(&self) -> &Option<&str> {
143        &self.version
144    }
145
146    pub fn get_clear_history(&self) -> bool {
147        self.clear_history
148    }
149
150    pub fn get_encrypt_history(&self) -> bool {
151        self.encrypt_history
152    }
153
154    pub fn get_deferred_props(&self) -> &DeferredProps {
155        &self.deferred_props
156    }
157
158    pub fn get_merge_props(&self) -> &Option<Vec<&str>> {
159        &self.merge_props
160    }
161}