aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/dispatch.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/server/src/dispatch.rs')
-rw-r--r--crates/server/src/dispatch.rs71
1 files changed, 33 insertions, 38 deletions
diff --git a/crates/server/src/dispatch.rs b/crates/server/src/dispatch.rs
index eb23ab64f..369c56b64 100644
--- a/crates/server/src/dispatch.rs
+++ b/crates/server/src/dispatch.rs
@@ -19,38 +19,27 @@ pub struct Responder<R: ClientRequest> {
19 ph: PhantomData<fn(R)>, 19 ph: PhantomData<fn(R)>,
20} 20}
21 21
22impl<R: ClientRequest> Responder<R> 22impl<R: ClientRequest> Responder<R> {
23{ 23 pub fn into_response(mut self, result: Result<R::Result>) -> Result<RawResponse> {
24 pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> {
25 match resp {
26 Ok(res) => self.result(io, res)?,
27 Err(e) => {
28 self.error(io)?;
29 return Err(e);
30 }
31 }
32 Ok(())
33 }
34
35 pub fn result(mut self, io: &mut Io, result: R::Result) -> Result<()> {
36 self.bomb.defuse(); 24 self.bomb.defuse();
37 io.send(RawMsg::Response(RawResponse { 25 let res = match result {
38 id: Some(self.id), 26 Ok(result) => {
39 result: serde_json::to_value(result)?, 27 RawResponse {
40 error: serde_json::Value::Null, 28 id: Some(self.id),
41 })); 29 result: serde_json::to_value(result)?,
42 Ok(()) 30 error: serde_json::Value::Null,
43 } 31 }
44 32 }
45 pub fn error(mut self, io: &mut Io) -> Result<()> { 33 Err(_) => {
46 self.bomb.defuse(); 34 error_response(self.id, ErrorCode::InternalError, "internal error")?
47 error(io, self.id, ErrorCode::InternalError, "internal error") 35 }
36 };
37 Ok(res)
48 } 38 }
49} 39}
50 40
51
52fn parse_request_as<R: ClientRequest>(raw: RawRequest) 41fn parse_request_as<R: ClientRequest>(raw: RawRequest)
53 -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>> 42 -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
54{ 43{
55 if raw.method != R::METHOD { 44 if raw.method != R::METHOD {
56 return Ok(Err(raw)); 45 return Ok(Err(raw));
@@ -77,13 +66,13 @@ pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()>
77 Err(r) => { 66 Err(r) => {
78 *req = Some(r); 67 *req = Some(r);
79 Ok(()) 68 Ok(())
80 }, 69 }
81 } 70 }
82 } 71 }
83} 72}
84 73
85pub fn expect_request<R: ClientRequest>(io: &mut Io, raw: RawRequest) 74pub fn expect_request<R: ClientRequest>(io: &mut Io, raw: RawRequest)
86 -> Result<Option<(R::Params, Responder<R>)>> 75 -> Result<Option<(R::Params, Responder<R>)>>
87{ 76{
88 let ret = match parse_request_as::<R>(raw)? { 77 let ret = match parse_request_as::<R>(raw)? {
89 Ok(x) => Some(x), 78 Ok(x) => Some(x),
@@ -120,21 +109,21 @@ pub fn handle_notification<N, F>(not: &mut Option<RawNotification>, f: F) -> Res
120 Err(n) => { 109 Err(n) => {
121 *not = Some(n); 110 *not = Some(n);
122 Ok(()) 111 Ok(())
123 }, 112 }
124 } 113 }
125 } 114 }
126} 115}
127 116
128pub fn send_notification<N>(io: &mut Io, params: N::Params) -> Result<()> 117pub fn send_notification<N>(params: N::Params) -> RawNotification
129 where 118 where
130 N: Notification, 119 N: Notification,
131 N::Params: Serialize 120 N::Params: Serialize
132{ 121{
133 io.send(RawMsg::Notification(RawNotification { 122 RawNotification {
134 method: N::METHOD.to_string(), 123 method: N::METHOD.to_string(),
135 params: serde_json::to_value(params)?, 124 params: serde_json::to_value(params)
136 })); 125 .unwrap(),
137 Ok(()) 126 }
138} 127}
139 128
140 129
@@ -142,20 +131,26 @@ pub fn unknown_method(io: &mut Io, raw: RawRequest) -> Result<()> {
142 error(io, raw.id, ErrorCode::MethodNotFound, "unknown method") 131 error(io, raw.id, ErrorCode::MethodNotFound, "unknown method")
143} 132}
144 133
145fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> { 134fn error_response(id: u64, code: ErrorCode, message: &'static str) -> Result<RawResponse> {
146 #[derive(Serialize)] 135 #[derive(Serialize)]
147 struct Error { 136 struct Error {
148 code: i32, 137 code: i32,
149 message: &'static str, 138 message: &'static str,
150 } 139 }
151 io.send(RawMsg::Response(RawResponse { 140 let resp = RawResponse {
152 id: Some(id), 141 id: Some(id),
153 result: serde_json::Value::Null, 142 result: serde_json::Value::Null,
154 error: serde_json::to_value(Error { 143 error: serde_json::to_value(Error {
155 code: code as i32, 144 code: code as i32,
156 message, 145 message,
157 })?, 146 })?,
158 })); 147 };
148 Ok(resp)
149}
150
151fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> {
152 let resp = error_response(id, code, message)?;
153 io.send(RawMsg::Response(resp));
159 Ok(()) 154 Ok(())
160} 155}
161 156