Trait FromRes

pub trait FromRes<Encoding, Response, E>: Sized {
    // Required method
    fn from_res(res: Response) -> impl Future<Output = Result<Self, E>> + Send;
}
Expand description

Deserializes the data type from an HTTP response.

Implementations use the methods of the ClientRes trait to extract data from a response. They are often quite short, usually consisting of just two steps:

  1. Extracting a String, Bytes, or a Stream from the response body.
  2. Deserializing the data type from that value.

For example, here’s the implementation for Json.

ⓘ
impl<E, T, Response> FromRes<Json, Response, E> for T
where
    Response: ClientRes<E> + Send,
    T: DeserializeOwned + Send,
    E: FromServerFnError,
{
    async fn from_res(
        res: Response,
    ) -> Result<Self, E> {
        // extracts the request body
        let data = res.try_into_string().await?;
        // and tries to deserialize it as JSON
        serde_json::from_str(&data)
            .map_err(|e| ServerFnErrorErr::Deserialization(e.to_string()).into_app_error())
    }
}

Required Methods§

fn from_res(res: Response) -> impl Future<Output = Result<Self, E>> + Send

Attempts to deserialize the outputs from a response.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

§

impl<E, Encoding, Response, T> FromRes<Patch<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

§

impl<E, Encoding, Response, T> FromRes<Post<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

§

impl<E, Encoding, Response, T> FromRes<Put<Encoding>, Response, E> for T
where Response: ClientRes<E> + Send, Encoding: Decodes<T>, E: FromServerFnError,

§

impl<E, Response> FromRes<Streaming, Response, E> for ByteStream<E>
where Response: ClientRes<E> + Send, E: FromServerFnError,

§

impl<E, Response> FromRes<StreamingText, Response, E> for TextStream<E>
where Response: ClientRes<E> + Send, E: FromServerFnError,