deribit_websocket/message/
response.rs1use crate::model::ws_types::{JsonRpcError, JsonRpcResponse, JsonRpcResult};
4
5#[derive(Debug, Clone)]
7pub struct ResponseHandler;
8
9impl ResponseHandler {
10 pub fn new() -> Self {
12 Self
13 }
14
15 pub fn parse_response(&self, data: &str) -> Result<JsonRpcResponse, serde_json::Error> {
17 serde_json::from_str(data)
18 }
19
20 pub fn is_success(&self, response: &JsonRpcResponse) -> bool {
22 matches!(response.result, JsonRpcResult::Success { .. })
23 }
24
25 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 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}