elicitation 0.8.0

Conversational elicitation of strongly-typed Rust values via MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Socket address byte-level validation foundation.
//!
//! This module provides validated socket address byte sequences (IP + port).
//! It forms the foundation for socket address contract types.

use super::ValidationError;
use super::ipaddr_bytes::{Ipv4Bytes, Ipv6Bytes};

// ============================================================================
// Port Ranges (IANA)
// ============================================================================
//
// Well-Known Ports: 0-1023
//   - Assigned by IANA for standard services
//   - Examples: 80 (HTTP), 443 (HTTPS), 22 (SSH)
//
// Registered Ports: 1024-49151
//   - Registered with IANA for specific applications
//   - Examples: 3000 (dev servers), 5432 (PostgreSQL)
//
// Dynamic/Private Ports: 49152-65535
//   - Available for dynamic allocation, ephemeral connections
//
// Special:
//   - Port 0: Invalid for binding (used for "any available port")

// ============================================================================
// Port Validation
// ============================================================================

/// Check if port is well-known (0-1023).
pub fn is_well_known_port(port: u16) -> bool {
    port <= 1023
}

/// Check if port is registered (1024-49151).
pub fn is_registered_port(port: u16) -> bool {
    (1024..=49151).contains(&port)
}

/// Check if port is dynamic/private (49152-65535).
pub fn is_dynamic_port(port: u16) -> bool {
    port >= 49152
}

/// Check if port is privileged (< 1024, requires special permissions).
pub fn is_privileged_port(port: u16) -> bool {
    port < 1024
}

/// Check if port is non-zero.
pub fn is_nonzero_port(port: u16) -> bool {
    port != 0
}

// ============================================================================
// SocketAddrV4
// ============================================================================

/// A validated IPv4 socket address (IPv4 + port).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV4Bytes {
    ip: Ipv4Bytes,
    port: u16,
}

impl SocketAddrV4Bytes {
    /// Create a new SocketAddrV4Bytes.
    ///
    /// Always succeeds since all combinations are valid.
    pub fn new(ip: Ipv4Bytes, port: u16) -> Self {
        Self { ip, port }
    }

    /// Create from raw octets and port.
    pub fn from_octets(ip_octets: [u8; 4], port: u16) -> Self {
        Self::new(Ipv4Bytes::new(ip_octets), port)
    }

    /// Get the IP address.
    pub fn ip(&self) -> &Ipv4Bytes {
        &self.ip
    }

    /// Get the port.
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Decompose into IP and port.
    pub fn into_parts(self) -> (Ipv4Bytes, u16) {
        (self.ip, self.port)
    }
}

// ============================================================================
// SocketAddrV6
// ============================================================================

/// A validated IPv6 socket address (IPv6 + port).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV6Bytes {
    ip: Ipv6Bytes,
    port: u16,
}

impl SocketAddrV6Bytes {
    /// Create a new SocketAddrV6Bytes.
    ///
    /// Always succeeds since all combinations are valid.
    pub fn new(ip: Ipv6Bytes, port: u16) -> Self {
        Self { ip, port }
    }

    /// Create from raw octets and port.
    pub fn from_octets(ip_octets: [u8; 16], port: u16) -> Self {
        Self::new(Ipv6Bytes::new(ip_octets), port)
    }

    /// Get the IP address.
    pub fn ip(&self) -> &Ipv6Bytes {
        &self.ip
    }

    /// Get the port.
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Decompose into IP and port.
    pub fn into_parts(self) -> (Ipv6Bytes, u16) {
        (self.ip, self.port)
    }
}

// ============================================================================
// Contract Types: Non-Zero Port
// ============================================================================

/// An IPv4 socket address with non-zero port.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV4NonZero(SocketAddrV4Bytes);

