inertia_rust/
page.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
use std::collections::HashMap;

use crate::inertia::Component;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// Inertia Full Page response to be rendered inside the root template
/// on the first request or on a full visit request.
#[derive(Deserialize)]
pub struct InertiaSSRPage {
    /// All html-string elements to be injected in inertia_head, at the root template.
    pub(crate) head: Vec<String>,
    /// All html-string elements to be injected in inertia_body div container, at the root template.
    pub(crate) body: String,
}

impl InertiaSSRPage {
    /// Instantiates a new InertiaSSRPage object. See [`InertiaSSRPage`] struct docs for more
    /// details of its usage.
    ///
    /// [`InertiaSSRPage`]: InertiaSSRPage
    ///
    /// # Arguments
    /// * `head` -  A stringified html of the content to be injected in the layout
    ///             (given by [template_path]) head element (by innerHTML method).
    /// * `body` -  A stringified html of the body to be injected in the Inertia's div container
    ///             in the layout.
    ///
    /// [template_path]: crate::inertia::Inertia
    ///
    pub fn new(head: Vec<String>, body: String) -> Self {
        InertiaSSRPage { head, body }
    }

    pub fn get_head(&self) -> String {
        self.head.join("\n")
    }
    pub fn get_body(&self) -> &String {
        &self.body
    }
}

pub type DeferredProps<'a> = Option<HashMap<&'a str, Vec<&'a str>>>;

/// Response containing a valid Inertia Payload that will be used
/// by the Inertia client to render the components.
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct InertiaPage<'a> {
    // The name of the JavaScript page component.
    pub(crate) component: Component,

    // A merge of page props and shared props .
    pub(crate) props: Map<String, Value>,

    // this is not the same as Inertia::url, that represents the application url.
    // this url represents the current request's url, i.e. the page url.
    pub(crate) url: &'a str,

    /// Current assets version.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) version: Option<&'a str>,

    #[serde(rename = "clearHistory")]
    pub(crate) clear_history: bool,

    #[serde(rename = "encryptHistory")]
    pub(crate) encrypt_history: bool,

    #[serde(rename = "deferredProps")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) deferred_props: DeferredProps<'a>,

    #[serde(rename = "mergeProps")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) merge_props: Option<Vec<&'a str>>,
}

#[allow(clippy::too_many_arguments)]
impl<'a> InertiaPage<'a> {
    /// Instantiates an Inertia Page object to sent as http response,
    /// according to [Inertia Protocol].
    ///
    ///
    /// # Arguments
    /// * `component`       -   The name of the javascript page component (e.g. "/Me").
    /// * `url`             -   The Inertia instance's url (the application URL). It can be a
    ///                         whole href or an absolute hostless path ("/me").
    /// * `version`         -   Current assets version. Used to assert assets are up-to-date. See
    ///                         [Inertia's assets versioning] page for more details.
    /// * `props`           -   A map of the page's props.
    /// * `merge_props`     -   A list containing the keys of the properties that shall me merged by the
    ///                         client-side adapter.
    /// * `deferred_props`  -   A hashmap of which the keys are groups. It contains the keys of the props
    ///                         that must be fetched by the client-side adapter after the first page load.
    ///                         Refer to [Deferred Props] for more details.
    /// * `clear_history`   -   Whether the history must be cleaned by the client-side once this response is
    ///                         received. Refer to [Clearing history] section from History Encryption documentatin
    ///                         for more details.
    /// * `encrypt_history` -   Whether the client-side adapter must encrypt the history page data related to
    ///                         this response. Refer to [History Encryption] for more details.
    ///
    /// [Inertia Protocol]: https://inertiajs.com/the-protocol
    /// [Inertia's assets versioning]: https://inertiajs.com/the-protocol#asset-versioning
    /// [Deferred Props]: https://inertiajs.com/deferred-props
    /// [History Encryption]: https://inertiajs.com/history-encryption
    /// [Clearing history]: https://inertiajs.com/history-encryption#clearing-history
    pub fn new(
        component: Component,
        url: &'a str,
        version: Option<&'a str>,
        props: Map<String, Value>,
        merge_props: Option<Vec<&'a str>>,
        deferred_props: DeferredProps<'a>,
        clear_history: bool,
        encrypt_history: bool,
    ) -> Self {
        InertiaPage {
            component,
            url,
            props,
            version,
            merge_props,
            deferred_props,
            clear_history,
            encrypt_history,
        }
    }

    pub fn get_props(&self) -> &Map<String, Value> {
        &self.props
    }

    pub fn get_component(&self) -> &Component {
        &self.component
    }

    pub fn get_url(&self) -> &str {
        self.url
    }

    pub fn get_version(&self) -> &Option<&str> {
        &self.version
    }

    pub fn get_clear_history(&self) -> bool {
        self.clear_history
    }

    pub fn get_encrypt_history(&self) -> bool {
        self.encrypt_history
    }

    pub fn get_deferred_props(&self) -> &DeferredProps {
        &self.deferred_props
    }

    pub fn get_merge_props(&self) -> &Option<Vec<&str>> {
        &self.merge_props
    }
}