1- use std:: collections:: HashMap ;
2- use std:: path:: { Path , PathBuf } ;
3- use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
41use anyhow:: { bail, Context , Result } ;
52use serde:: { Deserialize , Serialize } ;
63use serde_json:: { self as encoder, Value } ;
4+ use std:: collections:: HashMap ;
5+ use std:: path:: { Path , PathBuf } ;
6+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
77use tokio:: io:: { AsyncBufReadExt , AsyncReadExt , AsyncWriteExt , BufStream } ;
88use tokio:: net:: UnixStream ;
99
@@ -62,19 +62,20 @@ impl Client {
6262 async fn jsonrpc_request ( & self , method : & str , params : Option < Value > ) -> Result < Value > {
6363 // First try JSON-RPC
6464 let result = self . try_jsonrpc ( method, params. clone ( ) ) . await ;
65-
65+
6666 // If JSON-RPC fails, try legacy protocol
6767 if let Err ( e) = & result {
68- if e. to_string ( ) . contains ( "Invalid JSON-RPC response" ) ||
69- e. to_string ( ) . contains ( "Failed to parse" ) {
68+ if e. to_string ( ) . contains ( "Invalid JSON-RPC response" )
69+ || e. to_string ( ) . contains ( "Failed to parse" )
70+ {
7071 debug ! ( "JSON-RPC failed, trying legacy protocol: {}" , e) ;
7172 return self . try_legacy_protocol ( method, params) . await ;
7273 }
7374 }
74-
75+
7576 result
7677 }
77-
78+
7879 // Try using JSON-RPC protocol
7980 async fn try_jsonrpc ( & self , method : & str , params : Option < Value > ) -> Result < Value > {
8081 // Get a unique ID for this request
@@ -146,7 +147,7 @@ impl Client {
146147 } else {
147148 bail ! ( "Missing parameters" ) ;
148149 }
149- } ,
150+ }
150151 "service.start" => {
151152 if let Some ( params) = params {
152153 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -157,7 +158,7 @@ impl Client {
157158 } else {
158159 bail ! ( "Missing parameters" ) ;
159160 }
160- } ,
161+ }
161162 "service.stop" => {
162163 if let Some ( params) = params {
163164 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -168,7 +169,7 @@ impl Client {
168169 } else {
169170 bail ! ( "Missing parameters" ) ;
170171 }
171- } ,
172+ }
172173 "service.forget" => {
173174 if let Some ( params) = params {
174175 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -179,7 +180,7 @@ impl Client {
179180 } else {
180181 bail ! ( "Missing parameters" ) ;
181182 }
182- } ,
183+ }
183184 "service.monitor" => {
184185 if let Some ( params) = params {
185186 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -190,7 +191,7 @@ impl Client {
190191 } else {
191192 bail ! ( "Missing parameters" ) ;
192193 }
193- } ,
194+ }
194195 "service.kill" => {
195196 if let Some ( params) = params {
196197 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -205,23 +206,23 @@ impl Client {
205206 } else {
206207 bail ! ( "Missing parameters" ) ;
207208 }
208- } ,
209+ }
209210 "service.create" => {
210211 // The legacy protocol doesn't directly support service creation
211212 bail ! ( "Service creation not supported in legacy protocol" ) ;
212- } ,
213+ }
213214 "service.delete" => {
214215 // The legacy protocol doesn't directly support service deletion
215216 bail ! ( "Service deletion not supported in legacy protocol" ) ;
216- } ,
217+ }
217218 "service.get" => {
218219 // The legacy protocol doesn't directly support getting service config
219220 bail ! ( "Getting service configuration not supported in legacy protocol" ) ;
220- } ,
221+ }
221222 "rpc.discover" => {
222223 // This is a JSON-RPC specific method with no legacy equivalent
223224 bail ! ( "RPC discovery not supported in legacy protocol" ) ;
224- } ,
225+ }
225226 "service.restart" => {
226227 if let Some ( params) = params {
227228 if let Some ( name) = params. get ( "name" ) . and_then ( |v| v. as_str ( ) ) {
@@ -232,7 +233,7 @@ impl Client {
232233 } else {
233234 bail ! ( "Missing parameters" ) ;
234235 }
235- } ,
236+ }
236237 "log" => {
237238 if let Some ( params) = params {
238239 if let Some ( filter) = params. get ( "filter" ) . and_then ( |v| v. as_str ( ) ) {
@@ -251,7 +252,7 @@ impl Client {
251252 } else {
252253 "log" . to_string ( )
253254 }
254- } ,
255+ }
255256 _ => bail ! ( "Unsupported method for legacy protocol: {}" , method) ,
256257 } ;
257258
@@ -450,4 +451,4 @@ impl Client {
450451
451452 self . jsonrpc_request ( "service.get" , Some ( params) ) . await
452453 }
453- }
454+ }
0 commit comments