summaryrefslogtreecommitdiff
path: root/support/texlab/crates/jsonrpc/src/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/crates/jsonrpc/src/server.rs')
-rw-r--r--support/texlab/crates/jsonrpc/src/server.rs68
1 files changed, 31 insertions, 37 deletions
diff --git a/support/texlab/crates/jsonrpc/src/server.rs b/support/texlab/crates/jsonrpc/src/server.rs
index ccc04f286e..ce34dc8b60 100644
--- a/support/texlab/crates/jsonrpc/src/server.rs
+++ b/support/texlab/crates/jsonrpc/src/server.rs
@@ -1,24 +1,22 @@
-use crate::types::*;
+use super::types::*;
+use async_trait::async_trait;
use futures::prelude::*;
-use futures_boxed::boxed;
-use serde::de::DeserializeOwned;
-use serde::Serialize;
+use serde::{de::DeserializeOwned, Serialize};
use serde_json::json;
pub type Result<T> = std::result::Result<T, String>;
+#[async_trait]
pub trait RequestHandler {
- #[boxed]
async fn handle_request(&self, request: Request) -> Response;
- fn handle_notification(&self, notification: Notification);
+ async fn handle_notification(&self, notification: Notification);
}
+#[async_trait]
pub trait Middleware {
- #[boxed]
async fn before_message(&self);
- #[boxed]
async fn after_message(&self);
}
@@ -29,12 +27,10 @@ where
I: DeserializeOwned + Send,
O: Serialize,
{
- let handle = |json| {
- async move {
- let params: I = serde_json::from_value(json).map_err(|_| Error::deserialize_error())?;
- let result = handler(params).await.map_err(Error::internal_error)?;
- Ok(result)
- }
+ let handle = |json| async move {
+ let params: I = serde_json::from_value(json).map_err(|_| Error::deserialize_error())?;
+ let result = handler(params).await.map_err(Error::internal_error)?;
+ Ok(result)
};
match handle(request.params).await {
@@ -43,20 +39,20 @@ where
}
}
-pub fn handle_notification<'a, H, I>(notification: Notification, handler: H)
+pub async fn handle_notification<'a, H, F, I>(notification: Notification, handler: H)
where
- H: Fn(I) -> () + Send + Sync + 'a,
+ H: Fn(I) -> F + Send + Sync + 'a,
+ F: Future<Output = ()> + Send,
I: DeserializeOwned + Send,
{
- let params =
- serde_json::from_value(notification.params).expect(&Error::deserialize_error().message);
- handler(params);
+ let error = Error::deserialize_error().message;
+ let params = serde_json::from_value(notification.params).expect(&error);
+ handler(params).await;
}
#[cfg(test)]
mod tests {
use super::*;
- use futures::executor::block_on;
const METHOD_NAME: &str = "foo";
@@ -64,7 +60,7 @@ mod tests {
Ok(i + 1)
}
- fn panic(_params: ()) {
+ async fn panic(_params: ()) {
panic!("success");
}
@@ -85,15 +81,15 @@ mod tests {
}
}
- #[test]
- fn test_request_valid() {
+ #[tokio::test]
+ async fn request_valid() {
let value = 42;
let request = setup_request(value);
- let response = block_on(handle_request(request.clone(), increment));
+ let response = handle_request(request.clone(), increment).await;
let expected = Response {
jsonrpc: request.jsonrpc,
- result: Some(json!(block_on(increment(value)).unwrap())),
+ result: Some(json!(increment(value).await.unwrap())),
error: None,
id: Some(request.id),
};
@@ -101,11 +97,11 @@ mod tests {
assert_eq!(response, expected);
}
- #[test]
- fn test_request_invalid_params() {
+ #[tokio::test]
+ async fn request_invalid_params() {
let request = setup_request((0, 0));
- let response = block_on(handle_request(request.clone(), increment));
+ let response = handle_request(request.clone(), increment).await;
let expected = Response {
jsonrpc: request.jsonrpc.clone(),
result: None,
@@ -116,22 +112,20 @@ mod tests {
assert_eq!(response, expected);
}
- #[test]
+ #[tokio::test]
#[should_panic(expected = "success")]
- fn test_notification_valid() {
+ async fn notification_valid() {
let notification = setup_notification();
- handle_notification(notification, panic);
+ handle_notification(notification, panic).await;
}
- #[test]
+ #[tokio::test]
#[should_panic]
- fn test_notification_invalid_params() {
- let notification = setup_notification();
+ async fn notification_invalid_params() {
let notification = Notification {
params: json!(0),
- ..notification
+ ..setup_notification()
};
-
- handle_notification(notification, panic);
+ handle_notification(notification, panic).await;
}
}