@@ -3,6 +3,41 @@ use jsonrpsee::types::error::ErrorObject as JsonRpcError;
33use serde:: { Deserialize , Serialize } ;
44use std:: os:: unix:: io:: OwnedFd ;
55
6+ /// The JSON key used to identify file descriptor placeholders.
7+ pub const FD_PLACEHOLDER_KEY : & str = "__jsonrpc_fd__" ;
8+ /// The JSON key for the file descriptor index within a placeholder.
9+ pub const FD_INDEX_KEY : & str = "index" ;
10+ /// The JSON-RPC protocol version.
11+ pub const JSONRPC_VERSION : & str = "2.0" ;
12+
13+ /// Count file descriptor placeholders in a JSON value.
14+ pub fn count_fd_placeholders ( value : & serde_json:: Value ) -> usize {
15+ fn count_inner ( value : & serde_json:: Value , count : & mut usize ) {
16+ match value {
17+ serde_json:: Value :: Object ( map) => {
18+ if let ( Some ( serde_json:: Value :: Bool ( true ) ) , Some ( _) ) =
19+ ( map. get ( FD_PLACEHOLDER_KEY ) , map. get ( FD_INDEX_KEY ) )
20+ {
21+ * count += 1 ;
22+ } else {
23+ for v in map. values ( ) {
24+ count_inner ( v, count) ;
25+ }
26+ }
27+ }
28+ serde_json:: Value :: Array ( arr) => {
29+ for v in arr {
30+ count_inner ( v, count) ;
31+ }
32+ }
33+ _ => { }
34+ }
35+ }
36+ let mut count = 0 ;
37+ count_inner ( value, & mut count) ;
38+ count
39+ }
40+
641#[ derive( Debug , Clone , Serialize , Deserialize ) ]
742pub struct FileDescriptorPlaceholder {
843 #[ serde( rename = "__jsonrpc_fd__" ) ]
@@ -55,7 +90,7 @@ pub enum JsonRpcMessage {
5590impl JsonRpcRequest {
5691 pub fn new ( method : String , params : Option < serde_json:: Value > , id : serde_json:: Value ) -> Self {
5792 Self {
58- jsonrpc : "2.0" . to_string ( ) ,
93+ jsonrpc : JSONRPC_VERSION . to_string ( ) ,
5994 method,
6095 params,
6196 id,
@@ -66,7 +101,7 @@ impl JsonRpcRequest {
66101impl JsonRpcResponse {
67102 pub fn success ( result : serde_json:: Value , id : serde_json:: Value ) -> Self {
68103 Self {
69- jsonrpc : "2.0" . to_string ( ) ,
104+ jsonrpc : JSONRPC_VERSION . to_string ( ) ,
70105 result : Some ( result) ,
71106 error : None ,
72107 id,
@@ -75,7 +110,7 @@ impl JsonRpcResponse {
75110
76111 pub fn error ( error : JsonRpcError < ' static > , id : serde_json:: Value ) -> Self {
77112 Self {
78- jsonrpc : "2.0" . to_string ( ) ,
113+ jsonrpc : JSONRPC_VERSION . to_string ( ) ,
79114 result : None ,
80115 error : Some ( error) ,
81116 id,
@@ -86,7 +121,7 @@ impl JsonRpcResponse {
86121impl JsonRpcNotification {
87122 pub fn new ( method : String , params : Option < serde_json:: Value > ) -> Self {
88123 Self {
89- jsonrpc : "2.0" . to_string ( ) ,
124+ jsonrpc : JSONRPC_VERSION . to_string ( ) ,
90125 method,
91126 params,
92127 }
@@ -137,18 +172,30 @@ impl MessageWithFds {
137172 }
138173
139174 pub fn serialize_with_placeholders ( & self ) -> Result < String > {
175+ self . serialize_with_placeholders_impl ( false )
176+ }
177+
178+ pub fn serialize_with_placeholders_pretty ( & self ) -> Result < String > {
179+ self . serialize_with_placeholders_impl ( true )
180+ }
181+
182+ fn serialize_with_placeholders_impl ( & self , pretty : bool ) -> Result < String > {
140183 let mut message_json = self . message . to_json_value ( ) ?;
141184 self . insert_placeholders ( & mut message_json) ?;
142185
143- let json_str = serde_json:: to_string ( & message_json) ?;
144- Ok ( format ! ( "{}\n " , json_str) )
186+ let json_str = if pretty {
187+ serde_json:: to_string_pretty ( & message_json) ?
188+ } else {
189+ serde_json:: to_string ( & message_json) ?
190+ } ;
191+ Ok ( json_str)
145192 }
146193
147194 fn insert_placeholders ( & self , value : & mut serde_json:: Value ) -> Result < ( ) > {
148195 let fd_count = self . file_descriptors . len ( ) ;
149196 let mut placeholder_indices = Vec :: new ( ) ;
150197
151- self . collect_placeholder_indices ( value, & mut placeholder_indices) ;
198+ Self :: collect_placeholder_indices ( value, & mut placeholder_indices) ;
152199
153200 if placeholder_indices. len ( ) != fd_count {
154201 return Err ( Error :: MismatchedCount {
@@ -166,26 +213,26 @@ impl MessageWithFds {
166213 Ok ( ( ) )
167214 }
168215
169- fn collect_placeholder_indices ( & self , value : & serde_json:: Value , indices : & mut Vec < usize > ) {
216+ fn collect_placeholder_indices ( value : & serde_json:: Value , indices : & mut Vec < usize > ) {
170217 match value {
171218 serde_json:: Value :: Object ( map) => {
172219 if let (
173220 Some ( serde_json:: Value :: Bool ( true ) ) ,
174221 Some ( serde_json:: Value :: Number ( index) ) ,
175- ) = ( map. get ( "__jsonrpc_fd__" ) , map. get ( "index" ) )
222+ ) = ( map. get ( FD_PLACEHOLDER_KEY ) , map. get ( FD_INDEX_KEY ) )
176223 {
177224 if let Some ( index) = index. as_u64 ( ) {
178225 indices. push ( index as usize ) ;
179226 }
180227 } else {
181228 for v in map. values ( ) {
182- self . collect_placeholder_indices ( v, indices) ;
229+ Self :: collect_placeholder_indices ( v, indices) ;
183230 }
184231 }
185232 }
186233 serde_json:: Value :: Array ( arr) => {
187234 for v in arr {
188- self . collect_placeholder_indices ( v, indices) ;
235+ Self :: collect_placeholder_indices ( v, indices) ;
189236 }
190237 }
191238 _ => { }
@@ -195,8 +242,7 @@ impl MessageWithFds {
195242 pub fn from_json_with_fds ( json_str : & str , fds : Vec < OwnedFd > ) -> Result < Self > {
196243 let message_json: serde_json:: Value = serde_json:: from_str ( json_str) ?;
197244
198- let mut placeholder_count = 0 ;
199- Self :: count_placeholders ( & message_json, & mut placeholder_count) ;
245+ let placeholder_count = count_fd_placeholders ( & message_json) ;
200246
201247 if placeholder_count != fds. len ( ) {
202248 return Err ( Error :: MismatchedCount {
@@ -215,28 +261,6 @@ impl MessageWithFds {
215261 Ok ( Self :: new ( message, fds) )
216262 }
217263
218- fn count_placeholders ( value : & serde_json:: Value , count : & mut usize ) {
219- match value {
220- serde_json:: Value :: Object ( map) => {
221- if let ( Some ( serde_json:: Value :: Bool ( true ) ) , Some ( _) ) =
222- ( map. get ( "__jsonrpc_fd__" ) , map. get ( "index" ) )
223- {
224- * count += 1 ;
225- } else {
226- for v in map. values ( ) {
227- Self :: count_placeholders ( v, count) ;
228- }
229- }
230- }
231- serde_json:: Value :: Array ( arr) => {
232- for v in arr {
233- Self :: count_placeholders ( v, count) ;
234- }
235- }
236- _ => { }
237- }
238- }
239-
240264 fn validate_placeholder_indices (
241265 value : & serde_json:: Value ,
242266 expected_count : usize ,
@@ -260,7 +284,7 @@ impl MessageWithFds {
260284 if let (
261285 Some ( serde_json:: Value :: Bool ( true ) ) ,
262286 Some ( serde_json:: Value :: Number ( index) ) ,
263- ) = ( map. get ( "__jsonrpc_fd__" ) , map. get ( "index" ) )
287+ ) = ( map. get ( FD_PLACEHOLDER_KEY ) , map. get ( FD_INDEX_KEY ) )
264288 {
265289 if let Some ( index) = index. as_u64 ( ) {
266290 indices. push ( index as usize ) ;
0 commit comments