@@ -23,51 +23,69 @@ fn skip_if_zero_or_none(fds: &Option<usize>) -> bool {
2323 fds. map_or ( true , |n| n == 0 )
2424}
2525
26- // Define our own message types that don't have lifetime constraints
26+ /// A JSON-RPC 2.0 request message.
2727#[ derive( Debug , Clone , Serialize , Deserialize ) ]
2828pub struct JsonRpcRequest {
29+ /// The JSON-RPC protocol version (always "2.0").
2930 pub jsonrpc : String ,
31+ /// The name of the method to invoke.
3032 pub method : String ,
33+ /// Optional parameters for the method.
3134 #[ serde( skip_serializing_if = "Option::is_none" ) ]
3235 pub params : Option < serde_json:: Value > ,
36+ /// The request identifier.
3337 pub id : serde_json:: Value ,
34- /// Number of file descriptors attached to this message
38+ /// Number of file descriptors attached to this message.
3539 #[ serde( skip_serializing_if = "skip_if_zero_or_none" ) ]
3640 pub fds : Option < usize > ,
3741}
3842
43+ /// A JSON-RPC 2.0 response message.
3944#[ derive( Debug , Clone , Serialize , Deserialize ) ]
4045pub struct JsonRpcResponse {
46+ /// The JSON-RPC protocol version (always "2.0").
4147 pub jsonrpc : String ,
48+ /// The result of the method invocation (present on success).
4249 #[ serde( skip_serializing_if = "Option::is_none" ) ]
4350 pub result : Option < serde_json:: Value > ,
51+ /// The error object (present on failure).
4452 #[ serde( skip_serializing_if = "Option::is_none" ) ]
4553 pub error : Option < JsonRpcError < ' static > > ,
54+ /// The request identifier this response corresponds to.
4655 pub id : serde_json:: Value ,
47- /// Number of file descriptors attached to this message
56+ /// Number of file descriptors attached to this message.
4857 #[ serde( skip_serializing_if = "skip_if_zero_or_none" ) ]
4958 pub fds : Option < usize > ,
5059}
5160
61+ /// A JSON-RPC 2.0 notification message (a request without an id).
5262#[ derive( Debug , Clone , Serialize , Deserialize ) ]
5363pub struct JsonRpcNotification {
64+ /// The JSON-RPC protocol version (always "2.0").
5465 pub jsonrpc : String ,
66+ /// The name of the method to invoke.
5567 pub method : String ,
68+ /// Optional parameters for the method.
5669 #[ serde( skip_serializing_if = "Option::is_none" ) ]
5770 pub params : Option < serde_json:: Value > ,
58- /// Number of file descriptors attached to this message
71+ /// Number of file descriptors attached to this message.
5972 #[ serde( skip_serializing_if = "skip_if_zero_or_none" ) ]
6073 pub fds : Option < usize > ,
6174}
6275
76+ /// A JSON-RPC 2.0 message (request, response, or notification).
6377#[ derive( Debug , Clone ) ]
6478pub enum JsonRpcMessage {
79+ /// A request message expecting a response.
6580 Request ( JsonRpcRequest ) ,
81+ /// A response to a previous request.
6682 Response ( JsonRpcResponse ) ,
83+ /// A notification (no response expected).
6784 Notification ( JsonRpcNotification ) ,
6885}
6986
7087impl JsonRpcRequest {
88+ /// Create a new JSON-RPC request.
7189 pub fn new ( method : String , params : Option < serde_json:: Value > , id : serde_json:: Value ) -> Self {
7290 Self {
7391 jsonrpc : JSONRPC_VERSION . to_string ( ) ,
@@ -80,6 +98,7 @@ impl JsonRpcRequest {
8098}
8199
82100impl JsonRpcResponse {
101+ /// Create a successful JSON-RPC response.
83102 pub fn success ( result : serde_json:: Value , id : serde_json:: Value ) -> Self {
84103 Self {
85104 jsonrpc : JSONRPC_VERSION . to_string ( ) ,
@@ -90,6 +109,7 @@ impl JsonRpcResponse {
90109 }
91110 }
92111
112+ /// Create an error JSON-RPC response.
93113 pub fn error ( error : JsonRpcError < ' static > , id : serde_json:: Value ) -> Self {
94114 Self {
95115 jsonrpc : JSONRPC_VERSION . to_string ( ) ,
@@ -102,6 +122,7 @@ impl JsonRpcResponse {
102122}
103123
104124impl JsonRpcNotification {
125+ /// Create a new JSON-RPC notification.
105126 pub fn new ( method : String , params : Option < serde_json:: Value > ) -> Self {
106127 Self {
107128 jsonrpc : JSONRPC_VERSION . to_string ( ) ,
@@ -113,6 +134,7 @@ impl JsonRpcNotification {
113134}
114135
115136impl JsonRpcMessage {
137+ /// Convert this message to a JSON value.
116138 pub fn to_json_value ( & self ) -> Result < serde_json:: Value > {
117139 match self {
118140 JsonRpcMessage :: Request ( req) => Ok ( serde_json:: to_value ( req) ?) ,
@@ -121,6 +143,7 @@ impl JsonRpcMessage {
121143 }
122144 }
123145
146+ /// Parse a JSON-RPC message from a JSON value.
124147 pub fn from_json_value ( value : serde_json:: Value ) -> Result < Self > {
125148 if let serde_json:: Value :: Object ( obj) = & value {
126149 if obj. contains_key ( "method" ) && obj. contains_key ( "id" ) {
@@ -141,9 +164,12 @@ impl JsonRpcMessage {
141164 }
142165}
143166
167+ /// A JSON-RPC message paired with file descriptors to send or that were received.
144168#[ derive( Debug ) ]
145169pub struct MessageWithFds {
170+ /// The JSON-RPC message.
146171 pub message : JsonRpcMessage ,
172+ /// File descriptors attached to this message.
147173 pub file_descriptors : Vec < OwnedFd > ,
148174}
149175
@@ -169,6 +195,7 @@ impl JsonRpcMessage {
169195}
170196
171197impl MessageWithFds {
198+ /// Create a new message with file descriptors.
172199 pub fn new ( message : JsonRpcMessage , file_descriptors : Vec < OwnedFd > ) -> Self {
173200 Self {
174201 message,
@@ -218,8 +245,10 @@ impl MessageWithFds {
218245 }
219246}
220247
248+ /// JSON-RPC error code for file descriptor errors.
221249pub const FILE_DESCRIPTOR_ERROR_CODE : i32 = -32050 ;
222250
251+ /// Create a JSON-RPC error object for file descriptor errors.
223252pub fn file_descriptor_error ( ) -> JsonRpcError < ' static > {
224253 JsonRpcError :: owned (
225254 FILE_DESCRIPTOR_ERROR_CODE ,
0 commit comments