Convert Thunk to Task
This commit is contained in:
parent
23c06db9c2
commit
8dad14b5cd
@ -19,38 +19,27 @@ pub struct Responder<R: ClientRequest> {
|
||||
ph: PhantomData<fn(R)>,
|
||||
}
|
||||
|
||||
impl<R: ClientRequest> Responder<R>
|
||||
{
|
||||
pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> {
|
||||
match resp {
|
||||
Ok(res) => self.result(io, res)?,
|
||||
Err(e) => {
|
||||
self.error(io)?;
|
||||
return Err(e);
|
||||
impl<R: ClientRequest> Responder<R> {
|
||||
pub fn into_response(mut self, result: Result<R::Result>) -> Result<RawResponse> {
|
||||
self.bomb.defuse();
|
||||
let res = match result {
|
||||
Ok(result) => {
|
||||
RawResponse {
|
||||
id: Some(self.id),
|
||||
result: serde_json::to_value(result)?,
|
||||
error: serde_json::Value::Null,
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn result(mut self, io: &mut Io, result: R::Result) -> Result<()> {
|
||||
self.bomb.defuse();
|
||||
io.send(RawMsg::Response(RawResponse {
|
||||
id: Some(self.id),
|
||||
result: serde_json::to_value(result)?,
|
||||
error: serde_json::Value::Null,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn error(mut self, io: &mut Io) -> Result<()> {
|
||||
self.bomb.defuse();
|
||||
error(io, self.id, ErrorCode::InternalError, "internal error")
|
||||
Err(_) => {
|
||||
error_response(self.id, ErrorCode::InternalError, "internal error")?
|
||||
}
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn parse_request_as<R: ClientRequest>(raw: RawRequest)
|
||||
-> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
|
||||
-> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
|
||||
{
|
||||
if raw.method != R::METHOD {
|
||||
return Ok(Err(raw));
|
||||
@ -77,13 +66,13 @@ pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()>
|
||||
Err(r) => {
|
||||
*req = Some(r);
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_request<R: ClientRequest>(io: &mut Io, raw: RawRequest)
|
||||
-> Result<Option<(R::Params, Responder<R>)>>
|
||||
-> Result<Option<(R::Params, Responder<R>)>>
|
||||
{
|
||||
let ret = match parse_request_as::<R>(raw)? {
|
||||
Ok(x) => Some(x),
|
||||
@ -120,21 +109,21 @@ pub fn handle_notification<N, F>(not: &mut Option<RawNotification>, f: F) -> Res
|
||||
Err(n) => {
|
||||
*not = Some(n);
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_notification<N>(io: &mut Io, params: N::Params) -> Result<()>
|
||||
pub fn send_notification<N>(params: N::Params) -> RawNotification
|
||||
where
|
||||
N: Notification,
|
||||
N::Params: Serialize
|
||||
{
|
||||
io.send(RawMsg::Notification(RawNotification {
|
||||
RawNotification {
|
||||
method: N::METHOD.to_string(),
|
||||
params: serde_json::to_value(params)?,
|
||||
}));
|
||||
Ok(())
|
||||
params: serde_json::to_value(params)
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -142,20 +131,26 @@ pub fn unknown_method(io: &mut Io, raw: RawRequest) -> Result<()> {
|
||||
error(io, raw.id, ErrorCode::MethodNotFound, "unknown method")
|
||||
}
|
||||
|
||||
fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> {
|
||||
fn error_response(id: u64, code: ErrorCode, message: &'static str) -> Result<RawResponse> {
|
||||
#[derive(Serialize)]
|
||||
struct Error {
|
||||
code: i32,
|
||||
message: &'static str,
|
||||
}
|
||||
io.send(RawMsg::Response(RawResponse {
|
||||
let resp = RawResponse {
|
||||
id: Some(id),
|
||||
result: serde_json::Value::Null,
|
||||
error: serde_json::to_value(Error {
|
||||
code: code as i32,
|
||||
message,
|
||||
})?,
|
||||
}));
|
||||
};
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> {
|
||||
let resp = error_response(id, code, message)?;
|
||||
io.send(RawMsg::Response(resp));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -32,10 +32,10 @@
|
||||
use libanalysis::{WorldState, World};
|
||||
|
||||
use ::{
|
||||
io::{Io, RawMsg, RawRequest},
|
||||
io::{Io, RawMsg, RawRequest, RawResponse, RawNotification},
|
||||
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations,
|
||||
handle_document_symbol, handle_code_action},
|
||||
util::{FilePath, FnBox}
|
||||
util::FilePath,
|
||||
};
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
||||
@ -80,9 +80,9 @@ fn initialize(io: &mut Io) -> Result<()> {
|
||||
match io.recv()? {
|
||||
RawMsg::Request(req) => {
|
||||
if let Some((_params, resp)) = dispatch::expect_request::<req::Initialize>(io, req)? {
|
||||
resp.result(io, req::InitializeResult {
|
||||
capabilities: caps::SERVER_CAPABILITIES
|
||||
})?;
|
||||
let res = req::InitializeResult { capabilities: caps::SERVER_CAPABILITIES };
|
||||
let resp = resp.into_response(Ok(res))?;
|
||||
io.send(RawMsg::Response(resp));
|
||||
match io.recv()? {
|
||||
RawMsg::Notification(n) => {
|
||||
if n.method != "initialized" {
|
||||
@ -106,13 +106,18 @@ fn initialize(io: &mut Io) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
type Thunk = Box<for<'a> FnBox<&'a mut Io, Result<()>>>;
|
||||
|
||||
enum Task {
|
||||
Respond(RawResponse),
|
||||
Notify(RawNotification),
|
||||
Die(::failure::Error),
|
||||
}
|
||||
|
||||
fn initialized(io: &mut Io) -> Result<()> {
|
||||
{
|
||||
let mut world = WorldState::new();
|
||||
let mut pool = ThreadPool::new(4);
|
||||
let (sender, receiver) = bounded::<Thunk>(16);
|
||||
let (sender, receiver) = bounded::<Task>(16);
|
||||
info!("lifecycle: handshake finished, server ready to serve requests");
|
||||
let res = main_loop(io, &mut world, &mut pool, sender, receiver.clone());
|
||||
info!("waiting for background jobs to finish...");
|
||||
@ -140,14 +145,14 @@ fn main_loop(
|
||||
io: &mut Io,
|
||||
world: &mut WorldState,
|
||||
pool: &mut ThreadPool,
|
||||
sender: Sender<Thunk>,
|
||||
receiver: Receiver<Thunk>,
|
||||
sender: Sender<Task>,
|
||||
receiver: Receiver<Task>,
|
||||
) -> Result<()> {
|
||||
info!("server initialized, serving requests");
|
||||
loop {
|
||||
enum Event {
|
||||
Msg(RawMsg),
|
||||
Thunk(Thunk),
|
||||
Task(Task),
|
||||
ReceiverDead,
|
||||
}
|
||||
|
||||
@ -156,7 +161,7 @@ enum Event {
|
||||
Some(msg) => Event::Msg(msg),
|
||||
None => Event::ReceiverDead,
|
||||
},
|
||||
recv(receiver, thunk) => Event::Thunk(thunk.unwrap()),
|
||||
recv(receiver, task) => Event::Task(task.unwrap()),
|
||||
};
|
||||
|
||||
let msg = match event {
|
||||
@ -164,8 +169,15 @@ enum Event {
|
||||
io.cleanup_receiver()?;
|
||||
unreachable!();
|
||||
}
|
||||
Event::Thunk(thunk) => {
|
||||
thunk.call_box(io)?;
|
||||
Event::Task(task) => {
|
||||
match task {
|
||||
Task::Respond(response) =>
|
||||
io.send(RawMsg::Response(response)),
|
||||
Task::Notify(n) =>
|
||||
io.send(RawMsg::Notification(n)),
|
||||
Task::Die(error) =>
|
||||
return Err(error),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Event::Msg(msg) => msg,
|
||||
@ -175,21 +187,22 @@ enum Event {
|
||||
RawMsg::Request(req) => {
|
||||
let mut req = Some(req);
|
||||
handle_request_on_threadpool::<req::SyntaxTree>(
|
||||
&mut req, pool, world, &sender, handle_syntax_tree
|
||||
&mut req, pool, world, &sender, handle_syntax_tree,
|
||||
)?;
|
||||
handle_request_on_threadpool::<req::ExtendSelection>(
|
||||
&mut req, pool, world, &sender, handle_extend_selection
|
||||
&mut req, pool, world, &sender, handle_extend_selection,
|
||||
)?;
|
||||
handle_request_on_threadpool::<req::DocumentSymbolRequest>(
|
||||
&mut req, pool, world, &sender, handle_document_symbol
|
||||
&mut req, pool, world, &sender, handle_document_symbol,
|
||||
)?;
|
||||
handle_request_on_threadpool::<req::CodeActionRequest>(
|
||||
&mut req, pool, world, &sender, handle_code_action
|
||||
&mut req, pool, world, &sender, handle_code_action,
|
||||
)?;
|
||||
|
||||
let mut shutdown = false;
|
||||
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
|
||||
resp.result(io, ())?;
|
||||
let resp = resp.into_response(Ok(()))?;
|
||||
io.send(RawMsg::Response(resp));
|
||||
shutdown = true;
|
||||
Ok(())
|
||||
})?;
|
||||
@ -227,10 +240,12 @@ enum Event {
|
||||
dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
|
||||
let path = params.text_document.file_path()?;
|
||||
world.change_overlay(path, None);
|
||||
dispatch::send_notification::<req::PublishDiagnostics>(io, req::PublishDiagnosticsParams {
|
||||
let not = req::PublishDiagnosticsParams {
|
||||
uri: params.text_document.uri,
|
||||
diagnostics: Vec::new(),
|
||||
})?;
|
||||
};
|
||||
let not = dispatch::send_notification::<req::PublishDiagnostics>(not);
|
||||
io.send(RawMsg::Notification(not));
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
@ -249,7 +264,7 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
|
||||
req: &mut Option<RawRequest>,
|
||||
pool: &ThreadPool,
|
||||
world: &WorldState,
|
||||
sender: &Sender<Thunk>,
|
||||
sender: &Sender<Task>,
|
||||
f: fn(World, R::Params) -> Result<R::Result>,
|
||||
) -> Result<()>
|
||||
{
|
||||
@ -258,7 +273,11 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
|
||||
let sender = sender.clone();
|
||||
pool.execute(move || {
|
||||
let res = f(world, params);
|
||||
sender.send(Box::new(|io: &mut Io| resp.response(io, res)))
|
||||
let task = match resp.into_response(res) {
|
||||
Ok(resp) => Task::Respond(resp),
|
||||
Err(e) => Task::Die(e),
|
||||
};
|
||||
sender.send(task);
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
@ -267,7 +286,7 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
|
||||
fn update_file_notifications_on_threadpool(
|
||||
pool: &ThreadPool,
|
||||
world: World,
|
||||
sender: Sender<Thunk>,
|
||||
sender: Sender<Task>,
|
||||
uri: Url,
|
||||
) {
|
||||
pool.execute(move || {
|
||||
@ -276,9 +295,8 @@ fn update_file_notifications_on_threadpool(
|
||||
error!("failed to compute diagnostics: {:?}", e)
|
||||
}
|
||||
Ok(params) => {
|
||||
sender.send(Box::new(|io: &mut Io| {
|
||||
dispatch::send_notification::<req::PublishDiagnostics>(io, params)
|
||||
}))
|
||||
let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
|
||||
sender.send(Task::Notify(not));
|
||||
}
|
||||
}
|
||||
match publish_decorations(world, uri) {
|
||||
@ -286,9 +304,8 @@ fn update_file_notifications_on_threadpool(
|
||||
error!("failed to compute decortions: {:?}", e)
|
||||
}
|
||||
Ok(params) => {
|
||||
sender.send(Box::new(|io: &mut Io| {
|
||||
dispatch::send_notification::<req::PublishDecorations>(io, params)
|
||||
}))
|
||||
let not = dispatch::send_notification::<req::PublishDecorations>(params);
|
||||
sender.send(Task::Notify(not))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -3,16 +3,6 @@
|
||||
TextDocumentIdentifier, Url};
|
||||
use ::{Result};
|
||||
|
||||
pub trait FnBox<A, R>: Send {
|
||||
fn call_box(self: Box<Self>, a: A) -> R;
|
||||
}
|
||||
|
||||
impl<A, R, F: FnOnce(A) -> R + Send> FnBox<A, R> for F {
|
||||
fn call_box(self: Box<F>, a: A) -> R {
|
||||
(*self)(a)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FilePath {
|
||||
fn file_path(&self) -> Result<PathBuf>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user