rs2_stream/connectors/
stream_connector.rs

1//! Core traits for stream connectors
2
3use crate::RS2Stream;
4use async_trait::async_trait;
5use serde::{Deserialize, Serialize};
6use std::time::Duration;
7
8/// Common configuration for all connectors
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct CommonConfig {
11    /// Batch size for processing
12    pub batch_size: usize,
13    /// Timeout for operations in milliseconds
14    pub timeout_ms: u64,
15    /// Number of retry attempts
16    pub retry_attempts: usize,
17    /// Enable compression
18    pub compression: bool,
19}
20
21impl Default for CommonConfig {
22    fn default() -> Self {
23        Self {
24            batch_size: 100,
25            timeout_ms: 30000,
26            retry_attempts: 3,
27            compression: false,
28        }
29    }
30}
31
32/// Main trait for stream connectors
33#[async_trait]
34pub trait StreamConnector<T>: Send + Sync
35where
36    T: Send + 'static,
37{
38    /// Configuration type for this connector
39    type Config: Send + Sync;
40
41    /// Error type for this connector
42    type Error: std::error::Error + Send + Sync + 'static;
43
44    /// Metadata type returned by operations
45    type Metadata: Send + Sync;
46
47    /// Create a source stream from the connector
48    async fn from_source(&self, config: Self::Config) -> Result<RS2Stream<T>, Self::Error>;
49
50    /// Send a stream to the connector as a sink
51    async fn to_sink(
52        &self,
53        stream: RS2Stream<T>,
54        config: Self::Config,
55    ) -> Result<Self::Metadata, Self::Error>;
56
57    /// Check if the connector is healthy
58    async fn health_check(&self) -> Result<bool, Self::Error>;
59
60    /// Get connector metadata
61    async fn metadata(&self) -> Result<Self::Metadata, Self::Error>;
62
63    /// Get connector name
64    fn name(&self) -> &'static str;
65
66    /// Get connector version
67    fn version(&self) -> &'static str;
68}
69
70/// Trait for bidirectional connectors (can both produce and consume)
71#[async_trait]
72pub trait BidirectionalConnector<T>: StreamConnector<T>
73where
74    T: Send + 'static,
75{
76    /// Create a bidirectional stream (source and sink combined)
77    async fn bidirectional(
78        &self,
79        input_config: Self::Config,
80        output_config: Self::Config,
81    ) -> Result<
82        (
83            RS2Stream<T>,
84            Box<dyn Fn(RS2Stream<T>) -> Result<(), Self::Error> + Send + Sync>,
85        ),
86        Self::Error,
87    >;
88}
89
90/// Health status for connectors
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct HealthStatus {
93    pub healthy: bool,
94    pub message: String,
95    pub last_check: Duration,
96}
97
98/// Connection statistics
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct ConnectionStats {
101    pub messages_sent: u64,
102    pub messages_received: u64,
103    pub bytes_sent: u64,
104    pub bytes_received: u64,
105    pub errors: u64,
106    pub uptime: Duration,
107}