Repository: leptos-rs/leptos
File: server_fn/src/response/mod.rs (Rust)

Code before:
use std::future::Future;

/// Represents the response as created by the server;
pub trait TryRes<E>
where
    Self: Sized,
{
    /// Attempts to convert a UTF-8 string into an HTTP response.
    fn try_from_string(content_type: &str, data: String) -> Result<Self, E>;

    /// Attempts to convert a binary blob represented as bytes into an HTTP response.
    fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, E>;

    /// Attempts to convert a stream of bytes into an HTTP response.
    fn try_from_stream(
        content_type: &str,
        data: impl Stream<Item = Result<Bytes, Bytes>> + Send + 'static,
    ) -> Result<Self, E>;
}

/// Represents the response as created by the server;
pub trait Res {
    /// Converts an error into a response, with a `500` status code and the error text as its body.
    fn error_response(path: &str, err: Bytes) -> Self;
    /// Set the content type header for the response.
    fn content_type(&mut self, #[allow(unused_variables)] content_type: &str) {}
    /// Redirect the response by setting a 302 code and Location header.
    fn redirect(&mut self, path: &str);
}

/// Represents the response as received by the client.
pub trait ClientRes<E> {
    /// Attempts to extract a UTF-8 string from an HTTP response.
    fn try_into_string(self) -> impl Future<Output = Result<String, E>> + Send;

    /// Attempts to extract a binary blob from an HTTP response.
    fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, E>> + Send;

    /// Attempts to extract a binary stream from an HTTP response.
    fn try_into_stream(
        self,
    ) -> Result<
        impl Stream<Item = Result<Bytes, Bytes>> + Send + Sync + 'static,
        E,
    >;

    /// HTTP status code of the response.
    fn status(&self) -> u16;

    /// Status text for the status code.
    fn status_text(&self) -> String;

Review comment (suggestion):
Using `#[allow(unused_variables)]` here instead of naming the parameter `_content_type` so that when IDEs generate a placeholder implementation, the parameter name is `content_type` instead of `_content_type`.

Code after:
use std::future::Future;

/// Represents the response as created by the server;
pub trait TryRes<E>
where
    Self: Sized,
{
    /// Attempts to convert a UTF-8 string into an HTTP response.
    fn try_from_string(content_type: &str, data: String) -> Result<Self, E>;

    /// Attempts to convert a binary blob represented as bytes into an HTTP response.
    fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, E>;

    /// Attempts to convert a stream of bytes into an HTTP response.
    fn try_from_stream(
        content_type: &str,
        data: impl Stream<Item = Result<Bytes, Bytes>> + Send + 'static,
    ) -> Result<Self, E>;
}

/// Represents the response as created by the server;
pub trait Res {
    /// Converts an error into a response, with a `500` status code and the error as its body.
    fn error_response(path: &str, err: Bytes) -> Self;
    /// Set the `Content-Type` header for the response.
    fn content_type(&mut self, #[allow(unused_variables)] content_type: &str) {
        // TODO 0.9: remove this method and default implementation. It is only included here
        //  to allow setting the `Content-Type` header for error responses without requiring a
        //  semver-incompatible change.
    }
    /// Redirect the response by setting a 302 code and Location header.
    fn redirect(&mut self, path: &str);
}

/// Represents the response as received by the client.
pub trait ClientRes<E> {
    /// Attempts to extract a UTF-8 string from an HTTP response.
    fn try_into_string(self) -> impl Future<Output = Result<String, E>> + Send;

    /// Attempts to extract a binary blob from an HTTP response.
    fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, E>> + Send;

    /// Attempts to extract a binary stream from an HTTP response.
    fn try_into_stream(
        self,
    ) -> Result<
        impl Stream<Item = Result<Bytes, Bytes>> + Send + Sync + 'static,
        E,
    >;

    /// HTTP status code of the response.