impl SocketAddrV4NonZero {
    /// Create a new SocketAddrV4NonZero, validating port is non-zero.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortIsZero` if port is 0.
    pub fn new(ip: Ipv4Bytes, port: u16) -> Result<Self, ValidationError> {
        if port == 0 {
            return Err(ValidationError::PortIsZero);
        }
        Ok(Self(SocketAddrV4Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV4Bytes.
    pub fn get(&self) -> &SocketAddrV4Bytes {
        &self.0
    }

    /// Get the IP address.
    pub fn ip(&self) -> &Ipv4Bytes {
        self.0.ip()
    }

    /// Get the port (guaranteed non-zero).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV4Bytes.
    pub fn into_inner(self) -> SocketAddrV4Bytes {
        self.0
    }
}

/// An IPv6 socket address with non-zero port.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV6NonZero(SocketAddrV6Bytes);

impl SocketAddrV6NonZero {
    /// Create a new SocketAddrV6NonZero, validating port is non-zero.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortIsZero` if port is 0.
    pub fn new(ip: Ipv6Bytes, port: u16) -> Result<Self, ValidationError> {
        if port == 0 {
            return Err(ValidationError::PortIsZero);
        }
        Ok(Self(SocketAddrV6Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV6Bytes.
    pub fn get(&self) -> &SocketAddrV6Bytes {
        &self.0
    }

    /// Get the IP address.
    pub fn ip(&self) -> &Ipv6Bytes {
        self.0.ip()
    }

    /// Get the port (guaranteed non-zero).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV6Bytes.
    pub fn into_inner(self) -> SocketAddrV6Bytes {
        self.0
    }
}

// ============================================================================
// Contract Types: Privileged Ports
// ============================================================================

/// An IPv4 socket address with privileged port (< 1024).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV4Privileged(SocketAddrV4Bytes);

impl SocketAddrV4Privileged {
    /// Create a new SocketAddrV4Privileged, validating port is privileged.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortNotPrivileged` if port >= 1024.
    pub fn new(ip: Ipv4Bytes, port: u16) -> Result<Self, ValidationError> {
        if port >= 1024 {
            return Err(ValidationError::PortNotPrivileged(port));
        }
        Ok(Self(SocketAddrV4Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV4Bytes.
    pub fn get(&self) -> &SocketAddrV4Bytes {
        &self.0
    }

    /// Get the port (guaranteed < 1024).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV4Bytes.
    pub fn into_inner(self) -> SocketAddrV4Bytes {
        self.0
    }
}

/// An IPv6 socket address with privileged port (< 1024).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV6Privileged(SocketAddrV6Bytes);

impl SocketAddrV6Privileged {
    /// Create a new SocketAddrV6Privileged, validating port is privileged.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortNotPrivileged` if port >= 1024.
    pub fn new(ip: Ipv6Bytes, port: u16) -> Result<Self, ValidationError> {
        if port >= 1024 {
            return Err(ValidationError::PortNotPrivileged(port));
        }
        Ok(Self(SocketAddrV6Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV6Bytes.
    pub fn get(&self) -> &SocketAddrV6Bytes {
        &self.0
    }

    /// Get the port (guaranteed < 1024).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV6Bytes.
    pub fn into_inner(self) -> SocketAddrV6Bytes {
        self.0
    }
}

// ============================================================================
// Contract Types: Unprivileged Ports
// ============================================================================

/// An IPv4 socket address with unprivileged port (>= 1024).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV4Unprivileged(SocketAddrV4Bytes);

impl SocketAddrV4Unprivileged {
    /// Create a new SocketAddrV4Unprivileged, validating port is unprivileged.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortIsPrivileged` if port < 1024.
    pub fn new(ip: Ipv4Bytes, port: u16) -> Result<Self, ValidationError> {
        if port < 1024 {
            return Err(ValidationError::PortIsPrivileged(port));
        }
        Ok(Self(SocketAddrV4Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV4Bytes.
    pub fn get(&self) -> &SocketAddrV4Bytes {
        &self.0
    }

    /// Get the port (guaranteed >= 1024).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV4Bytes.
    pub fn into_inner(self) -> SocketAddrV4Bytes {
        self.0
    }
}

/// An IPv6 socket address with unprivileged port (>= 1024).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SocketAddrV6Unprivileged(SocketAddrV6Bytes);

impl SocketAddrV6Unprivileged {
    /// Create a new SocketAddrV6Unprivileged, validating port is unprivileged.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::PortIsPrivileged` if port < 1024.
    pub fn new(ip: Ipv6Bytes, port: u16) -> Result<Self, ValidationError> {
        if port < 1024 {
            return Err(ValidationError::PortIsPrivileged(port));
        }
        Ok(Self(SocketAddrV6Bytes::new(ip, port)))
    }

    /// Get the underlying SocketAddrV6Bytes.
    pub fn get(&self) -> &SocketAddrV6Bytes {
        &self.0
    }

    /// Get the port (guaranteed >= 1024).
    pub fn port(&self) -> u16 {
        self.0.port()
    }

    /// Unwrap into the underlying SocketAddrV6Bytes.
    pub fn into_inner(self) -> SocketAddrV6Bytes {
        self.0
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_port_ranges() {
        assert!(is_well_known_port(80));
        assert!(is_well_known_port(443));
        assert!(!is_well_known_port(1024));

        assert!(is_registered_port(3000));
        assert!(is_registered_port(5432));
        assert!(!is_registered_port(1023));
        assert!(!is_registered_port(49152));

        assert!(is_dynamic_port(49152));
        assert!(is_dynamic_port(65535));
        assert!(!is_dynamic_port(49151));
    }

    #[test]
    fn test_privileged_port() {
        assert!(is_privileged_port(0));
        assert!(is_privileged_port(80));
        assert!(is_privileged_port(1023));
        assert!(!is_privileged_port(1024));
        assert!(!is_privileged_port(8080));
    }

    #[test]
    fn test_socketaddrv4_construction() {
        let ip = Ipv4Bytes::new([192, 168, 1, 1]);
        let port = 8080;

        let addr = SocketAddrV4Bytes::new(ip, port);
        assert_eq!(addr.ip().octets(), [192, 168, 1, 1]);
        assert_eq!(addr.port(), 8080);
    }

    #[test]
    fn test_socketaddrv4_nonzero() {
        let ip = Ipv4Bytes::new([192, 168, 1, 1]);

        let zero_result = SocketAddrV4NonZero::new(ip, 0);
        assert!(zero_result.is_err());

        let nonzero_result = SocketAddrV4NonZero::new(ip, 8080);
        assert!(nonzero_result.is_ok());
        assert_eq!(nonzero_result.unwrap().port(), 8080);
    }

    #[test]
    fn test_socketaddrv4_privileged() {
        let ip = Ipv4Bytes::new([192, 168, 1, 1]);

        let privileged = SocketAddrV4Privileged::new(ip, 80);
        assert!(privileged.is_ok());
        assert_eq!(privileged.unwrap().port(), 80);

        let unprivileged = SocketAddrV4Privileged::new(ip, 8080);
        assert!(unprivileged.is_err());
    }

    #[test]
    fn test_socketaddrv4_unprivileged() {
        let ip = Ipv4Bytes::new([192, 168, 1, 1]);

        let unprivileged = SocketAddrV4Unprivileged::new(ip, 8080);
        assert!(unprivileged.is_ok());
        assert_eq!(unprivileged.unwrap().port(), 8080);

        let privileged = SocketAddrV4Unprivileged::new(ip, 80);
        assert!(privileged.is_err());
    }

    #[test]
    fn test_socketaddrv6_construction() {
        let ip = Ipv6Bytes::new([0x20, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
        let port = 8080;

        let addr = SocketAddrV6Bytes::new(ip, port);
        assert_eq!(addr.port(), 8080);
    }

    #[test]
    fn test_socketaddrv6_nonzero() {
        let ip = Ipv6Bytes::new([0x20, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);

        let zero_result = SocketAddrV6NonZero::new(ip, 0);
        assert!(zero_result.is_err());

        let nonzero_result = SocketAddrV6NonZero::new(ip, 8080);
        assert!(nonzero_result.is_ok());
    }
}