pub enum JsonRpcMessage {
Request(JsonRpcRequest),
Response(JsonRpcResponse),
Notification(JsonRpcNotification),
}Expand description
JSON-RPC message types supporting requests, responses, and notifications
This enum unifies all JSON-RPC 2.0 message types into a single type for transport and handling. Each variant preserves the specific structure of its message type while providing unified serialization.
Variants§
Request(JsonRpcRequest)
JSON-RPC request message
Response(JsonRpcResponse)
JSON-RPC response message
Notification(JsonRpcNotification)
JSON-RPC notification message
Implementations§
Source§impl JsonRpcMessage
JSON-RPC 2.0 message validation and utilities
impl JsonRpcMessage
JSON-RPC 2.0 message validation and utilities
Sourcepub fn validate(&self) -> Result<(), JsonRpcError>
pub fn validate(&self) -> Result<(), JsonRpcError>
Validate a JSON-RPC message according to JSON-RPC 2.0 specification
This function performs comprehensive validation including:
- JSON-RPC version validation (must be exactly “2.0”)
- Method field validation (must be present and non-empty for requests/notifications)
- Request ID validation (proper format checking)
- Mutual exclusion of result/error in responses
§Returns
Ok(())- Message is valid according to JSON-RPC 2.0 specErr(JsonRpcError)- Message violates JSON-RPC 2.0 specification
§Examples
use airsprotocols_mcp::protocol::{JsonRpcMessage, JsonRpcRequest, RequestId};
use serde_json::json;
let request = JsonRpcMessage::Request(JsonRpcRequest::new(
"ping",
None,
RequestId::new_number(1)
));
assert!(request.validate().is_ok());Sourcepub fn create_error_response(
code: i32,
message: &str,
data: Option<Value>,
id: Option<RequestId>,
) -> Self
pub fn create_error_response( code: i32, message: &str, data: Option<Value>, id: Option<RequestId>, ) -> Self
Create a standardized JSON-RPC error response
This function creates a properly formatted JSON-RPC 2.0 error response according to the specification with the correct error object structure.
§Arguments
code- JSON-RPC error code (use constants from error_codes module)message- Human-readable error messagedata- Optional additional error dataid- Request ID from the original request (None for parse errors)
§Examples
use airsprotocols_mcp::protocol::{JsonRpcMessage, RequestId};
use airsprotocols_mcp::protocol::constants::error_codes;
let error_response = JsonRpcMessage::create_error_response(
error_codes::INVALID_REQUEST,
"Missing required field",
None,
Some(RequestId::new_number(1))
);Sourcepub fn parse_and_validate_from_slice(data: &[u8]) -> Result<Self, Self>
pub fn parse_and_validate_from_slice(data: &[u8]) -> Result<Self, Self>
Parse and validate a JSON-RPC message from a byte slice
This method combines parsing and validation in a single step, providing proper JSON-RPC error responses for invalid messages. Works directly with byte slices for optimal performance.
§Performance Benefits
- No intermediate string allocation
- Single UTF-8 validation + JSON parsing step
- Better cache locality for large messages
§Arguments
data- Raw byte slice containing JSON data
§Returns
Ok(JsonRpcMessage) if parsing and validation succeed,
Err(JsonRpcMessage) containing a JSON-RPC error response if validation fails
§Examples
use airsprotocols_mcp::protocol::JsonRpcMessage;
// Parse from byte slice (most efficient)
let data = br#"{"jsonrpc":"2.0","method":"ping","id":1}"#;
let result = JsonRpcMessage::parse_and_validate_from_slice(data);
assert!(result.is_ok());
// Parse from string (convert to bytes first)
let json_str = r#"{"jsonrpc":"2.0","method":"ping","id":1}"#;
let result = JsonRpcMessage::parse_and_validate_from_slice(json_str.as_bytes());
assert!(result.is_ok());Source§impl JsonRpcMessage
impl JsonRpcMessage
Trait Implementations§
Source§impl Clone for JsonRpcMessage
impl Clone for JsonRpcMessage
Source§fn clone(&self) -> JsonRpcMessage
fn clone(&self) -> JsonRpcMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more