hyperdb-api-core 0.1.0

Internal implementation details for hyperdb-api. Not a stable API; use hyperdb-api instead.
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Connection endpoint types for Hyper database.
//!
//! This module provides [`ConnectionEndpoint`] for representing different
//! transport mechanisms (TCP, Unix Domain Socket, Windows Named Pipe).

use std::fmt;
#[cfg(unix)]
use std::path::PathBuf;

use super::error::{Error, Result};

/// Represents a connection endpoint for Hyper database.
///
/// Supports different transport mechanisms:
/// - TCP: `tab.tcp://host:port`
/// - Unix Domain Socket: `tab.domain://<directory>/domain/<name>` (Unix only)
/// - Windows Named Pipe: `tab.pipe://<host>/pipe/<name>` (Windows only, future)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionEndpoint {
    /// TCP endpoint: `tab.tcp://host:port`
    Tcp {
        /// Hostname or IP address
        host: String,
        /// Port number (0 for auto-assign)
        port: u16,
    },

    /// Unix Domain Socket: `tab.domain://<directory>/domain/<name>`
    #[cfg(unix)]
    DomainSocket {
        /// Directory containing the socket file
        directory: PathBuf,
        /// Socket name (e.g., `.s.PGSQL.12345`)
        name: String,
    },
    /// Windows Named Pipe: `tab.pipe://<host>/pipe/<name>`
    ///
    /// **TODO (Windows)**: Implement Named Pipe support for Windows IPC.
    /// See IPC_IMPLEMENTATION.md for detailed implementation guide.
    ///
    /// Example format: `tab.pipe://./pipe/hyper-12345` for local pipe
    #[cfg(windows)]
    NamedPipe {
        /// Server name ("." for local machine)
        host: String,
        /// Pipe name (e.g., "hyper-12345")
        name: String,
    },
}

impl ConnectionEndpoint {
    /// Creates a new TCP endpoint.
    pub fn tcp(host: impl Into<String>, port: u16) -> Self {
        ConnectionEndpoint::Tcp {
            host: host.into(),
            port,
        }
    }

    /// Creates a new Unix Domain Socket endpoint.
    #[cfg(unix)]
    pub fn domain_socket(directory: impl Into<PathBuf>, name: impl Into<String>) -> Self {
        ConnectionEndpoint::DomainSocket {
            directory: directory.into(),
            name: name.into(),
        }
    }

    /// Creates a new Windows Named Pipe endpoint.
    ///
    /// # Arguments
    /// * `host` - Server name ("." for local machine)
    /// * `name` - Pipe name (e.g., "hyper-12345")
    #[cfg(windows)]
    pub fn named_pipe(host: impl Into<String>, name: impl Into<String>) -> Self {
        ConnectionEndpoint::NamedPipe {
            host: host.into(),
            name: name.into(),
        }
    }

    /// Parses a connection descriptor string.
    ///
    /// Supported formats:
    /// - `tab.tcp://host:port` or `host:port` → TCP
    /// - `tab.domain://<dir>/domain/<name>` → Unix Domain Socket
    /// - `tab.pipe://<host>/pipe/<name>` → Named Pipe (future)
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (connection) when:
    /// - The descriptor has the `tab.domain://` prefix but is missing
    ///   the `/domain/` separator, has an empty socket name, or is
    ///   used on a non-Unix platform.
    /// - The descriptor has the `tab.pipe://` prefix but malformed
    ///   `/pipe/` segment, empty name, or is used on a non-Windows
    ///   platform.
    /// - The TCP descriptor cannot be parsed into a `host:port` pair,
    ///   or the port is not a valid `u16`.
    pub fn parse(descriptor: &str) -> Result<Self> {
        // Unix Domain Socket: tab.domain://<directory>/domain/<name>
        #[allow(unused_variables, reason = "`rest` is unused on non-unix platforms")]
        if let Some(rest) = descriptor.strip_prefix("tab.domain://") {
            #[cfg(unix)]
            {
                let idx = rest.find("/domain/").ok_or_else(|| {
                    Error::connection(format!(
                        "Invalid domain socket format: '{descriptor}'. Expected 'tab.domain://<dir>/domain/<name>'"
                    ))
                })?;
                let directory = &rest[..idx];
                let name = &rest[idx + 8..]; // "/domain/".len() == 8

                if name.is_empty() {
                    return Err(Error::connection("Domain socket name cannot be empty"));
                }

                return Ok(ConnectionEndpoint::DomainSocket {
                    directory: PathBuf::from(directory),
                    name: name.to_string(),
                });
            }
            #[cfg(not(unix))]
            {
                return Err(Error::connection(
                    "Unix domain sockets are not supported on this platform",
                ));
            }
        }

        // Named Pipe: tab.pipe://<host>/pipe/<name>
        #[allow(unused_variables, reason = "`rest` is unused on non-windows platforms")]
        if let Some(rest) = descriptor.strip_prefix("tab.pipe://") {
            #[cfg(windows)]
            {
                // Format: tab.pipe://<host>/pipe/<name>
                // Example: tab.pipe://./pipe/hyper-12345
                let idx = rest.find("/pipe/").ok_or_else(|| {
                    Error::connection(format!(
                        "Invalid named pipe format: '{descriptor}'. Expected 'tab.pipe://<host>/pipe/<name>'"
                    ))
                })?;
                let host = &rest[..idx];
                let name = &rest[idx + 6..]; // "/pipe/".len() == 6

                if name.is_empty() {
                    return Err(Error::connection("Named pipe name cannot be empty"));
                }

                return Ok(ConnectionEndpoint::NamedPipe {
                    host: host.to_string(),
                    name: name.to_string(),
                });
            }
            #[cfg(not(windows))]
            {
                return Err(Error::connection(
                    "Named pipes are not supported on this platform",
                ));
            }
        }

        // TCP: tab.tcp://host:port or just host:port
        let tcp_part = descriptor
            .strip_prefix("tab.tcp://")
            .or_else(|| descriptor.strip_prefix("tcp.libpq://"))
            .unwrap_or(descriptor);

        Self::parse_tcp(tcp_part)
    }

