Skip to main content

server_fn/response/
mod.rs

1/// Response types for Actix.
2#[cfg(feature = "actix-no-default")]
3pub mod actix;
4/// Response types for the browser.
5#[cfg(feature = "browser")]
6pub mod browser;
7#[cfg(feature = "generic")]
8pub mod generic;
9/// Response types for Axum.
10#[cfg(feature = "axum-no-default")]
11pub mod http;
12/// Response types for [`reqwest`].
13#[cfg(feature = "reqwest")]
14pub mod reqwest;
15
16use crate::error::ServerFnErrorResponseParts;
17use bytes::Bytes;
18use futures::Stream;
19use std::future::Future;
20
21/// Represents the response as created by the server;
22pub trait TryRes<E>
23where
24    Self: Sized,
25{
26    /// Attempts to convert a UTF-8 string into an HTTP response.
27    fn try_from_string(content_type: &str, data: String) -> Result<Self, E>;
28
29    /// Attempts to convert a binary blob represented as bytes into an HTTP response.
30    fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, E>;
31
32    /// Attempts to convert a stream of bytes into an HTTP response.
33    fn try_from_stream(
34        content_type: &str,
35        data: impl Stream<Item = Result<Bytes, Bytes>> + Send + 'static,
36    ) -> Result<Self, E>;
37}
38
39/// Represents the response as created by the server;
40pub trait Res {
41    /// Converts an error into a response, with a `500` status code and the serialized error as its body.
42    fn error_response(path: &str, err: ServerFnErrorResponseParts) -> Self;
43    /// Redirect the response by setting a 302 code and Location header.
44    fn redirect(&mut self, path: &str);
45}
46
47/// Represents the response as received by the client.
48pub trait ClientRes<E> {
49    /// Attempts to extract a UTF-8 string from an HTTP response.
50    fn try_into_string(self) -> impl Future<Output = Result<String, E>> + Send;
51
52    /// Attempts to extract a binary blob from an HTTP response.
53    fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, E>> + Send;
54
55    /// Attempts to extract a binary stream from an HTTP response.
56    fn try_into_stream(
57        self,
58    ) -> Result<
59        impl Stream<Item = Result<Bytes, Bytes>> + Send + Sync + 'static,
60        E,
61    >;
62
63    /// HTTP status code of the response.
64    fn status(&self) -> u16;
65
66    /// Status text for the status code.
67    fn status_text(&self) -> String;
68
69    /// The `Location` header or (if none is set), the URL of the response.
70    fn location(&self) -> String;
71
72    /// Whether the response has the [`REDIRECT_HEADER`](crate::redirect::REDIRECT_HEADER) set.
73    fn has_redirect(&self) -> bool;
74}
75
76/// A mocked response type that can be used in place of the actual server response,
77/// when compiling for the browser.
78///
79/// ## Panics
80/// This always panics if its methods are called. It is used solely to stub out the
81/// server response type when compiling for the client.
82pub struct BrowserMockRes;
83
84impl<E> TryRes<E> for BrowserMockRes {
85    fn try_from_string(_content_type: &str, _data: String) -> Result<Self, E> {
86        unreachable!()
87    }
88
89    fn try_from_bytes(_content_type: &str, _data: Bytes) -> Result<Self, E> {
90        unreachable!()
91    }
92
93    fn try_from_stream(
94        _content_type: &str,
95        _data: impl Stream<Item = Result<Bytes, Bytes>>,
96    ) -> Result<Self, E> {
97        unreachable!()
98    }
99}
100
101impl Res for BrowserMockRes {
102    fn error_response(_path: &str, _err: ServerFnErrorResponseParts) -> Self {
103        unreachable!()
104    }
105
106    fn redirect(&mut self, _path: &str) {
107        unreachable!()
108    }
109}