add roundtrip abstraction to remove subrequest subresponse boilerplate code
This commit is contained in:
parent
abbf0272fa
commit
c4dd25f6cd
1 changed files with 24 additions and 55 deletions
|
|
@ -166,79 +166,48 @@ struct ProcMacroClientHandle<'a, C: Codec> {
|
|||
buf: &'a mut C::Buf,
|
||||
}
|
||||
|
||||
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
|
||||
fn file(&mut self, file_id: u32) -> String {
|
||||
let req =
|
||||
bidirectional::BidirectionalMessage::SubRequest(bidirectional::SubRequest::FileName {
|
||||
file_id,
|
||||
});
|
||||
impl<'a, C: Codec> ProcMacroClientHandle<'a, C> {
|
||||
fn roundtrip(
|
||||
&mut self,
|
||||
req: bidirectional::SubRequest,
|
||||
) -> Option<bidirectional::BidirectionalMessage> {
|
||||
let msg = bidirectional::BidirectionalMessage::SubRequest(req);
|
||||
|
||||
if req.write::<_, C>(&mut self.stdout.lock()).is_err() {
|
||||
return String::new();
|
||||
if msg.write::<_, C>(&mut self.stdout.lock()).is_err() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let msg = match bidirectional::BidirectionalMessage::read::<_, C>(
|
||||
&mut self.stdin.lock(),
|
||||
self.buf,
|
||||
) {
|
||||
Ok(Some(msg)) => msg,
|
||||
_ => return String::new(),
|
||||
};
|
||||
match bidirectional::BidirectionalMessage::read::<_, C>(&mut self.stdin.lock(), self.buf) {
|
||||
Ok(Some(msg)) => Some(msg),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match msg {
|
||||
bidirectional::BidirectionalMessage::SubResponse(
|
||||
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
|
||||
fn file(&mut self, file_id: u32) -> String {
|
||||
match self.roundtrip(bidirectional::SubRequest::FileName { file_id }) {
|
||||
Some(bidirectional::BidirectionalMessage::SubResponse(
|
||||
bidirectional::SubResponse::FileNameResult { name },
|
||||
) => name,
|
||||
)) => name,
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
|
||||
let req = bidirectional::BidirectionalMessage::SubRequest(
|
||||
bidirectional::SubRequest::SourceText { file_id, start, end },
|
||||
);
|
||||
|
||||
if req.write::<_, C>(&mut self.stdout.lock()).is_err() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let msg = match bidirectional::BidirectionalMessage::read::<_, C>(
|
||||
&mut self.stdin.lock(),
|
||||
self.buf,
|
||||
) {
|
||||
Ok(Some(msg)) => msg,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
match msg {
|
||||
bidirectional::BidirectionalMessage::SubResponse(
|
||||
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, start, end }) {
|
||||
Some(bidirectional::BidirectionalMessage::SubResponse(
|
||||
bidirectional::SubResponse::SourceTextResult { text },
|
||||
) => text,
|
||||
)) => text,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn local_file(&mut self, file_id: u32) -> Option<String> {
|
||||
let req = bidirectional::BidirectionalMessage::SubRequest(
|
||||
bidirectional::SubRequest::LocalFileName { file_id },
|
||||
);
|
||||
|
||||
if req.write::<_, C>(&mut self.stdout.lock()).is_err() {
|
||||
return Some(String::new());
|
||||
}
|
||||
|
||||
let msg = match bidirectional::BidirectionalMessage::read::<_, C>(
|
||||
&mut self.stdin.lock(),
|
||||
self.buf,
|
||||
) {
|
||||
Ok(msg) => msg,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
match msg {
|
||||
match self.roundtrip(bidirectional::SubRequest::LocalFileName { file_id }) {
|
||||
Some(bidirectional::BidirectionalMessage::SubResponse(
|
||||
bidirectional::SubResponse::LocalFileNameResult { name },
|
||||
)) => Some(name.unwrap_or_default()),
|
||||
)) => name,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue