1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub mod trades;
pub mod updated_orderbook;
use super::graphql::ResponseOrError;
use crate::errors::Result;
use super::{
NashProtocolSubscription, State,
};
use async_trait::async_trait;
use futures::lock::Mutex;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub enum SubscriptionRequest {
Trades(trades::SubscribeTrades),
Orderbook(updated_orderbook::SubscribeOrderbook)
}
#[derive(Debug)]
pub enum SubscriptionResponse {
Orderbook(updated_orderbook::SubscribeOrderbookResponse),
Trades(trades::TradesResponse),
}
#[async_trait]
impl NashProtocolSubscription for SubscriptionRequest {
type SubscriptionResponse = SubscriptionResponse;
async fn graphql(&self, state: Arc<Mutex<State>>) -> Result<serde_json::Value> {
match self {
Self::Trades(trades_req) => trades_req.graphql(state).await,
Self::Orderbook(orderbook_req) => orderbook_req.graphql(state).await
}
}
fn subscription_response_from_json(
&self,
response: serde_json::Value,
) -> Result<ResponseOrError<Self::SubscriptionResponse>> {
match self {
Self::Trades(trades_req) => Ok(trades_req
.subscription_response_from_json(response)?
.map(Box::new(|res| SubscriptionResponse::Trades(res)))),
Self::Orderbook(orderbook_req) => Ok(orderbook_req
.subscription_response_from_json(response)?
.map(Box::new(|res| SubscriptionResponse::Orderbook(res))))
}
}
fn wrap_response_as_any_subscription(
&self,
response: serde_json::Value,
) -> Result<ResponseOrError<SubscriptionResponse>> {
let response = self.subscription_response_from_json(response)?;
Ok(response)
}
}