deribit_websocket/message/
notification.rs

1//! WebSocket notification message handling
2
3use crate::model::ws_types::JsonRpcNotification;
4
5/// Notification handler for WebSocket messages
6#[derive(Debug, Clone)]
7pub struct NotificationHandler;
8
9impl NotificationHandler {
10    /// Create a new notification handler
11    pub fn new() -> Self {
12        Self
13    }
14
15    /// Parse a JSON-RPC notification
16    pub fn parse_notification(&self, data: &str) -> Result<JsonRpcNotification, serde_json::Error> {
17        serde_json::from_str(data)
18    }
19
20    /// Check if this is a subscription notification
21    pub fn is_subscription_notification(&self, notification: &JsonRpcNotification) -> bool {
22        notification.method.starts_with("subscription")
23    }
24
25    /// Extract channel from subscription notification
26    pub fn extract_channel(&self, notification: &JsonRpcNotification) -> Option<String> {
27        if let Some(params) = &notification.params
28            && let Some(channel) = params.get("channel")
29        {
30            return channel.as_str().map(|s| s.to_string());
31        }
32        None
33    }
34
35    /// Extract data from subscription notification
36    pub fn extract_data(&self, notification: &JsonRpcNotification) -> Option<serde_json::Value> {
37        if let Some(params) = &notification.params
38            && let Some(data) = params.get("data")
39        {
40            return Some(data.clone());
41        }
42        None
43    }
44}
45
46impl Default for NotificationHandler {
47    fn default() -> Self {
48        Self::new()
49    }
50}