Refactor a bit

This commit is contained in:
Edwin Cheng 2020-03-31 22:01:34 +08:00
parent f461dc48d1
commit 02b849a2a0

View file

@ -29,7 +29,7 @@ pub(crate) struct ProcMacroProcessThread {
struct Task { struct Task {
req: Request, req: Request,
result_tx: Sender<Response>, result_tx: Sender<Option<Response>>,
} }
struct Process { struct Process {
@ -131,18 +131,21 @@ impl ProcMacroProcessSrv {
Some(it) => it, Some(it) => it,
}; };
sender.send(Task { req: req.into(), result_tx }).unwrap(); sender.send(Task { req: req.into(), result_tx }).unwrap();
let res = result_rx
.recv()
.map_err(|_| ra_tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?;
let res = result_rx.recv().unwrap();
match res { match res {
Response::Error(err) => { Some(Response::Error(err)) => {
return Err(ra_tt::ExpansionError::ExpansionError(err.message)); return Err(ra_tt::ExpansionError::ExpansionError(err.message));
} }
_ => Ok(res.try_into().map_err(|err| { Some(res) => Ok(res.try_into().map_err(|err| {
ra_tt::ExpansionError::Unknown(format!( ra_tt::ExpansionError::Unknown(format!(
"Fail to get response, reason : {:#?} ", "Fail to get response, reason : {:#?} ",
err err
)) ))
})?), })?),
None => Err(ra_tt::ExpansionError::Unknown("Empty result".into())),
} }
} }
} }
@ -156,8 +159,8 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
for task in task_rx { for task in task_rx {
let Task { req, result_tx } = task; let Task { req, result_tx } = task;
let res = match send_request(&mut stdin, &mut stdout, req) { match send_request(&mut stdin, &mut stdout, req) {
Ok(res) => res, Ok(res) => result_tx.send(res).unwrap(),
Err(_err) => { Err(_err) => {
let res = Response::Error(ResponseError { let res = Response::Error(ResponseError {
code: ErrorCode::ServerErrorEnd, code: ErrorCode::ServerErrorEnd,
@ -174,12 +177,7 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
}; };
stdin = stdio.0; stdin = stdio.0;
stdout = stdio.1; stdout = stdio.1;
continue;
} }
};
if let Some(res) = res {
result_tx.send(res).unwrap();
} }
} }
} }