22mod acp_tests;
33mod schema;
44
5- use anyhow:: Result ;
5+ use anyhow:: { Result , anyhow } ;
66use futures:: {
77 AsyncBufReadExt as _, AsyncRead , AsyncWrite , AsyncWriteExt as _, FutureExt as _,
88 StreamExt as _,
@@ -20,6 +20,7 @@ use serde::{Deserialize, Serialize};
2020use serde_json:: value:: RawValue ;
2121use 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+
160179impl < In , Out > Connection < In , Out >
161180where
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