umicp_sdk/
types.rs

1/*!
2# UMICP Types
3
4Core type definitions for the UMICP protocol.
5*/
6
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// Operation types for UMICP messages
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum OperationType {
14    /// Control message for protocol management
15    Control = 0,
16    /// Regular data message
17    Data = 1,
18    /// Acknowledgment message
19    Ack = 2,
20    /// Error message
21    Error = 3,
22    /// Request message
23    Request = 4,
24    /// Response message
25    Response = 5,
26}
27
28impl Default for OperationType {
29    fn default() -> Self {
30        OperationType::Control
31    }
32}
33
34impl std::fmt::Display for OperationType {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        let op_str = match self {
37            OperationType::Control => "control",
38            OperationType::Data => "data",
39            OperationType::Ack => "ack",
40            OperationType::Error => "error",
41            OperationType::Request => "request",
42            OperationType::Response => "response",
43        };
44        write!(f, "{}", op_str)
45    }
46}
47
48/// Payload types for message content
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "lowercase")]
51pub enum PayloadType {
52    /// Vector data (numeric arrays)
53    Vector = 0,
54    /// Text data
55    Text = 1,
56    /// Metadata information
57    Metadata = 2,
58    /// Binary data
59    Binary = 3,
60}
61
62impl Default for PayloadType {
63    fn default() -> Self {
64        PayloadType::Text
65    }
66}
67
68impl std::fmt::Display for PayloadType {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        let type_str = match self {
71            PayloadType::Vector => "vector",
72            PayloadType::Text => "text",
73            PayloadType::Metadata => "metadata",
74            PayloadType::Binary => "binary",
75        };
76        write!(f, "{}", type_str)
77    }
78}
79
80/// Encoding types for numeric data
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82#[serde(rename_all = "lowercase")]
83pub enum EncodingType {
84    /// 32-bit floating point
85    Float32 = 0,
86    /// 64-bit floating point
87    Float64 = 1,
88    /// 32-bit signed integer
89    Int32 = 2,
90    /// 64-bit signed integer
91    Int64 = 3,
92    /// 8-bit unsigned integer
93    Uint8 = 4,
94    /// 16-bit unsigned integer
95    Uint16 = 5,
96    /// 32-bit unsigned integer
97    Uint32 = 6,
98    /// 64-bit unsigned integer
99    Uint64 = 7,
100}
101
102impl Default for EncodingType {
103    fn default() -> Self {
104        EncodingType::Float32
105    }
106}
107
108impl std::fmt::Display for EncodingType {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        let enc_str = match self {
111            EncodingType::Float32 => "float32",
112            EncodingType::Float64 => "float64",
113            EncodingType::Int32 => "int32",
114            EncodingType::Int64 => "int64",
115            EncodingType::Uint8 => "uint8",
116            EncodingType::Uint16 => "uint16",
117            EncodingType::Uint32 => "uint32",
118            EncodingType::Uint64 => "uint64",
119        };
120        write!(f, "{}", enc_str)
121    }
122}
123
124/// Payload hint for message content description
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct PayloadHint {
127    /// Type of payload
128    pub payload_type: PayloadType,
129    /// Size in bytes (optional)
130    pub size: Option<u64>,
131    /// Encoding type for numeric data (optional)
132    pub encoding: Option<EncodingType>,
133    /// Number of elements (optional)
134    pub count: Option<u64>,
135}
136
137impl Default for PayloadHint {
138    fn default() -> Self {
139        PayloadHint {
140            payload_type: PayloadType::Text,
141            size: None,
142            encoding: None,
143            count: None,
144        }
145    }
146}
147
148/// Transport statistics
149#[derive(Debug, Clone, Serialize, Deserialize, Default)]
150pub struct TransportStats {
151    /// Total messages sent
152    pub messages_sent: u64,
153    /// Total messages received
154    pub messages_received: u64,
155    /// Total bytes sent
156    pub bytes_sent: u64,
157    /// Total bytes received
158    pub bytes_received: u64,
159    /// Current connection count
160    pub active_connections: u32,
161    /// Total connection count
162    pub total_connections: u64,
163    /// Uptime in seconds
164    pub uptime_seconds: u64,
165    /// Average latency in milliseconds
166    pub avg_latency_ms: Option<f64>,
167}
168
169/// Connection information
170#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct ConnectionInfo {
172    /// Unique connection ID
173    pub id: String,
174    /// Remote address
175    pub remote_addr: String,
176    /// Local address
177    pub local_addr: String,
178    /// Connection established timestamp
179    pub connected_at: chrono::DateTime<chrono::Utc>,
180    /// Last activity timestamp
181    pub last_activity: chrono::DateTime<chrono::Utc>,
182}
183
184/// Matrix operation result
185#[derive(Debug, Clone)]
186pub struct MatrixResult {
187    /// Operation success status
188    pub success: bool,
189    /// Error message if operation failed
190    pub error: Option<String>,
191    /// Numeric result (for dot product, etc.)
192    pub result: Option<f64>,
193    /// Similarity score (for cosine similarity)
194    pub similarity: Option<f64>,
195    /// Vector/matrix data result
196    pub data: Option<Vec<f32>>,
197}
198
199/// Frame options for advanced messaging
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct FrameOptions {
202    /// Frame type identifier
203    pub frame_type: Option<u32>,
204    /// Stream ID for multiplexing
205    pub stream_id: Option<u32>,
206    /// Sequence number
207    pub sequence: Option<u64>,
208    /// Frame flags
209    pub flags: Option<u32>,
210    /// Compression enabled
211    pub compressed: bool,
212    /// Encryption enabled
213    pub encrypted: bool,
214}
215
216impl Default for FrameOptions {
217    fn default() -> Self {
218        FrameOptions {
219            frame_type: None,
220            stream_id: None,
221            sequence: None,
222            flags: None,
223            compressed: false,
224            encrypted: false,
225        }
226    }
227}
228
229/// Transport configuration
230#[derive(Debug, Clone)]
231pub struct TransportConfig {
232    /// Maximum payload size in bytes
233    pub max_payload_size: usize,
234    /// Heartbeat interval in seconds
235    pub heartbeat_interval: u64,
236    /// Maximum reconnection attempts
237    pub max_reconnect_attempts: u32,
238    /// Connection timeout in seconds
239    pub connection_timeout: u64,
240    /// Enable compression
241    pub compression_enabled: bool,
242    /// Enable TLS/SSL
243    pub tls_enabled: bool,
244    /// TLS certificate path (optional)
245    pub tls_cert_path: Option<String>,
246    /// TLS private key path (optional)
247    pub tls_key_path: Option<String>,
248}
249
250impl Default for TransportConfig {
251    fn default() -> Self {
252        TransportConfig {
253            max_payload_size: 1024 * 1024, // 1MB
254            heartbeat_interval: 30,
255            max_reconnect_attempts: 3,
256            connection_timeout: 10,
257            compression_enabled: true,
258            tls_enabled: false,
259            tls_cert_path: None,
260            tls_key_path: None,
261        }
262    }
263}
264
265/// Envelope capabilities (key-value metadata with native types)
266///
267/// BREAKING CHANGE (v0.2.0): Changed from HashMap<String, String> to HashMap<String, Value>
268/// to support native JSON types (numbers, booleans, arrays, objects).
269pub type Capabilities = HashMap<String, serde_json::Value>;
270
271/// Accepted content types
272pub type AcceptTypes = Vec<String>;
273
274/// Payload references for multi-part messages
275pub type PayloadRefs = Vec<HashMap<String, String>>;