pub fn split_host_port(addr: &str) -> (&str, &str) {
if let Some(rest) = addr.strip_prefix('[') {
if let Some((host, port)) = rest.split_once("]:") {
(host, port)
} else {
(rest.trim_end_matches(']'), "5432")
}
} else if let Some(colon) = addr.rfind(':') {
if addr[..colon].contains(':') {
(addr, "5432")
} else {
(&addr[..colon], &addr[colon + 1..])
}
} else {
(addr, "5432")
}
}
#[cfg(test)]
mod tests {
use super::split_host_port;
#[test]
fn simple_host_port() {
assert_eq!(split_host_port("localhost:5432"), ("localhost", "5432"));
}
#[test]
fn ipv4_port() {
assert_eq!(split_host_port("127.0.0.1:5433"), ("127.0.0.1", "5433"));
}
#[test]
fn bracketed_ipv6_port() {
assert_eq!(split_host_port("[::1]:5432"), ("::1", "5432"));
}
#[test]
fn bracketed_ipv6_no_port() {
assert_eq!(split_host_port("[::1]"), ("::1", "5432"));
}
#[test]
fn bare_ipv6_no_port() {
assert_eq!(split_host_port("::1"), ("::1", "5432"));
}
#[test]
fn hostname_no_port() {
assert_eq!(
split_host_port("pg-primary.internal"),
("pg-primary.internal", "5432")
);
}
#[test]
fn custom_port() {
assert_eq!(
split_host_port("db.example.com:6432"),
("db.example.com", "6432")
);
}
#[test]
fn full_ipv6_bracketed() {
assert_eq!(
split_host_port("[2001:db8::1]:5432"),
("2001:db8::1", "5432")
);
}
}