deribit_websocket/message/
response.rs

1//! WebSocket response message handling
2
3use crate::model::ws_types::{JsonRpcError, JsonRpcResponse, JsonRpcResult};
4
5/// Response handler for WebSocket messages
6#[derive(Debug, Clone)]
7pub struct ResponseHandler;
8
9impl ResponseHandler {
10    /// Create a new response handler
11    pub fn new() -> Self {
12        Self
13    }
14
15    /// Parse a JSON-RPC response
16    pub fn parse_response(&self, data: &str) -> Result<JsonRpcResponse, serde_json::Error> {
17        serde_json::from_str(data)
18    }
19
20    /// Check if response is successful
21    pub fn is_success(&self, response: &JsonRpcResponse) -> bool {
22        matches!(response.result, JsonRpcResult::Success { .. })
23    }
24
25    /// Extract result from successful response
26    pub fn extract_result<'a>(
27        &self,
28        response: &'a JsonRpcResponse,
29    ) -> Option<&'a serde_json::Value> {
30        match &response.result {
31            JsonRpcResult::Success { result } => Some(result),
32            JsonRpcResult::Error { .. } => None,
33        }
34    }
35
36    /// Extract error from response
37    pub fn extract_error<'a>(&self, response: &'a JsonRpcResponse) -> Option<&'a JsonRpcError> {
38        match &response.result {
39            JsonRpcResult::Success { .. } => None,
40            JsonRpcResult::Error { error } => Some(error),
41        }
42    }
43}
44
45impl Default for ResponseHandler {
46    fn default() -> Self {
47        Self::new()
48    }
49}