exc_binance/types/
response.rs

1use futures::{Stream, TryStreamExt};
2
3use crate::{
4    http::{
5        error::RestError,
6        response::{Data, RestResponse},
7    },
8    websocket::{error::WsError, protocol::frame::StreamFrame, response::WsResponse},
9    Error,
10};
11
12/// Binance response.
13#[derive(Debug)]
14pub enum Response {
15    /// Http resposne.
16    Http(Box<RestResponse<Data>>),
17    /// Websocket response.
18    Ws(WsResponse),
19}
20
21impl Response {
22    /// Convert into a stream of the given type.
23    pub fn into_stream<T>(self) -> Result<impl Stream<Item = Result<T, Error>>, Error>
24    where
25        T: TryFrom<StreamFrame, Error = WsError>,
26    {
27        match self {
28            Self::Http(_) => Err(Error::WrongResponseType),
29            Self::Ws(resp) => resp
30                .into_stream()
31                .map(|stream| stream.map_err(Error::from))
32                .ok_or(Error::WrongResponseType),
33        }
34    }
35
36    /// Convert to a response of the given type.
37    pub fn into_response<T>(self) -> Result<T, Error>
38    where
39        T: TryFrom<Data, Error = RestError>,
40    {
41        match self {
42            Self::Http(resp) => resp.into_response().map_err(Error::from),
43            Self::Ws(_) => Err(Error::WrongResponseType),
44        }
45    }
46}