rusty_common/websocket/bridge/
message_router.rs

1//! Message routing for channel bridge
2//!
3//! Defines the interface for routing WebSocket messages to appropriate channels.
4
5use super::super::{Message, WebSocketResult};
6use std::any::Any;
7
8/// Type of subscription
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum SubscriptionType {
11    /// Trade/ticker data
12    Trades,
13    /// Order book depth data
14    OrderBook,
15    /// Instrument/symbol information
16    Instruments,
17    /// Order updates
18    Orders,
19    /// Account balance updates
20    Balances,
21    /// Custom subscription type
22    Custom(&'static str),
23}
24
25/// Routed message with metadata
26pub struct RoutedMessage {
27    /// Subscription key (e.g., "trades:BTC-USD")
28    pub subscription_key: String,
29    /// Type of subscription
30    pub subscription_type: SubscriptionType,
31    /// Parsed message payload
32    pub payload: Box<dyn Any + Send>,
33}
34
35/// Message router trait
36pub trait MessageRouter: Send + Sync {
37    /// Route a message to the appropriate subscription
38    ///
39    /// Returns None if the message is not a subscription message (e.g., auth response, pong)
40    fn route_message(&self, message: &Message) -> WebSocketResult<Option<RoutedMessage>>;
41
42    /// Get subscription key for a symbol and type
43    fn get_subscription_key(&self, symbol: &str, sub_type: SubscriptionType) -> String {
44        match sub_type {
45            SubscriptionType::Trades => format!("trades:{symbol}"),
46            SubscriptionType::OrderBook => format!("orderbook:{symbol}"),
47            SubscriptionType::Instruments => format!("instruments:{symbol}"),
48            SubscriptionType::Orders => format!("orders:{symbol}"),
49            SubscriptionType::Balances => "balances".to_string(),
50            SubscriptionType::Custom(name) => format!("{name}:{symbol}"),
51        }
52    }
53}