    /// Parses a TCP host:port string.
    fn parse_tcp(s: &str) -> Result<Self> {
        // Handle IPv6 addresses: [::1]:port
        if s.starts_with('[') {
            let end_bracket = s
                .find(']')
                .ok_or_else(|| Error::connection(format!("Invalid IPv6 address format: '{s}'")))?;
            let host = &s[1..end_bracket];
            let port_str = s[end_bracket + 1..]
                .strip_prefix(':')
                .ok_or_else(|| Error::connection(format!("Missing port in: '{s}'")))?;

            let port = Self::parse_port(port_str)?;
            return Ok(ConnectionEndpoint::Tcp {
                host: host.to_string(),
                port,
            });
        }

        // Regular host:port
        let colon_idx = s.rfind(':').ok_or_else(|| {
            Error::connection(format!(
                "Invalid endpoint format: '{s}'. Expected 'host:port'"
            ))
        })?;

        let host = &s[..colon_idx];
        let port_str = &s[colon_idx + 1..];

        if host.is_empty() {
            return Err(Error::connection("Host cannot be empty"));
        }

        let port = Self::parse_port(port_str)?;

        Ok(ConnectionEndpoint::Tcp {
            host: host.to_string(),
            port,
        })
    }

    /// Parses a port string, handling "auto" as 0.
    fn parse_port(s: &str) -> Result<u16> {
        if s == "auto" {
            return Ok(0);
        }
        s.parse::<u16>()
            .map_err(|_| Error::connection(format!("Invalid port number: '{s}'")))
    }

    /// Returns the connection descriptor string format.
    ///
    /// This is the format used by Hyper for `--listen-connection` and `--callback-connection`.
    #[must_use]
    pub fn to_descriptor(&self) -> String {
        match self {
            ConnectionEndpoint::Tcp { host, port } => {
                let port_str = if *port == 0 {
                    "auto".to_string()
                } else {
                    port.to_string()
                };
                // Handle IPv6 addresses
                if host.contains(':') {
                    format!("tab.tcp://[{host}]:{port_str}")
                } else {
                    format!("tab.tcp://{host}:{port_str}")
                }
            }
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { directory, name } => {
                format!("tab.domain://{}/domain/{}", directory.display(), name)
            }
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { host, name } => {
                format!("tab.pipe://{host}/pipe/{name}")
            }
        }
    }

    /// Returns the socket file path for Unix Domain Sockets.
    #[cfg(unix)]
    #[must_use]
    pub fn socket_path(&self) -> Option<PathBuf> {
        match self {
            ConnectionEndpoint::DomainSocket { directory, name } => Some(directory.join(name)),
            ConnectionEndpoint::Tcp { .. } => None,
        }
    }

    /// Returns the Windows pipe path (e.g., `\\.\pipe\hyper-12345`).
    #[cfg(windows)]
    pub fn pipe_path(&self) -> Option<String> {
        match self {
            ConnectionEndpoint::NamedPipe { host, name } => {
                Some(format!("\\\\{host}\\pipe\\{name}"))
            }
            ConnectionEndpoint::Tcp { .. } => None,
        }
    }

    /// Returns true if this is a TCP endpoint.
    #[must_use]
    pub fn is_tcp(&self) -> bool {
        matches!(self, ConnectionEndpoint::Tcp { .. })
    }

    /// Returns true if this is a Unix Domain Socket endpoint.
    #[cfg(unix)]
    #[must_use]
    pub fn is_domain_socket(&self) -> bool {
        matches!(self, ConnectionEndpoint::DomainSocket { .. })
    }

    /// Returns true if this is a Windows Named Pipe endpoint.
    #[cfg(windows)]
    pub fn is_named_pipe(&self) -> bool {
        matches!(self, ConnectionEndpoint::NamedPipe { .. })
    }

    /// Returns the host and port for TCP endpoints.
    #[must_use]
    pub fn tcp_addr(&self) -> Option<(&str, u16)> {
        match self {
            ConnectionEndpoint::Tcp { host, port } => Some((host, *port)),
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { .. } => None,
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { .. } => None,
        }
    }
}

