use crate::model::ws_types::JsonRpcNotification;
#[derive(Debug, Clone)]
pub struct NotificationHandler;
impl NotificationHandler {
pub fn new() -> Self {
Self
}
pub fn parse_notification(&self, data: &str) -> Result<JsonRpcNotification, serde_json::Error> {
serde_json::from_str(data)
}
pub fn is_subscription_notification(&self, notification: &JsonRpcNotification) -> bool {
notification.method.starts_with("subscription")
}
pub fn extract_channel(&self, notification: &JsonRpcNotification) -> Option<String> {
if let Some(params) = ¬ification.params
&& let Some(channel) = params.get("channel")
{
return channel.as_str().map(|s| s.to_string());
}
None
}
pub fn extract_data(&self, notification: &JsonRpcNotification) -> Option<serde_json::Value> {
if let Some(params) = ¬ification.params
&& let Some(data) = params.get("data")
{
return Some(data.clone());
}
None
}
}
impl Default for NotificationHandler {
fn default() -> Self {
Self::new()
}
}