diff options
Diffstat (limited to 'codeless/server/src/dispatch.rs')
-rw-r--r-- | codeless/server/src/dispatch.rs | 174 |
1 files changed, 0 insertions, 174 deletions
diff --git a/codeless/server/src/dispatch.rs b/codeless/server/src/dispatch.rs deleted file mode 100644 index 2da0996e3..000000000 --- a/codeless/server/src/dispatch.rs +++ /dev/null | |||
@@ -1,174 +0,0 @@ | |||
1 | use std::marker::PhantomData; | ||
2 | |||
3 | use serde::{ | ||
4 | ser::Serialize, | ||
5 | de::DeserializeOwned, | ||
6 | }; | ||
7 | use serde_json; | ||
8 | use drop_bomb::DropBomb; | ||
9 | |||
10 | use ::{ | ||
11 | Result, | ||
12 | req::{Request, Notification}, | ||
13 | io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}, | ||
14 | }; | ||
15 | |||
16 | pub struct Responder<R: Request> { | ||
17 | id: u64, | ||
18 | bomb: DropBomb, | ||
19 | ph: PhantomData<R>, | ||
20 | } | ||
21 | |||
22 | impl<R: Request> Responder<R> | ||
23 | where | ||
24 | R::Params: DeserializeOwned, | ||
25 | R::Result: Serialize, | ||
26 | { | ||
27 | pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> { | ||
28 | match resp { | ||
29 | Ok(res) => self.result(io, res)?, | ||
30 | Err(e) => { | ||
31 | self.error(io)?; | ||
32 | return Err(e); | ||
33 | } | ||
34 | } | ||
35 | Ok(()) | ||
36 | } | ||
37 | |||
38 | pub fn result(mut self, io: &mut Io, result: R::Result) -> Result<()> { | ||
39 | self.bomb.defuse(); | ||
40 | io.send(RawMsg::Response(RawResponse { | ||
41 | id: Some(self.id), | ||
42 | result: serde_json::to_value(result)?, | ||
43 | error: serde_json::Value::Null, | ||
44 | })); | ||
45 | Ok(()) | ||
46 | } | ||
47 | |||
48 | pub fn error(mut self, io: &mut Io) -> Result<()> { | ||
49 | self.bomb.defuse(); | ||
50 | error(io, self.id, ErrorCode::InternalError, "internal error") | ||
51 | } | ||
52 | } | ||
53 | |||
54 | |||
55 | fn parse_request_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>> | ||
56 | where | ||
57 | R: Request, | ||
58 | R::Params: DeserializeOwned, | ||
59 | R::Result: Serialize, | ||
60 | { | ||
61 | if raw.method != R::METHOD { | ||
62 | return Ok(Err(raw)); | ||
63 | } | ||
64 | |||
65 | let params: R::Params = serde_json::from_value(raw.params)?; | ||
66 | let responder = Responder { | ||
67 | id: raw.id, | ||
68 | bomb: DropBomb::new("dropped request"), | ||
69 | ph: PhantomData, | ||
70 | }; | ||
71 | Ok(Ok((params, responder))) | ||
72 | } | ||
73 | |||
74 | pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()> | ||
75 | where | ||
76 | R: Request, | ||
77 | R::Params: DeserializeOwned, | ||
78 | R::Result: Serialize, | ||
79 | F: FnOnce(R::Params, Responder<R>) -> Result<()> | ||
80 | { | ||
81 | match req.take() { | ||
82 | None => Ok(()), | ||
83 | Some(r) => match parse_request_as::<R>(r)? { | ||
84 | Ok((params, responder)) => f(params, responder), | ||
85 | Err(r) => { | ||
86 | *req = Some(r); | ||
87 | Ok(()) | ||
88 | }, | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | pub fn expect_request<R>(io: &mut Io, raw: RawRequest) -> Result<Option<(R::Params, Responder<R>)>> | ||
94 | where | ||
95 | R: Request, | ||
96 | R::Params: DeserializeOwned, | ||
97 | R::Result: Serialize, | ||
98 | { | ||
99 | let ret = match parse_request_as::<R>(raw)? { | ||
100 | Ok(x) => Some(x), | ||
101 | Err(raw) => { | ||
102 | unknown_method(io, raw)?; | ||
103 | None | ||
104 | } | ||
105 | }; | ||
106 | Ok(ret) | ||
107 | } | ||
108 | |||
109 | fn parse_notification_as<N>(raw: RawNotification) -> Result<::std::result::Result<N::Params, RawNotification>> | ||
110 | where | ||
111 | N: Notification, | ||
112 | N::Params: DeserializeOwned, | ||
113 | { | ||
114 | if raw.method != N::METHOD { | ||
115 | return Ok(Err(raw)); | ||
116 | } | ||
117 | let params: N::Params = serde_json::from_value(raw.params)?; | ||
118 | Ok(Ok(params)) | ||
119 | } | ||
120 | |||
121 | pub fn handle_notification<N, F>(not: &mut Option<RawNotification>, f: F) -> Result<()> | ||
122 | where | ||
123 | N: Notification, | ||
124 | N::Params: DeserializeOwned, | ||
125 | F: FnOnce(N::Params) -> Result<()> | ||
126 | { | ||
127 | match not.take() { | ||
128 | None => Ok(()), | ||
129 | Some(n) => match parse_notification_as::<N>(n)? { | ||
130 | Ok(params) => f(params), | ||
131 | Err(n) => { | ||
132 | *not = Some(n); | ||
133 | Ok(()) | ||
134 | }, | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | |||
140 | pub fn unknown_method(io: &mut Io, raw: RawRequest) -> Result<()> { | ||
141 | error(io, raw.id, ErrorCode::MethodNotFound, "unknown method") | ||
142 | } | ||
143 | |||
144 | fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> { | ||
145 | #[derive(Serialize)] | ||
146 | struct Error { | ||
147 | code: i32, | ||
148 | message: &'static str, | ||
149 | } | ||
150 | io.send(RawMsg::Response(RawResponse { | ||
151 | id: Some(id), | ||
152 | result: serde_json::Value::Null, | ||
153 | error: serde_json::to_value(Error { | ||
154 | code: code as i32, | ||
155 | message, | ||
156 | })?, | ||
157 | })); | ||
158 | Ok(()) | ||
159 | } | ||
160 | |||
161 | |||
162 | #[allow(unused)] | ||
163 | enum ErrorCode { | ||
164 | ParseError = -32700, | ||
165 | InvalidRequest = -32600, | ||
166 | MethodNotFound = -32601, | ||
167 | InvalidParams = -32602, | ||
168 | InternalError = -32603, | ||
169 | ServerErrorStart = -32099, | ||
170 | ServerErrorEnd = -32000, | ||
171 | ServerNotInitialized = -32002, | ||
172 | UnknownErrorCode = -32001, | ||
173 | RequestCancelled = -32800, | ||
174 | } | ||