Skip to content

Commit ac582bd

Browse files
committed
Go back to anyhow
1 parent f37f8ac commit ac582bd

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

rust/acp.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
mod acp_tests;
33
mod schema;
44

5-
use anyhow::Result;
5+
use anyhow::{Result, anyhow};
66
use futures::{
77
AsyncBufReadExt as _, AsyncRead, AsyncWrite, AsyncWriteExt as _, FutureExt as _,
88
StreamExt as _,
@@ -20,6 +20,7 @@ use serde::{Deserialize, Serialize};
2020
use serde_json::value::RawValue;
2121
use std::{
2222
collections::HashMap,
23+
fmt::Display,
2324
sync::{
2425
Arc,
2526
atomic::{AtomicI32, Ordering::SeqCst},
@@ -60,14 +61,16 @@ impl AgentConnection {
6061
pub fn request<R: AgentRequest + 'static>(
6162
&self,
6263
params: R,
63-
) -> impl Future<Output = Result<R::Response, crate::Error>> {
64+
) -> impl Future<Output = Result<R::Response>> {
6465
let params = params.into_any();
6566
let result = self.0.request(params.method_name(), params);
6667
async move {
6768
let result = result.await?;
68-
R::response_from_any(result).ok_or_else(|| crate::Error {
69-
code: -32700,
70-
message: "Unexpected Response".to_string(),
69+
R::response_from_any(result).ok_or_else(|| {
70+
anyhow!(crate::Error {
71+
code: -32700,
72+
message: "Unexpected Response".to_string(),
73+
})
7174
})
7275
}
7376
}
@@ -98,14 +101,16 @@ impl ClientConnection {
98101
pub fn request<R: ClientRequest>(
99102
&self,
100103
params: R,
101-
) -> impl use<R> + Future<Output = Result<R::Response, crate::Error>> {
104+
) -> impl use<R> + Future<Output = Result<R::Response>> {
102105
let params = params.into_any();
103106
let result = self.0.request(params.method_name(), params);
104107
async move {
105108
let result = result.await?;
106-
R::response_from_any(result).ok_or_else(|| Error {
107-
code: -32700,
108-
message: "Could not parse".to_string(),
109+
R::response_from_any(result).ok_or_else(|| {
110+
anyhow!(Error {
111+
code: -32700,
112+
message: "Could not parse".to_string(),
113+
})
109114
})
110115
}
111116
}
@@ -157,6 +162,20 @@ pub struct Error {
157162
pub message: String,
158163
}
159164

165+
impl std::error::Error for Error {}
166+
impl Display for Error {
167+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168+
write!(f, "{}: {}", self.code, self.message)
169+
}
170+
}
171+
172+
#[derive(Serialize)]
173+
pub struct JsonRpcMessage<Req, Resp> {
174+
pub jsonrpc: &'static str,
175+
#[serde(flatten)]
176+
message: OutgoingMessage<Req, Resp>,
177+
}
178+
160179
impl<In, Out> Connection<In, Out>
161180
where
162181
In: AnyRequest,
@@ -193,10 +212,11 @@ where
193212
&self,
194213
method: &'static str,
195214
params: Out,
196-
) -> impl use<In, Out> + Future<Output = Result<Out::Response, crate::Error>> {
215+
) -> impl use<In, Out> + Future<Output = Result<Out::Response>> {
197216
let (tx, rx) = oneshot::channel();
198217
let id = self.next_id.fetch_add(1, SeqCst);
199-
if self
218+
self.response_senders.lock().insert(id, (method, tx));
219+
if !self
200220
.outgoing_tx
201221
.unbounded_send(OutgoingMessage::Request {
202222
id,
@@ -205,15 +225,9 @@ where
205225
})
206226
.is_ok()
207227
{
208-
// if the io thread has aborted, immediately drop tx.
209-
self.response_senders.lock().insert(id, (method, tx));
210-
}
211-
async move {
212-
rx.await.map_err(|_| Error {
213-
code: -9,
214-
message: "acp connection lost".to_string(),
215-
})?
228+
self.response_senders.lock().remove(&id);
216229
}
230+
async move { rx.await?.map_err(|e| anyhow!(e)) }
217231
}
218232

219233
async fn handle_io(

0 commit comments

Comments
 (0)