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    /// Server host address
45    pub host: String,
46    /// Server port number
47    pub port: u16,
48    /// Auto-reconnect on disconnect
49    pub auto_reconnect: bool,
50    /// Reconnect delay in milliseconds
51    pub reconnect_delay_ms: u64,
52    /// Maximum reconnection attempts (0 = infinite)
53    pub max_reconnect_attempts: u32,
54    /// Ping interval in milliseconds (0 = disabled)
55    pub ping_interval_ms: u64,
56}
57
58impl Default for MIPClientOptions {
59    fn default() -> Self {
60        Self {
61            host: "127.0.0.1".to_string(),
62            port: 9000,
63            auto_reconnect: true,
64            reconnect_delay_ms: 3000,
65            max_reconnect_attempts: 10,
66            ping_interval_ms: 0,
67        }
68    }
69}
70
71impl MIPClientOptions {
72    /// Set the host address
73    pub fn host(mut self, host: impl Into<String>) -> Self {
74        self.host = host.into();
75        self
76    }
77
78    /// Set the port number
79    pub fn port(mut self, port: u16) -> Self {
80        self.port = port;
81        self
82    }
83
84    /// Set auto-reconnect option
85    pub fn auto_reconnect(mut self, enabled: bool) -> Self {
86        self.auto_reconnect = enabled;
87        self
88    }
89
90    /// Set reconnect delay in milliseconds
91    pub fn reconnect_delay_ms(mut self, delay: u64) -> Self {
92        self.reconnect_delay_ms = delay;
93        self
94    }
95
96    /// Set maximum reconnection attempts
97    pub fn max_reconnect_attempts(mut self, attempts: u32) -> Self {
98        self.max_reconnect_attempts = attempts;
99        self
100    }
101
102    /// Set ping interval in milliseconds
103    pub fn ping_interval_ms(mut self, interval: u64) -> Self {
104        self.ping_interval_ms = interval;
105        self
106    }
107}
108
109// ============================================================================
110// Message Types
111// ============================================================================
112
113/// Received message event
114#[derive(Debug, Clone)]
115pub struct MIPMessage {
116    /// Frame header
117    pub header: Header,
118    /// Topic name
119    pub topic: String,
120    /// Message content
121    pub message: String,
122}