inertia_rust/
facade.rs

1use std::collections::HashMap;
2
3use crate::{Component, InertiaError, InertiaProps};
4use async_trait::async_trait;
5use serde_json::Value;
6
7#[async_trait(?Send)]
8pub trait InertiaFacade<THttpRequest, TResponse, TRedirect> {
9    /// Renders an Inertia Page as an HTTP response.
10    ///
11    /// # Arguments
12    /// * `req`         -   A reference to the HTTP request.
13    /// * `component`   -   The page javascript component name to be rendered by the
14    ///                     client-side adapter.
15    ///
16    /// # Panic
17    /// Panics if Inertia instance hasn't been configured (set to AppData).
18    async fn render(req: &THttpRequest, component: Component) -> Result<TResponse, InertiaError>;
19
20    /// Renders an Inertia Page with props as an HTTP response.
21    ///
22    /// # Arguments
23    /// * `req`         -   A reference to the HTTP request.
24    /// * `component`   -   The page component to be rendered by the client-side adapter.
25    /// * `props`       -   A `TProps` (serializable) struct containing
26    ///                     the props to be sent to the client-side.
27    ///
28    /// # Errors
29    /// This operation may result in one of InertiaErrors if the props struct
30    /// or any of its fields don't implement [`Serialize`] trait.
31    ///
32    /// # Panics
33    /// Panics if Inertia instance hasn't been configured (set to AppData).
34    ///
35    /// [`Serialize`]: serde::Serialize
36    async fn render_with_props(
37        req: &THttpRequest,
38        component: Component,
39        props: InertiaProps<'_>,
40    ) -> Result<TResponse, InertiaError>;
41
42    /// Provokes a client-side redirect to an extern URL.
43    ///
44    /// # Arguments
45    /// * `req`     - A reference to the HTTP request.
46    /// * `url`     - The URL to be redirected to.
47    fn location(req: &THttpRequest, url: &str) -> TResponse;
48
49    /// Whether to encrypt or not the current request. Refer to [History Encrypt] for more
50    /// details.
51    ///
52    /// [History Encrypt]: https://kaiofelps.github.io/inertia-rust/history-encrypt
53    fn encrypt_history(req: &THttpRequest, encrypt: bool);
54
55    /// Triggers a history clearing from server-side. Refer to [History Encrypt] for more
56    /// details.
57    ///
58    /// [History Encrypt]: https://kaiofelps.github.io/inertia-rust/history-encrypt#clearing-history
59    fn clear_history(req: &THttpRequest);
60
61    /// Triggers a redirect to the previous URL (or "/", if there is no previous URL).
62    /// Please refer to [Flash Messages and Validation Errors] for more information.
63    ///
64    /// [Flash Messages and Validation Errors]: https://kaiofelps.github.io/inertia-rust/advanced/flash-messages.html
65    fn back(req: &THttpRequest) -> TRedirect;
66
67    /// Triggers a redirect to the previous URL (or "/", if there is no previous URL) with
68    /// errors. Please refer to [Flash Messages and Validation Errors] for more information.
69    ///
70    /// [Flash Messages and Validation Errors]: https://kaiofelps.github.io/inertia-rust/advanced/flash-messages.html
71    fn back_with_errors<Key: ToString>(
72        req: &THttpRequest,
73        errors: HashMap<Key, Value>,
74    ) -> TRedirect;
75
76    /// Inserts custom view data to be accessed by the template root.
77    fn view_data(req: &THttpRequest, data: HashMap<&str, Value>);
78}