Skip to content

Commit

Permalink
Rename jsonrpc::Server to jsonrpc::RequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
efoerster committed May 30, 2019
1 parent b1c6a4c commit a1d954e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion jsonrpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where

let result = match response.error {
Some(why) => Err(why),
None => Ok(response.result.unwrap_or(serde_json::Value::Null))
None => Ok(response.result.unwrap_or(serde_json::Value::Null)),
};
sender.send(result).unwrap();
};
Expand Down
44 changes: 15 additions & 29 deletions jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod types;

pub use self::{
client::{Client, ResponseHandler},
server::{handle_notification, handle_request, EventHandler, Server},
server::{handle_notification, handle_request, EventHandler, RequestHandler},
types::*,
};

Expand All @@ -16,37 +16,21 @@ use futures::prelude::*;
use futures::task::*;
use std::sync::Arc;

pub struct MessageHandler<S, H, I, O> {
server: Arc<S>,
response_handler: Arc<H>,
input: I,
output: Arc<Mutex<O>>,
pool: ThreadPool,
pub struct MessageHandler<S, C, I, O> {
pub server: Arc<S>,
pub client: Arc<C>,
pub input: I,
pub output: Arc<Mutex<O>>,
pub pool: ThreadPool,
}

impl<S, H, I, O> MessageHandler<S, H, I, O>
impl<S, C, I, O> MessageHandler<S, C, I, O>
where
S: Server + EventHandler + Send + Sync + 'static,
H: ResponseHandler + Send + Sync + 'static,
S: RequestHandler + EventHandler + Send + Sync + 'static,
C: ResponseHandler + Send + Sync + 'static,
I: Stream<Item = std::io::Result<String>> + Unpin,
O: Sink<String> + Unpin + Send + 'static,
{
pub fn new(
server: S,
response_handler: Arc<H>,
input: I,
output: Arc<Mutex<O>>,
pool: ThreadPool,
) -> Self {
MessageHandler {
server: Arc::new(server),
response_handler,
input,
output,
pool,
}
}

pub async fn listen(&mut self) {
while let Some(json) = await!(self.input.next()) {
let message = serde_json::from_str(&json.expect("")).map_err(|_| Error {
Expand All @@ -73,12 +57,14 @@ where
self.server.handle_notification(notification);

let server = Arc::clone(&self.server);
self.pool.spawn(async move {
let handler = async move {
await!(server.handle_events());
});
};

self.pool.spawn(handler).unwrap();
}
Ok(Message::Response(response)) => {
await!(self.response_handler.handle(response));
await!(self.client.handle(response));
}
Err(why) => {
let response = Response::error(why, None);
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::json;

pub type Result<T> = std::result::Result<T, String>;

pub trait Server {
pub trait RequestHandler {
fn handle_request(&self, request: Request) -> BoxFuture<'_, Response>;

fn handle_notification(&self, notification: Notification);
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn jsonrpc_server(
let tokens = quote! {
#impl_

impl #generics jsonrpc::Server for #self_ty {
impl #generics jsonrpc::RequestHandler for #self_ty {
fn handle_request(&self, request: jsonrpc::Request)
-> futures::future::BoxFuture<'_, jsonrpc::Response> {
use futures::prelude::*;
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ async fn run(pool: ThreadPool) {
let input = FramedRead::new(stdin, LspCodec).compat();
let output = Arc::new(Mutex::new(FramedWrite::new(stdout, LspCodec).sink_compat()));
let client = Arc::new(LatexLspClient::new(Arc::clone(&output)));
let server = LatexLspServer::new(Arc::clone(&client));
let mut handler = MessageHandler::new(server, client, input, output, pool);
let server = Arc::new(LatexLspServer::new(Arc::clone(&client)));
let mut handler = MessageHandler {
server,
client,
input,
output,
pool,
};

await!(handler.listen());
}
6 changes: 4 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {

#[jsonrpc_method("textDocument/didSave", kind = "notification")]
pub fn did_save(&self, params: DidSaveTextDocumentParams) {
self.event_manager.push(Event::Saved(params.text_document.uri));
self.event_manager
.push(Event::Saved(params.text_document.uri));
self.event_manager.push(Event::WorkspaceChanged);
}

Expand Down Expand Up @@ -321,7 +322,8 @@ impl<C: LspClient + Send + Sync> jsonrpc::EventHandler for LatexLspServer<C> {
method: Cow::from("workspace/didChangeWatchedFiles"),
register_options: Some(serde_json::to_value(options).unwrap()),
}]
})).unwrap();
}))
.unwrap();

match TexResolver::load() {
Ok(res) => {
Expand Down

0 comments on commit a1d954e

Please sign in to comment.