impl fmt::Display for ConnectionEndpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConnectionEndpoint::Tcp { host, port } => {
                if host.contains(':') {
                    write!(f, "[{host}]:{port}")
                } else {
                    write!(f, "{host}:{port}")
                }
            }
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { directory, name } => {
                write!(f, "{}/{}", directory.display(), name)
            }
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { host, name } => {
                write!(f, "\\\\{host}\\pipe\\{name}")
            }
        }
    }
}

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

    #[test]
    fn test_parse_tcp_simple() {
        let ep = ConnectionEndpoint::parse("localhost:7483").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::Tcp {
                host: "localhost".to_string(),
                port: 7483
            }
        );
    }

    #[test]
    fn test_parse_tcp_with_scheme() {
        let ep = ConnectionEndpoint::parse("tab.tcp://127.0.0.1:7483").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::Tcp {
                host: "127.0.0.1".to_string(),
                port: 7483
            }
        );
    }

    #[test]
    fn test_parse_tcp_auto_port() {
        let ep = ConnectionEndpoint::parse("tab.tcp://localhost:auto").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::Tcp {
                host: "localhost".to_string(),
                port: 0
            }
        );
    }

    #[test]
    fn test_parse_tcp_ipv6() {
        let ep = ConnectionEndpoint::parse("tab.tcp://[::1]:7483").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::Tcp {
                host: "::1".to_string(),
                port: 7483
            }
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_parse_domain_socket() {
        let ep =
            ConnectionEndpoint::parse("tab.domain:///tmp/hyper/domain/.s.PGSQL.12345").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::DomainSocket {
                directory: PathBuf::from("/tmp/hyper"),
                name: ".s.PGSQL.12345".to_string()
            }
        );
    }

    #[test]
    fn test_to_descriptor_tcp() {
        let ep = ConnectionEndpoint::tcp("localhost", 7483);
        assert_eq!(ep.to_descriptor(), "tab.tcp://localhost:7483");
    }

    #[test]
    fn test_to_descriptor_tcp_auto() {
        let ep = ConnectionEndpoint::tcp("localhost", 0);
        assert_eq!(ep.to_descriptor(), "tab.tcp://localhost:auto");
    }

    #[cfg(unix)]
    #[test]
    fn test_to_descriptor_domain_socket() {
        let ep = ConnectionEndpoint::domain_socket("/tmp/hyper", ".s.PGSQL.12345");
        assert_eq!(
            ep.to_descriptor(),
            "tab.domain:///tmp/hyper/domain/.s.PGSQL.12345"
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_socket_path() {
        let ep = ConnectionEndpoint::domain_socket("/tmp/hyper", ".s.PGSQL.12345");
        assert_eq!(
            ep.socket_path(),
            Some(PathBuf::from("/tmp/hyper/.s.PGSQL.12345"))
        );
    }

    #[test]
    fn test_display_tcp() {
        let ep = ConnectionEndpoint::tcp("localhost", 7483);
        assert_eq!(format!("{ep}"), "localhost:7483");
    }

    #[cfg(unix)]
    #[test]
    fn test_display_domain_socket() {
        let ep = ConnectionEndpoint::domain_socket("/tmp/hyper", ".s.PGSQL.12345");
        assert_eq!(format!("{ep}"), "/tmp/hyper/.s.PGSQL.12345");
    }

    #[cfg(windows)]
    #[test]
    fn test_parse_named_pipe() {
        let ep = ConnectionEndpoint::parse("tab.pipe://./pipe/hyper-12345").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::NamedPipe {
                host: ".".to_string(),
                name: "hyper-12345".to_string()
            }
        );
    }

    #[cfg(windows)]
    #[test]
    fn test_parse_named_pipe_remote() {
        let ep = ConnectionEndpoint::parse("tab.pipe://server1/pipe/hyper-db").unwrap();
        assert_eq!(
            ep,
            ConnectionEndpoint::NamedPipe {
                host: "server1".to_string(),
                name: "hyper-db".to_string()
            }
        );
    }

    #[cfg(windows)]
    #[test]
    fn test_to_descriptor_named_pipe() {
        let ep = ConnectionEndpoint::named_pipe(".", "hyper-12345");
        assert_eq!(ep.to_descriptor(), "tab.pipe://./pipe/hyper-12345");
    }

    #[cfg(windows)]
    #[test]
    fn test_pipe_path() {
        let ep = ConnectionEndpoint::named_pipe(".", "hyper-12345");
        assert_eq!(ep.pipe_path(), Some(r"\\.\pipe\hyper-12345".to_string()));
    }

    #[cfg(windows)]
    #[test]
    fn test_display_named_pipe() {
        let ep = ConnectionEndpoint::named_pipe(".", "hyper-12345");
        assert_eq!(format!("{ep}"), r"\\.\pipe\hyper-12345");
    }

    #[cfg(windows)]
    #[test]
    fn test_named_pipe_is_methods() {
        let ep = ConnectionEndpoint::named_pipe(".", "test");
        assert!(!ep.is_tcp());
        assert!(ep.is_named_pipe());
    }
}