Skip to main content

clasp_bridge/
traits.rs

1//! Bridge trait definitions
2
3use async_trait::async_trait;
4use clasp_core::Message;
5use tokio::sync::mpsc;
6
7use crate::Result;
8
9/// Events from a bridge
10#[derive(Debug, Clone)]
11pub enum BridgeEvent {
12    /// Message to send to Clasp
13    ToClasp(Box<Message>),
14    /// Bridge connected
15    Connected,
16    /// Bridge disconnected
17    Disconnected { reason: Option<String> },
18    /// Error occurred
19    Error(String),
20}
21
22/// Bridge configuration
23#[derive(Debug, Clone)]
24pub struct BridgeConfig {
25    /// Bridge name
26    pub name: String,
27    /// Protocol identifier
28    pub protocol: String,
29    /// Is bidirectional?
30    pub bidirectional: bool,
31    /// Protocol-specific options
32    pub options: std::collections::HashMap<String, String>,
33}
34
35impl Default for BridgeConfig {
36    fn default() -> Self {
37        Self {
38            name: "Bridge".to_string(),
39            protocol: "unknown".to_string(),
40            bidirectional: true,
41            options: std::collections::HashMap::new(),
42        }
43    }
44}
45
46/// Main bridge trait
47#[async_trait]
48pub trait Bridge: Send + Sync {
49    /// Get the bridge configuration
50    fn config(&self) -> &BridgeConfig;
51
52    /// Start the bridge
53    async fn start(&mut self) -> Result<mpsc::Receiver<BridgeEvent>>;
54
55    /// Stop the bridge
56    async fn stop(&mut self) -> Result<()>;
57
58    /// Send a message from Clasp to the bridged protocol
59    async fn send(&self, message: Message) -> Result<()>;
60
61    /// Check if the bridge is running
62    fn is_running(&self) -> bool;
63
64    /// Get the namespace this bridge provides
65    fn namespace(&self) -> &str;
66}