use derive_more::{AsRef, Deref, Display, From, Into};
use std::net::SocketAddr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, From, Into, AsRef, Deref)]
pub struct ClientAddress(SocketAddr);
impl ClientAddress {
#[inline]
#[must_use]
pub const fn new(addr: SocketAddr) -> Self {
Self(addr)
}
#[inline]
#[must_use]
pub const fn as_socket_addr(&self) -> &SocketAddr {
&self.0
}
#[inline]
#[must_use]
pub const fn into_inner(self) -> SocketAddr {
self.0
}
#[inline]
#[must_use]
pub const fn is_ipv4(&self) -> bool {
self.0.is_ipv4()
}
#[inline]
#[must_use]
pub const fn is_ipv6(&self) -> bool {
self.0.is_ipv6()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
#[test]
fn test_display() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)), 12345);
let client_addr = ClientAddress::from(addr);
assert_eq!(format!("{client_addr}"), "192.168.1.100:12345");
}
#[test]
fn test_debug() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let client_addr = ClientAddress::from(addr);
assert_eq!(format!("{client_addr:?}"), "ClientAddress(127.0.0.1:8119)");
}
#[test]
fn test_is_ipv4() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let client_addr = ClientAddress::from(addr);
assert!(client_addr.is_ipv4());
assert!(!client_addr.is_ipv6());
}
#[test]
fn test_is_ipv6() {
let addr = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 8119);
let client_addr = ClientAddress::from(addr);
assert!(client_addr.is_ipv6());
assert!(!client_addr.is_ipv4());
}
#[test]
fn test_equality() {
let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let addr3 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8120);
let client1 = ClientAddress::from(addr1);
let client2 = ClientAddress::from(addr2);
let client3 = ClientAddress::from(addr3);
assert_eq!(client1, client2);
assert_ne!(client1, client3);
}
#[test]
fn test_hash() {
use std::collections::HashSet;
let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let client1 = ClientAddress::from(addr1);
let client2 = ClientAddress::from(addr2);
let mut set = HashSet::new();
set.insert(client1);
assert!(set.contains(&client2));
}
#[test]
fn test_deref() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let client_addr = ClientAddress::from(addr);
assert!(client_addr.is_ipv4());
assert_eq!(client_addr.port(), 8119);
}
#[test]
fn test_as_ref() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8119);
let client_addr = ClientAddress::from(addr);
let socket_ref: &SocketAddr = client_addr.as_ref();
assert_eq!(socket_ref, &addr);
}
}