Trait server_fn::codec::FromRes

source ·
pub trait FromRes<Encoding, Response, CustErr>
where Self: Sized,
{ // Required method fn from_res( res: Response ) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + 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<CustErr, T, Response> FromRes<Json, Response, CustErr> for T
where
    Response: ClientRes<CustErr> + Send,
    T: DeserializeOwned + Send,
{
    async fn from_res(
        res: Response,
    ) -> Result<Self, ServerFnError<CustErr>> {
        // 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| ServerFnError::Deserialization(e.to_string()))
    }
}

Required Methods§

source

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

Attempts to deserialize the outputs from a response.

Object Safety§

This trait is not object safe.

Implementors§

source§

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

source§

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

source§

impl<CustErr, T, Response> FromRes<Cbor, Response, CustErr> for T
where Response: ClientRes<CustErr> + Send, T: DeserializeOwned + Send,

source§

impl<CustErr, T, Response> FromRes<Json, Response, CustErr> for T
where Response: ClientRes<CustErr> + Send, T: DeserializeOwned + Send,

source§

impl<CustErr, T, Response> FromRes<Rkyv, Response, CustErr> for T
where Response: ClientRes<CustErr> + Send, T: Serialize<AllocSerializer<1024>> + Send + Archive, T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, SharedDeserializeMap>,

source§

impl<CustErr, T, Response> FromRes<SerdeLite, Response, CustErr> for T
where Response: ClientRes<CustErr> + Send, T: Deserialize + Send,

source§

impl<T, Response, Err> FromRes<MsgPack, Response, Err> for T
where Response: ClientRes<Err> + Send, T: DeserializeOwned,