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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use super::subscriptions::SubscriptionResponse;
use super::{ProtocolHook, ResponseOrError, State};
use crate::errors::ProtocolError;
use crate::errors::Result;
use async_trait::async_trait;
use futures::lock::Mutex;
use std::fmt::Debug;
use std::sync::Arc;
#[async_trait]
pub trait NashProtocol: Sync {
type Response: Send + Sync;
async fn graphql(&self, state: Arc<Mutex<State>>) -> Result<serde_json::Value>;
fn response_from_json(
&self,
response: serde_json::Value,
) -> Result<ResponseOrError<Self::Response>>;
async fn process_response(
&self,
_response: &Self::Response,
_state: Arc<Mutex<State>>,
) -> Result<()> {
Ok(())
}
async fn run_before(&self, _state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
Ok(None)
}
async fn run_after(&self, _state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
Ok(None)
}
}
#[async_trait]
pub trait NashProtocolPipeline: Debug {
type PipelineState;
type ActionType: NashProtocol;
async fn init_state(&self, state: Arc<Mutex<State>>) -> Self::PipelineState;
async fn next_step(
&self,
pipeline_state: &Self::PipelineState,
client_state: Arc<Mutex<State>>,
) -> Result<Option<Self::ActionType>>;
async fn process_step(
&self,
result: <<Self as NashProtocolPipeline>::ActionType as NashProtocol>::Response,
pipeline_state: &mut Self::PipelineState,
);
fn output(
&self,
pipeline_state: Self::PipelineState,
) -> Result<ResponseOrError<<Self::ActionType as NashProtocol>::Response>>;
async fn run_before(&self, _state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
Ok(None)
}
async fn run_after(&self, _state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
Ok(None)
}
}
#[async_trait]
impl<T> NashProtocolPipeline for T
where
T: NashProtocol + Clone + Debug + Sync + Send,
{
type PipelineState = Option<ResponseOrError<T::Response>>;
type ActionType = T;
async fn init_state(&self, _state: Arc<Mutex<State>>) -> Self::PipelineState {
None
}
async fn next_step(
&self,
pipeline_state: &Self::PipelineState,
_client_state: Arc<Mutex<State>>,
) -> Result<Option<Self::ActionType>> {
if let Some(_) = pipeline_state {
Ok(None)
}
else {
Ok(Some(self.clone()))
}
}
async fn process_step(
&self,
result: <<Self as NashProtocolPipeline>::ActionType as NashProtocol>::Response,
state: &mut Self::PipelineState,
) {
*state = Some(ResponseOrError::from_data(result));
}
fn output(&self, state: Self::PipelineState) -> Result<ResponseOrError<T::Response>> {
if let Some(state) = state {
Ok(state)
} else {
Err(ProtocolError(
"Protocol request not run, cannot retrieve output",
))
}
}
async fn run_before(&self, state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
self.run_before(state).await
}
async fn run_after(&self, state: Arc<Mutex<State>>) -> Result<Option<Vec<ProtocolHook>>> {
self.run_after(state).await
}
}
#[async_trait]
pub trait NashProtocolSubscription: Clone {
type SubscriptionResponse: Send + Sync;
async fn graphql(&self, state: Arc<Mutex<State>>) -> Result<serde_json::Value>;
fn subscription_response_from_json(
&self,
response: serde_json::Value,
) -> Result<ResponseOrError<Self::SubscriptionResponse>>;
async fn process_subscription_response(
&self,
_response: &Self::SubscriptionResponse,
_state: Arc<Mutex<State>>,
) -> Result<()> {
Ok(())
}
fn wrap_response_as_any_subscription(
&self,
response: serde_json::Value,
) -> Result<ResponseOrError<SubscriptionResponse>>;
}