Skip to main content

mip_client/client/
types.rs

1use thiserror::Error;
2
3use crate::protocol::header::Header;
4
5// ============================================================================
6// Error Types
7// ============================================================================
8
9/// MIP Client errors
10#[derive(Debug, Error)]
11pub enum MIPError {
12    #[error("Connection error: {0}")]
13    Connection(String),
14
15    #[error("IO error: {0}")]
16    Io(#[from] std::io::Error),
17
18    #[error("Client not connected")]
19    NotConnected,
20
21    #[error("Invalid magic number: {0}")]
22    InvalidMagic(u32),
23
24    #[error("Protocol error: {0}")]
25    Protocol(String),
26
27    #[error("Max reconnection attempts ({0}) reached")]
28    MaxReconnectAttempts(u32),
29
30    #[error("Server error: {0}")]
31    ServerError(String),
32}
33
34/// Result type for MIP operations
35pub type MIPResult<T> = Result<T, MIPError>;
36
37// ============================================================================
38// Configuration Options
39// ============================================================================
40
41/// Configuration options for the MIP client
42#[derive(Debug, Clone)]
43pub struct MIPClientOptions {
44    /// Unique client identifier (optional)
45    pub client_id: String,
46    /// Server host address
47    pub host: String,
48    /// Server port number
49    pub port: u16,
50    /// Auto-reconnect on disconnect
51    pub auto_reconnect: bool,
52    /// Reconnect delay in milliseconds
53    pub reconnect_delay_ms: u64,
54    /// Maximum reconnection attempts (0 = infinite)
55    pub max_reconnect_attempts: u32,
56    /// Ping interval in milliseconds (0 = disabled)
57    pub ping_interval_ms: u64,
58}
59
60impl Default for MIPClientOptions {
61    fn default() -> Self {
62        Self {
63            host: "127.0.0.1".to_string(),
64            port: 9000,
65            client_id: "".to_string(),
66            auto_reconnect: true,
67            reconnect_delay_ms: 3000,
68            max_reconnect_attempts: 10,
69            ping_interval_ms: 0,
70        }
71    }
72}
73
74impl MIPClientOptions {
75    /// Client ID (optional, can be used for authentication or tracking)
76    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
77        self.client_id = client_id.into();
78        self
79    }
80
81    /// Set the host address
82    pub fn host(mut self, host: impl Into<String>) -> Self {
83        self.host = host.into();
84        self
85    }
86
87    /// Set the port number
88    pub fn port(mut self, port: u16) -> Self {
89        self.port = port;
90        self
91    }
92
93    /// Set auto-reconnect option
94    pub fn auto_reconnect(mut self, enabled: bool) -> Self {
95        self.auto_reconnect = enabled;
96        self
97    }
98
99    /// Set reconnect delay in milliseconds
100    pub fn reconnect_delay_ms(mut self, delay: u64) -> Self {
101        self.reconnect_delay_ms = delay;
102        self
103    }
104
105    /// Set maximum reconnection attempts
106    pub fn max_reconnect_attempts(mut self, attempts: u32) -> Self {
107        self.max_reconnect_attempts = attempts;
108        self
109    }
110
111    /// Set ping interval in milliseconds
112    pub fn ping_interval_ms(mut self, interval: u64) -> Self {
113        self.ping_interval_ms = interval;
114        self
115    }
116}
117
118// ============================================================================
119// Message Types
120// ============================================================================
121
122/// Received message event
123#[derive(Debug, Clone)]
124pub struct MIPMessage {
125    /// Frame header
126    pub header: Header,
127    /// Topic name
128    pub topic: String,
129    /// Message content
130    pub message: String,
131}